제출 #861437

#제출 시각아이디문제언어결과실행 시간메모리
861437maks007Min-max tree (BOI18_minmaxtree)C++14
0 / 100
226 ms21508 KiB
#include "bits/stdc++.h" using namespace std; signed main () { int n; cin >> n; vector <int> g[n], depth(n), parent(n); map <pair <int,int>,int> edge; function <void(int,int)> dfs=[&](int v, int p) { if(p == -1) depth[v] = 0; else depth[v] = depth[p] + 1; parent[v] = p; for(auto u : g[v]) { if(p == u) continue; dfs(u, v); } }; for(int i = 0; i < n - 1; i ++) { int u, v; cin >> u >> v; u --, v --; g[u].push_back(v); g[v].push_back(u); edge[{u, v}] = 0; } dfs(0, -1); int k; cin >> k; vector <vector <int>> found; while(k --) { char ch; cin >> ch; assert(ch == 'M'); int u, v, w; cin >> u >> v >> w; u --, v --; found.push_back({u, v, w}); } sort(found.begin(), found.end(), [](vector <int> a, vector <int> b){ return abs(a[0]-a[1]) < abs(b[0]-b[1]); }); for(auto i : found) { int u = i[0], v = i[1], w = i[2]; if(depth[u] > depth[v]) swap(u, v); edge[{parent[v], v}] = w; } for(auto i : edge) { cout << i.first.first + 1 << " " << i.first.second + 1 << " " << i.second << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...