#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define ll long long
#define pp pair<int,int>
const int N = 7e4 + 4;
const int mod = 1e9 + 7;
vector < pp > V[N],A[N],B[N];
unordered_map < int , vector<pp> > M;
unordered_map < int , int > H,W;
int ans[N],U[N],sz[N],T[N],Mn[N],Mx[N],C;
void f(int x, int p, int n){
int h=1; sz[x] = 1;
for(auto y : V[x]){
if(U[y.st] || y.st == p) continue;
f(y.st,x,n);
sz[x] += sz[y.st];
if(sz[y.st] > n/2) h=0;
}
if(h && n-sz[x] <= n/2) C=x;
}
void g(int x, int p, int roo){
T[x] = roo;
for(auto y : V[x]){
if(U[y.st] || y.st == p) continue;
g(y.st,x,roo);
}
}
pp h(int x, int p, int i){
sz[x] = 1;
int mn = 0;
for(auto t : A[x]) if(T[t.st] && T[t.st] != T[x]) mn = max(mn , t.nd);
int mx = mod;
for(auto t : B[x]) if(T[t.st] && T[t.st] != T[x]) mx = min(mx , t.nd);
pp a;
for(auto y : V[x]){
if(U[y.st] || y.st == p) continue;
a = h(y.st,x,y.nd);
mn = max(mn , a.st);
mx = min(mx , a.nd);
sz[x] += sz[y.st];
}
Mn[i] = max(Mn[i] , mn);
Mx[i] = min(Mx[i] , mx);
return mp(mn,mx);
}
void slv(int x, int n){
f(x,-1,n);
x = C;
U[x] = 1;
T[x] = -1;
for(auto y : V[x]){
if(U[y.st]) continue;
g(y.st,x,y.st);
}
h(x,-1,0);
g(x,-1,0);
for(auto y : V[x]) if(!U[y.st]) slv(y.st,sz[y.st]);
}
void ff(int x){
H[x] = 1;
for(auto y : M[x]){
if(H[y.st]) continue;
ans[y.nd] = y.st;
ff(y.st);
}
}
void gg(int x, int p){
H[x] = 1;
for(auto y : M[x]){
if(y.nd == p) continue;
if(!H[y.st]) { ans[y.nd] = x; gg(y.st,y.nd); }
else if(!ans[y.nd]) ans[y.nd] = x;
}
}
int n,k,i,x,y,z,uu[N],vv[N],Z[N];
char c;
int main(){
scanf("%d",&n);
for(i=1;i<n;i++){
scanf("%d%d",&x,&y);
V[x].pb(mp(y,i));
V[y].pb(mp(x,i));
Mx[i] = mod;
uu[i] = x; vv[i] = y;
}
scanf("%d",&k);
for(i=1;i<=k;i++){
scanf(" %c%d%d%d",&c,&x,&y,&z);
Z[i] = z;
if(c == 'm') { A[x].pb(mp(y,z)); A[y].pb(mp(x,z)); }
else { B[x].pb(mp(y,z)); B[y].pb(mp(x,z)); }
}
slv(1,n);
for(i=1;i<n;i++)
if(Mn[i] != 0 || Mx[i] != mod){
M[ Mn[i] ].pb(mp(Mx[i],i));
M[ Mx[i] ].pb(mp(Mn[i],i));
W[ Mn[i] ]++; W[ Mx[i] ]++;
}
ff(0); ff(mod);
for(i=1;i<=k;i++){
x = Z[i];
if(H[x]) continue;
for(; W[x] == 1 ; x = z) {
W[x]--;
H[x] = 1;
z = -1;
for(auto y : M[x]){
W[y.st]--;
if(W[y.st] > 0){
ans[y.nd] = x;
z = y.st;
}
}
}
}
for(i=1;i<=k;i++)
if(!H[ Z[i] ])
gg(Z[i],0);
for(i=1;i<n;i++)
printf("%d %d %d\n", uu[i], vv[i], ans[i] ? ans[i] : Mx[i]);
return 0;
}
Compilation message
minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
minmaxtree.cpp:88:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&x,&y);
~~~~~^~~~~~~~~~~~~~
minmaxtree.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&k);
~~~~~^~~~~~~~~
minmaxtree.cpp:96:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c%d%d%d",&c,&x,&y,&z);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5352 KB |
Output is correct |
3 |
Correct |
6 ms |
5512 KB |
Output is correct |
4 |
Correct |
6 ms |
5512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
608 ms |
30388 KB |
Output is correct |
2 |
Correct |
423 ms |
30388 KB |
Output is correct |
3 |
Correct |
559 ms |
30388 KB |
Output is correct |
4 |
Correct |
595 ms |
30980 KB |
Output is correct |
5 |
Correct |
674 ms |
30980 KB |
Output is correct |
6 |
Correct |
634 ms |
30980 KB |
Output is correct |
7 |
Correct |
476 ms |
30980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
236 ms |
30980 KB |
Output is correct |
2 |
Correct |
250 ms |
30980 KB |
Output is correct |
3 |
Correct |
506 ms |
30980 KB |
Output is correct |
4 |
Correct |
524 ms |
30980 KB |
Output is correct |
5 |
Correct |
479 ms |
30980 KB |
Output is correct |
6 |
Correct |
451 ms |
30980 KB |
Output is correct |
7 |
Correct |
267 ms |
30980 KB |
Output is correct |
8 |
Correct |
314 ms |
30980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5352 KB |
Output is correct |
3 |
Correct |
6 ms |
5512 KB |
Output is correct |
4 |
Correct |
6 ms |
5512 KB |
Output is correct |
5 |
Correct |
608 ms |
30388 KB |
Output is correct |
6 |
Correct |
423 ms |
30388 KB |
Output is correct |
7 |
Correct |
559 ms |
30388 KB |
Output is correct |
8 |
Correct |
595 ms |
30980 KB |
Output is correct |
9 |
Correct |
674 ms |
30980 KB |
Output is correct |
10 |
Correct |
634 ms |
30980 KB |
Output is correct |
11 |
Correct |
476 ms |
30980 KB |
Output is correct |
12 |
Correct |
236 ms |
30980 KB |
Output is correct |
13 |
Correct |
250 ms |
30980 KB |
Output is correct |
14 |
Correct |
506 ms |
30980 KB |
Output is correct |
15 |
Correct |
524 ms |
30980 KB |
Output is correct |
16 |
Correct |
479 ms |
30980 KB |
Output is correct |
17 |
Correct |
451 ms |
30980 KB |
Output is correct |
18 |
Correct |
267 ms |
30980 KB |
Output is correct |
19 |
Correct |
314 ms |
30980 KB |
Output is correct |
20 |
Correct |
466 ms |
30980 KB |
Output is correct |
21 |
Correct |
395 ms |
30980 KB |
Output is correct |
22 |
Correct |
392 ms |
32312 KB |
Output is correct |
23 |
Correct |
334 ms |
34160 KB |
Output is correct |
24 |
Correct |
760 ms |
41948 KB |
Output is correct |
25 |
Correct |
733 ms |
46032 KB |
Output is correct |
26 |
Correct |
697 ms |
46724 KB |
Output is correct |
27 |
Correct |
707 ms |
48596 KB |
Output is correct |
28 |
Correct |
807 ms |
48836 KB |
Output is correct |
29 |
Correct |
656 ms |
51288 KB |
Output is correct |
30 |
Correct |
535 ms |
51288 KB |
Output is correct |
31 |
Correct |
528 ms |
53416 KB |
Output is correct |
32 |
Correct |
484 ms |
57348 KB |
Output is correct |
33 |
Correct |
390 ms |
58380 KB |
Output is correct |
34 |
Correct |
396 ms |
60604 KB |
Output is correct |
35 |
Correct |
375 ms |
61996 KB |
Output is correct |