UPC- number of combinations

Learning is like rowing upstream

The number of combinations

Title Description

Pick some integer number from 1 to N, the number to form a set of programs is counted. If the plus M limit: A number 2 can not simultaneously have to choose how to calculate?

Entry

The first line contains two integers N and M, 1≤N≤20,1≤M≤400. Below M rows, each row two different integers a and b represent the two
number can not be selected simultaneously. 1≤a, b≤N, some restrictions may occur repeatedly.

Export

An integer.

Sample Input

3 2
1 2
2 3

Sample Output

5

Sample Input

3 3
1 2
1 3
2 3

Sample Output

4

Problem-solving ideas

First to see the first reaction to this problem is to look after the data size, because this problem is not a mathematical combination is violence. Obviously this is a topic of violence, because only 20 data complexity in 2 the n- around, so go directly to violence, there is no problem.
Reflection
Why TLE? ? ? ?

After seeing the title, it is determined dfs, but the idea was wrong. Did not realize that this is a 01dfs Ever think of that tree structure, is achieved by pruning, but ignores the complexity of the implementation and pruning between the code, leading to a timeout, why not to think about the deeper reason is 01dfs . . .
for (int I = 0; I <n; I ++)
for (int J = 0; J <n; J ++)
of this process complexity understanding a problem, wrong to n 2 ! appreciated became n complex degree. In the corrected and after aware of these errors can write dfs, and then you can have fun a Accepted!

Oops

AC Time to
first paste the code

#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<string>
#include<math.h>
#include<stdio.h>
#pragma GCC optimize(2)
#define Swap(a,b)  a ^= b ^= a ^= b
using namespace std;
typedef long long ll;
const int MAXN=10010;
const ll long_inf=9223372036854775807;
const int int_inf=2147483647;
inline ll read() {
	ll c=getchar(),Nig=1,x=0;
	while(!isdigit(c)&&c!='-')c=getchar();
	if(c=='-')Nig=-1,c=getchar();
	while(isdigit(c))x=((x<<1)+(x<<3))+(c^'0'),c=getchar();
	return Nig*x;
}
bool dp[30][30];
bool save[30];
ll n,t;
ll ans;
void dfs(int t) {
	if(t==n){
		for(int i=1;i<=n;i++)
		if(save[i])
		{
			for(int j=1;j<=n;j++)
			if(save[j]&&dp[i][j])
			return ;
		}
		ans++;
		return ;
	}
	dfs(t+1);
	save[t+1]=1;
	dfs(t+1);
	save[t+1]=0;
}
int main() {
	n=read(),t=read();
	for(ll i=0; i<t; i++) {
		ll a,b;
		a=read(),b=read();
		dp[a][b]=1;
		dp[b][a]=1;
	}
	dfs(0);
	cout<<ans<<endl;
	return 0;
}

Thinking is very clear, that is binary enumeration, enumeration out after then judge it, with an array of judgment (this nor that dp dp is a judge data)

Ideas extension
In fact, this dfs in determining whether there is a code that can be optimized (but do not know why the assessment but more slowly ??)
First put the code

#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<string>
#include<math.h>
#include<stdio.h>
#pragma GCC optimize(2)
#define Swap(a,b)  a ^= b ^= a ^= b
using namespace std;
typedef long long ll;
const int MAXN=10010;
const ll long_inf=9223372036854775807;
const int int_inf=2147483647;
inline ll read() {
	ll c=getchar(),Nig=1,x=0;
	while(!isdigit(c)&&c!='-')c=getchar();
	if(c=='-')Nig=-1,c=getchar();
	while(isdigit(c))x=((x<<1)+(x<<3))+(c^'0'),c=getchar();
	return Nig*x;
}
bool dp[30][30];
bool save[30];
ll n,t;
ll ans;
int main() {
	n=read(),t=read();
	for(ll i=0; i<t; i++) {
		ll a,b;
		a=read(),b=read();
		dp[a][b]=1;
		dp[b][a]=1;
	}
	int temp=(1<<n);
	int ans=0;
	for(int i=0; i<temp; i++) {
		for(int j=0; j<n; j++)
			if(i&(1<<j))
				for(int k=0; k<n; k++)
					if(i&(1<<k)&&dp[j+1][k+1])
						goto A;
		ans++;
A:
		;
	}
	cout<<ans<<endl;
	return 0;
}

This is the general general solution (enum binary)
you can actually see the binary based on 01 questions out there that number does not appear, and with memory arrays is about the same effect.
Binary enumeration in fact, be able to determine the operational use & this operator.
1 << n is determined by the n + 1 bit is a true or flase

After the game Reflection
experienced this failure to understand the learning is endless, learning is like riding a boat to fall behind

There is a road ground for the tracks, Boundless Learning bitter for the boat.

By- wheel month

Published 32 original articles · won praise 12 · views 1193

Guess you like

Origin blog.csdn.net/qq_35339563/article/details/104149228