-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathBoj14567.cpp
More file actions
50 lines (50 loc) · 1.02 KB
/
Boj14567.cpp
File metadata and controls
50 lines (50 loc) · 1.02 KB
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 <iostream>
#include <queue>
#include <vector>
using namespace std;
int main(){
int n,m;
int l=0;
cin>>n>>m;
vector<int>v;
vector<vector<int>>map;
int result[n+1];
queue<int>q;
bool visited[n+1];
int arr[n+1];
for(int i=0;i<=n;i++){
arr[i]=0;
visited[i]=false;
map.push_back(vector<int>{});
}
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
arr[y]++;
map[x].push_back(y);
}
q.push(0);
int cnt=1;
while (!q.empty()){
int x=q.front();
cnt--;
q.pop();
for(int i=0;i<map[x].size();i++){
arr[map[x][i]]--;
}
if(cnt==0){
l++;
for(int i=1;i<=n;i++){
if(arr[i]==0&&!visited[i]){
cnt++;
q.push(i);
result[i]=l;
visited[i]= true;
}
}
}
}
for(int i=1;i<=n;i++)
cout<<result[i]<<" ";
return 0;
}