【DFS】bzoj2079 [Poi2010]Guilds

对一棵树黑白染色一定符合题意。

图一定有生成树。

因此,仅有一个孤立节点的联通块不合题意。

DFS。

#include<cstdio>
using namespace std;
int n,m,en,x,y,v[1000001],first[500001],next[1000001],cnt;
bool vis[500001];
void AddEdge(const int &U,const int &V)
{
	v[++en]=V;
	next[en]=first[U];
	first[U]=en;
}
void dfs(int U)
{
	vis[U]=1; ++cnt;
	for(int i=first[U];i;i=next[i])
	  if(!vis[v[i]])
	    dfs(v[i]);
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;++i)
	  {
	  	scanf("%d%d",&x,&y);
	  	AddEdge(x,y);
	  	AddEdge(y,x);
	  }
	for(int i=1;i<=n;++i)
	  if(!vis[i])
		{
	  	  cnt=0;
	  	  dfs(i);
	  	  if(cnt==1)
	  	    {
	  	      puts("NIE");
	  	      return 0;
	  	    }
	    }
	puts("TAK");
	return 0;
}
posted @ 2015-01-25 07:29  AutSky_JadeK  阅读(163)  评论(0编辑  收藏  举报
TVアニメ「Charlotte(シャーロット)」公式サイト TVアニメ「Charlotte(シャーロット)」公式サイト