fork download
  1. //http://i...content-available-to-author-only...e.com/vNEv5A
  2. //169445
  3. #include <iostream>
  4. #include <cstdio>
  5. using namespace std;
  6.  
  7. int n, adj[105][105]={0}, c[105], a;
  8.  
  9. bool dfs(int v, int col) {
  10. if(c[v]!=-1 && c[v]==col) return true;
  11. else if(c[v]!=-1 && c[v]!=col) return false;
  12. c[v]=col;
  13. for(int i=0; i<n; i++) if(adj[v][i] && !dfs(i, 1-col)) return false;
  14. return true;
  15. }
  16.  
  17. int main() {
  18. scanf("%d", &n);
  19. for(int i=0; i<n; i++) while(true) {
  20. scanf("%d", &a);
  21. if(a==0) break;
  22. adj[i][a-1]=adj[a-1][i]=1;
  23. }
  24. for(int i=0; i<105; i++) c[i]=-1;
  25. if(dfs(0, 0)==true) for(int i=0; i<n; i++) printf("%d", c[i]);
  26. else printf("-1");
  27. return 0;
  28. }
Success #stdin #stdout 0s 3340KB
stdin
3
2 0
3 0
0
stdout
010