-
Notifications
You must be signed in to change notification settings - Fork 0
/
11686 - Pick up sticks.cpp
50 lines (50 loc) · 1.12 KB
/
11686 - Pick up sticks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int i,l;
long long int node,edge;
while(1)
{
int u,v;
cin>>node>>edge;
if(node==0 && edge==0)
break;
int in[node+1];
vector<int>adj[node+1];
memset(in,0,sizeof(in));
for(i=0; i<edge; i++)
{
cin>>u>>v;
adj[u].push_back(v);
in[v]++;
}
queue<int>q;
vector<int>order;
for(i=1; i<node; i++)
if(in[i]==0)
q.push(i);
while(!q.empty())
{
int v,u=q.front();
q.pop();
order.push_back(u);
for(i=0; i<adj[u].size(); i++)
{
v=adj[u][i];
in[v]--;
if(in[v]==0)
q.push(v);
}
}
l=order.size();
if(l!=node)
cout<<"IMPOSSIBLE"<<endl;
else
for(i=0; i<l; i++)
{
cout<<order[i]<<endl;
}
}
return 0;
}