Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: C++  |  size: 0.79 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <list>
  3. #include <queue>
  4. #include <iterator>
  5.  
  6. using namespace std;
  7.  
  8. list <int> graph[100];
  9. int mas[100];
  10.  
  11. queue <int> qu;
  12.  
  13. int main()
  14. {
  15.         int n,buf;
  16.         cin >> n;
  17.         for (int i=0;i<n;i++) mas[i]=0;
  18.          for (int i=0;i<n;i++)
  19.          {
  20.                  cin >> buf;
  21.                  while (buf)
  22.                  {
  23.                          graph[i].push_back(buf-1);
  24.                          cin >> buf;
  25.                  }
  26.          }
  27.  
  28.          qu.push(0);
  29.          mas[0]=1;
  30.  
  31.          while (!qu.empty())
  32.          {
  33.                  int now = qu.front(); qu.pop();
  34.                  list <int>::iterator it = graph[now].begin();
  35.                  while (it!=graph[now].end())
  36.                  {
  37.                          if (mas[*it]==mas[now]) { cout << -1; return 0;}
  38.                          if (mas[*it]!=mas[now]*(-1)) {  mas[*it]=mas[now]*(-1); qu.push(*it);}
  39.                          it++;
  40.                  }
  41.          }
  42.  
  43.          for (int i=0;i<n;i++) if (mas[i]==1) cout << 0; else cout << 1;
  44.  
  45.          system ("pause");
  46.         return 0;
  47. }