ural 1106

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1106

本题题意就是看能否把一群人分成两个集合,是每一个集合中的人都有朋友在两一个集合中,由于前天做了,二分图,就直接套了,结果wa,现在想想他们中间是有区别的,主要在于,二分图要求分成两个集合,每一个集合内的人互相不认识,所以在对已经分过集合的人的处理上有很大的去别 ,只要他和他的朋友颜色一样就结束了  ,而这里不处理这种情况,只要保证在输入的时候判下是不是有没有朋友的情况就可以了。

View Code
 1 #include <iostream>
2 #include <cstring>
3 #include <cstdio>
4 #include <queue>
5 #include <cmath>
6 using namespace std;
7 int col[105];
8 int num[105][105];
9 int n;
10 bool BFS(int t)
11 {
12 int i;
13 queue<int>Q;
14 Q.push(t);
15 while (!Q.empty())
16 {
17 t=Q.front();
18 Q.pop();
19 for (i=1;i<=n;i++)
20 {
21 if(num[t][i])
22 {
23 if(col[i]==-1)
24 {
25 col[i]=col[t]^1;
26 Q.push(i);
27 }
28 // else if(col[i]==col[t])return false;
29 }
30 }
31
32 }
33
34 return true;
35 }
36 int main()
37 {
38 int i,x,m,flag;
39 while (scanf("%d",&n)!=EOF)
40 {
41 memset(col,0,sizeof(col));
42 for (i=1;i<=n;i++)
43 {
44 while (scanf("%d",&x))
45 {
46 if(x==0)break;
47 if(x==i)continue;
48 col[i]++;
49 col[x]++;
50 num[i][x]=num[x][i]=1;
51 }
52 }
53 flag=0;
54 for (i=1;i<=n;i++)
55 if(col[i]==0){flag=1;break;}
56 if(flag)printf("0\n");
57 else {
58 memset(col,-1,sizeof(col));
59 for (i=1;i<=n;i++)
60 {
61 if(col[i]==-1){
62 col[i]=1;
63 if(!BFS(i))
64 {
65 printf("0\n");
66 break;
67 }
68 }
69 }
70 m=0;
71 for (i=1;i<=n;i++)
72 {
73 if(col[i]==1)m++;
74 if(m==1&&!flag)flag=i;
75 }
76 printf("%d\n%d",m,flag);
77 for (i=flag+1;i<=n;i++)
78 {
79 if(col[i]==1)
80 printf(" %d",i);
81 }
82 printf("\n");
83 }
84 }
85 return 0;
86
87 }



posted @ 2012-03-25 09:51  我们一直在努力  阅读(229)  评论(0编辑  收藏  举报