crysoberil

Untitled

Apr 20th, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include<iostream>
  2. #include<stack>
  3.  
  4. using namespace std;
  5.  
  6. struct info
  7. {
  8.     int adjacent[10005],total,status;
  9. }list[10005];
  10.  
  11. bool visited[10005];
  12. stack<int>s;
  13.  
  14. bool possibile(int total);
  15. bool check(int point);
  16.  
  17. int main()
  18. {
  19.     int friends,i,test,friendships,f1,f2;
  20.     cin>>test;
  21.     while(test--)
  22.     {
  23.         cin>>friends>>friendships;
  24.         for(i=0;i<friends;i++)
  25.         {
  26.             list[i].total=visited[i]=false;
  27.             cin>>list[i].status;
  28.         }
  29.         for(i=0;i<friendships;i++)
  30.         {
  31.             cin>>f1>>f2;
  32.             list[f1].adjacent[list[f1].total++]=f2;
  33.             list[f2].adjacent[list[f2].total++]=f1;
  34.         }
  35.         if(possibile(friends))
  36.             cout<<"POSSIBLE"<<endl;
  37.         else
  38.             cout<<"IMPOSSIBLE"<<endl;
  39.     }
  40.     return 0;
  41. }
  42.  
  43. bool possibile(int friends)
  44. {
  45.     int i,flag=0;
  46.     while(!flag)
  47.     {
  48.         flag=1;
  49.         for(i=0;i<friends;i++)
  50.         {
  51.             if(!visited[i])
  52.             {
  53.                 flag=0;
  54.                 if(!check(i))
  55.                     return false;
  56.             }
  57.         }
  58.     }
  59.     return true;
  60. }
  61.  
  62. bool check(int point)
  63. {
  64.     int imbalance=0,temp,temp2,size,i;
  65.     while(!s.empty())
  66.         s.pop();
  67.     s.push(point);
  68.     while(!s.empty())
  69.     {
  70.         temp=s.top();
  71.         s.pop();
  72.         visited[temp]=true;
  73.         imbalance+=list[temp].status;
  74.         size=list[temp].total;
  75.         for(i=0;i<size;i++)
  76.         {
  77.             temp2=list[temp].adjacent[i];
  78.             if(visited[temp2]==false)
  79.                 s.push(temp2);
  80.         }
  81.     }
  82.     return !imbalance;
  83. }
Add Comment
Please, Sign In to add comment