-
Notifications
You must be signed in to change notification settings - Fork 0
/
10305 - Ordering Tasks.cpp
52 lines (50 loc) · 1.07 KB
/
10305 - Ordering Tasks.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
51
52
#include<bits/stdc++.h>
using namespace std;
int main()
{
int u,v;
int node,edge;
while(1)
{
cin>>node>>edge;
vector<int>adj[node+1];
int in[node+1];
memset(in,0,sizeof(in));
if(node==0 && edge==0)
break;
for(int i=0; i<edge; i++)
{
cin>>u>>v;
adj[u].push_back(v);
in[v]++;
}
queue<int>q;
vector<int>order;
for(int 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(int i=0; i<adj[u].size(); i++)
{
v=adj[u][i];
in[v]--;
if(in[v]==0)
q.push(v);
}
}
for(int i=0; i<node; i++)
{
if(i)
cout<<' ';
cout<<order[i];
}
cout<<endl;
}
return 0;
}