mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-10 05:58:48 +00:00
P3916 图的遍历
R41283496
This commit is contained in:
parent
c2d037d85a
commit
14ccb6a3bc
32
problem/P3916/P3916.cpp
Normal file
32
problem/P3916/P3916.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int n, m, u, v, a[100010];
|
||||
vector<int> g[100010];
|
||||
|
||||
void dfs(int x, int d) {
|
||||
if (a[x]) {
|
||||
return;
|
||||
}
|
||||
a[x] = d;
|
||||
for (int i = 0; i < g[x].size(); i++) {
|
||||
dfs(g[x][i], d);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
for (int i = 0; i < m; i++) {
|
||||
cin >> u >> v;
|
||||
g[v].push_back(u);
|
||||
}
|
||||
for (int i = n; i > 0; i--) {
|
||||
dfs(i, i);
|
||||
}
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cout << a[i] << ' ';
|
||||
}
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user