http://acm.timus.ru/problem.aspx?space=1&num=1080
dfs 水题不解释
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;
const int N=103;
int head[N];
struct node
{
int j,next;
}side[N*N];
int I;
int ans;
int color[N];
void build(int i,int j)
{
side[I].j=j;
side[I].next=head[i];
head[i]=I++;
}
void dfs(int x,int k)
{
if(color[x]!=-1)
{
if(color[x]!=k)
ans=-1;
return ;
}
color[x]=k;
for(int t=head[x];t!=-1;t=side[t].next)
{
int l=side[t].j;
dfs(l,(k?0:1));
if(ans==-1)
return ;
}
}
int main()
{
//freopen("data.txt","r",stdin);
int n;
while(scanf("%d",&n)!=EOF)
{
memset(head,-1,sizeof(head));
I=0;
for(int i=1;i<=n;++i)
{
int j;
while(scanf("%d",&j),j)
{
build(i,j);
build(j,i);
}
}
ans=0;
memset(color,-1,sizeof(color));
dfs(1,0);
if(ans==-1)
{printf("-1\n");continue;}
for(int i=1;i<=n;++i)
printf("%d",color[i]);
printf("\n");
}
return 0;
}
浙公网安备 33010602011771号