「日常训练」All Friends(POJ-2989)

题意

分析

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#define MP make_pair
#define PB emplace_back
#define fi first
#define se second
#define ZERO(x) memset((x), 0, sizeof(x))
#define ALL(x) (x).begin(),(x).end()
#define rep(i, a, b) for (repType i = (a); i <= (b); ++i)
#define per(i, a, b) for (repType i = (a); i >= (b); --i)
#define QUICKIO                  \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
using namespace std;
typedef long long ll;
typedef int repType;

const int MAXN=130;
bool mat[MAXN][MAXN];
int n,m,ans;
int done[MAXN][MAXN], notyet[MAXN][MAXN], searched[MAXN][MAXN];

void dfs(int n, int dcnt, int ncnt, int scnt)
{
	if(!ncnt && !scnt) ans++;
	if(ans>1000) return;
	int key=notyet[n][1];
	rep(j,1,ncnt)
	{
		int v=notyet[n][j], tmp_ncnt=0, tmp_scnt=0;
		if(mat[key][v]) continue;
		memcpy(done[n+1],done[n],sizeof(int)*(dcnt+1));
		done[n+1][dcnt+1]=v;
		rep(i,1,ncnt) if(mat[v][notyet[n][i]])
			notyet[n+1][++tmp_ncnt]=notyet[n][i];
		rep(i,1,scnt) if(mat[v][searched[n][i]])
			searched[n+1][++tmp_scnt]=searched[n][i];
		dfs(n+1, dcnt+1, tmp_ncnt, tmp_scnt);
		notyet[n][j]=0;
		searched[n][++scnt]=v;
	}
}

int main()
{
	while(cin>>n>>m)
	{
		ZERO(mat); ans=0;
		rep(i,1,m)
		{
			int x,y;
			cin>>x>>y;
			mat[x][y]=mat[y][x]=true;
		}
		rep(i,1,n) notyet[1][i]=i;
		dfs(1,0,n,0);
		if(ans>1000) cout<<"Too many maximal sets of friends."<<endl;
		else cout<<ans<<endl;
	}
	return 0;
}
posted @ 2018-10-15 23:35  ISoLT  阅读(299)  评论(0编辑  收藏  举报