Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 423 Accepted Submission(s): 161
只有建图,
判断是否是二分图就可以了。
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013/9/21 星期六 12:22:50 4 File Name :2013南京网络赛\1004.cpp 5 ************************************************ */ 6 7 #pragma comment(linker, "/STACK:1024000000,1024000000") 8 #include <stdio.h> 9 #include <string.h> 10 #include <iostream> 11 #include <algorithm> 12 #include <vector> 13 #include <queue> 14 #include <set> 15 #include <map> 16 #include <string> 17 #include <math.h> 18 #include <stdlib.h> 19 #include <time.h> 20 using namespace std; 21 const int MAXN = 110; 22 23 int color[MAXN]; 24 int head[MAXN]; 25 struct Edge 26 { 27 int to,next; 28 }edge[MAXN*MAXN]; 29 int tot; 30 void addedge(int u,int v) 31 { 32 edge[tot].to = v; 33 edge[tot].next = head[u]; 34 head[u] = tot++; 35 } 36 void init() 37 { 38 tot = 0; 39 memset(head,-1,sizeof(head)); 40 } 41 42 43 bool dfs(int u,int col)//染色判断二分图 44 { 45 color[u] = col; 46 for(int i = head[u];i != -1;i = edge[i].next) 47 { 48 int v = edge[i].to; 49 if(color[v] != -1) 50 { 51 if(color[v]==col)return false; 52 continue; 53 } 54 if(!dfs(v,!col))return false; 55 } 56 return true; 57 } 58 59 int g[MAXN][MAXN]; 60 int main() 61 { 62 //freopen("in.txt","r",stdin); 63 //freopen("out.txt","w",stdout); 64 int n; 65 while(scanf("%d",&n) == 1) 66 { 67 memset(g,0,sizeof(g)); 68 int t; 69 for(int i = 1;i <= n;i++) 70 { 71 while(scanf("%d",&t) && t) 72 { 73 g[i][t] = 1; 74 } 75 } 76 init(); 77 for(int i = 1;i <= n;i++) 78 for(int j = i+1;j <= n;j++) 79 if(g[i][j] == 0 || g[j][i] == 0) 80 { 81 addedge(i,j); 82 addedge(j,i); 83 } 84 memset(color,-1,sizeof(color)); 85 bool flag = true; 86 for(int i = 1;i <= n;i++) 87 if(color[i] == -1 && dfs(i,0) == false) 88 { 89 flag = false; 90 break; 91 } 92 if(flag)printf("YES\n"); 93 else printf("NO\n"); 94 } 95 return 0; 96 }