Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stack>
- using namespace std;
- struct info
- {
- int adjacent[10005],total,status;
- }list[10005];
- bool visited[10005];
- stack<int>s;
- bool possibile(int total);
- bool check(int point);
- int main()
- {
- int friends,i,test,friendships,f1,f2;
- cin>>test;
- while(test--)
- {
- cin>>friends>>friendships;
- for(i=0;i<friends;i++)
- {
- list[i].total=visited[i]=false;
- cin>>list[i].status;
- }
- for(i=0;i<friendships;i++)
- {
- cin>>f1>>f2;
- list[f1].adjacent[list[f1].total++]=f2;
- list[f2].adjacent[list[f2].total++]=f1;
- }
- if(possibile(friends))
- cout<<"POSSIBLE"<<endl;
- else
- cout<<"IMPOSSIBLE"<<endl;
- }
- return 0;
- }
- bool possibile(int friends)
- {
- int i,flag=0;
- while(!flag)
- {
- flag=1;
- for(i=0;i<friends;i++)
- {
- if(!visited[i])
- {
- flag=0;
- if(!check(i))
- return false;
- }
- }
- }
- return true;
- }
- bool check(int point)
- {
- int imbalance=0,temp,temp2,size,i;
- while(!s.empty())
- s.pop();
- s.push(point);
- while(!s.empty())
- {
- temp=s.top();
- s.pop();
- visited[temp]=true;
- imbalance+=list[temp].status;
- size=list[temp].total;
- for(i=0;i<size;i++)
- {
- temp2=list[temp].adjacent[i];
- if(visited[temp2]==false)
- s.push(temp2);
- }
- }
- return !imbalance;
- }
Add Comment
Please, Sign In to add comment