Advertisement
matheus_monteiro

Castelos da Nlogônia

Aug 20th, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, m;
  5. vector<int> G[200];
  6. int level[200], father[200], color[200];
  7.  
  8. void dfs(int v, int p, int d) {
  9.     father[v] = p;
  10.     level[v] = d;
  11.     for(int &u : G[v])
  12.         if(u != p)
  13.             dfs(u, v, d + 1);
  14. }
  15.  
  16. void paint(int u, int v, int c) {
  17.     while(u != v) {
  18.         color[u] = c;
  19.         color[v] = c;
  20.         if(level[u] > level[v])
  21.             u = father[u];
  22.         else
  23.             v = father[v];
  24.     }
  25.     color[u] = c;
  26. }
  27.  
  28. int32_t main() {
  29.     ios_base::sync_with_stdio(false);
  30.     cin.tie(nullptr);
  31.    
  32.     cin >> n >> m;
  33.     for(int i = 1; i < n; i++) {
  34.         int u, v;
  35.         cin >> u >> v; v--; u--;
  36.         G[u].push_back(v);
  37.         G[v].push_back(u);
  38.     }
  39.  
  40.     dfs(0, -1, 0);
  41.  
  42.     while(m--) {
  43.         int u, v, c;
  44.         cin >> u >> v >> c; u--; v--;
  45.         paint(u, v, c);
  46.     }
  47.  
  48.     for(int i = 0; i < n; i++)
  49.         cout << color[i] << ' ';
  50.     cout << endl;
  51.    
  52.     return 0;
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement