# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
61529 | 2018-07-26T07:05:35 Z | khsoo01 | Min-max tree (BOI18_minmaxtree) | C++11 | 199 ms | 14788 KB |
#include<bits/stdc++.h> using namespace std; typedef pair<int,int> pii; const int N = 70005; int n, m, dep[N], par[N], ans[N]; bool vv[N], ve[N]; vector<int> tr[N], cyc, usd; vector<pii> adj[N]; struct edge { int a, b, x; bool operator < (edge &T) { return x < T.x; } } e[N]; struct coloring { int c[N], p[N]; int Find (int X) { if(p[X] == X) return X; return p[X] = Find(p[X]); } void init () { for(int i=1;i<=n;i++) { c[i] = 0; p[i] = i; } } void color (int A, int B, int C) { while(true) { A = Find(A); B = Find(B); if(dep[A] == dep[B]) return; if(dep[A] < dep[B]) swap(A, B); c[A] = C; p[A] = par[A]; } } } col[2]; void calc (int C, int P) { dep[C] = dep[P] + 1; par[C] = P; for(auto &T : tr[C]) { if(T == P) continue; calc(T, C); } } bool findcyc (int I) { if(vv[I]) return true; vv[I] = true; for(auto &T : adj[I]) { int A, B; tie(A, B) = T; if(ve[B]) continue; ve[B] = true; if(findcyc(A)) { cyc.push_back(I); ans[B] = I; return true; } } usd.push_back(I); return false; } void dfs (int I) { vv[I] = true; for(auto &T : adj[I]) { int A, B; tie(A, B) = T; if(vv[A]) continue; ans[B] = A; dfs(A); } } int main() { scanf("%d",&n); for(int i=1;i<n;i++) { int A, B; scanf("%d%d",&A,&B); tr[A].push_back(B); tr[B].push_back(A); } calc(1, 0); scanf("%d",&m); for(int i=1;i<=m;i++) { char typ[2]; scanf("%s%d%d%d",typ,&e[i].a,&e[i].b,&e[i].x); if(typ[0] == 'm') e[i].x *= -1; } sort(e+1, e+1+m); col[0].init(); col[1].init(); for(int i=1;i<=m;i++) { col[e[i].x > 0].color(e[i].a, e[i].b, i); } for(int i=1;i<=n;i++) { int A = col[0].c[i], B = col[1].c[i]; adj[A].push_back({B, i}); adj[B].push_back({A, i}); } for(int i=0;i<=m;i++) { if(vv[i]) continue; findcyc(i); for(auto &T : usd) { vv[T] = false; } usd.clear(); for(auto &T : cyc) { dfs(T); } cyc.clear(); } for(int i=2;i<=n;i++) { printf("%d %d %d\n", par[i], i, abs(e[ans[i]].x)); } }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 5 ms | 3576 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 199 ms | 14788 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 101 ms | 14788 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 5 ms | 3576 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |