fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef vector<vector<int>> Graph;
  5.  
  6. void dfs(Graph& g, vector<int>& h, vector<int>& used, int u) {
  7. random_shuffle(begin(g[u]), end(g[u]));
  8. used[u] = 1;
  9. for(int v : g[u]) {
  10. if( !used[v] ) {
  11. dfs(g,h,used,v);
  12. }
  13. h[u] = min(h[u], h[v]);
  14. }
  15. }
  16.  
  17. int main() {
  18.  
  19. int n,m;
  20. while( cin>>n>>m ) {
  21. Graph g(n);
  22. for(int i = 0; i < m; i++) {
  23. int u,v; cin>>u>>v;
  24. g[u].push_back(v);
  25. }
  26. vector<int> h(n);
  27. for(int i = 0; i < n; i++)
  28. cin>>h[i];
  29. vector<int> permutation(n);
  30. for(int i = 0; i < n; i++)
  31. permutation[i] = i;
  32. for(int step = 0; step < 25; step++) {
  33. vector<int> used(n);
  34. random_shuffle(begin(permutation), end(permutation));
  35. for(int start = 0; start < n; start++)
  36. if( !used[permutation[start]])
  37. dfs(g,h,used,permutation[start]);
  38. }
  39. for(int i = 0; i < n; i++)
  40. cout<<h[i]<<" ";
  41. cout<<"\n";
  42. }
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 3464KB
stdin
4 4

0 1
1 2
2 3
3 0

1 2 3 4

26 25

0 25
1 25
2 25
3 25
4 25
5 25
6 25
7 25
8 25
9 25
10 25
11 25
12 25
13 25
14 25
15 25
16 25
17 25
18 25
19 25
20 25
21 25
22 25
23 25
24 25

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1
stdout
1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1