#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000000000000000
#define iii pair<int,pair<int,int> >
#define st first
#define nd second
#define pb push_back
#define umax(x,y) x=max(x,(y))
#define pw(x) (1<<(x))
#define N 70005
#define LOG 21
#define all(x) x.begin(),x.end()
#define sz(x) x.size()
int n,x,y,z,q;
vector<int> v[N],del[N];
multiset<int> add[N];
int xcost[N],ncost[N],baba[LOG+2][N],der[N];
vector<iii> Min,Max;
int lca(int x,int y) {
if(der[x]<der[y]) swap(x,y);
int dx=der[x];
for(int i=LOG-1;i>=0;i--) {
if(dx-pw(i)>=der[y]) {
x=baba[i][x];
dx-=pw(i);
}
}
if(x==y) return x;
for(int i=LOG-1;i>=0;i--) {
if(baba[i][x]!=baba[i][y]) {
x=baba[i][x];
y=baba[i][y];
}
}
return baba[0][x];
}
void dfs3(int node,int ata) {
for(int i:v[node]) {
if(i==ata) continue ;
dfs3(i,node);
if(sz(add[i])>sz(add[node])) swap(add[i],add[node]);
for(auto x:add[i]) add[node].insert(x);
add[i].clear();
}
for(int i:del[node]) {
add[node].erase(add[node].find(i));
add[node].erase(add[node].find(i));
}
if(node==1) return ;
if(sz(add[node])) xcost[node]=*add[node].begin(),ncost[node]=*add[node].rbegin();
else xcost[node]=123,ncost[node]=123;
}
void build_lca() {
for(int i=1;i<LOG;i++) {
for(int j=1;j<=n;j++) {
baba[i][j]=baba[i-1][baba[i-1][j]];
}
}
}
void dfs1(int node,int ata) {
baba[0][node]=ata;
der[node]=der[ata]+1;
for(int i:v[node]) {
if(i==ata) continue ;
dfs1(i,node);
}
}
int main() {
srand(time(NULL));
// freopen("in.txt","r",stdin);
scanf("%d ",&n);
for(int i=1;i<n;i++) {
scanf("%d %d",&x,&y);
v[x].pb(y);
v[y].pb(x);
}
dfs1(1,0);
build_lca();
scanf("%d",&q);
while(q--) {
char c;
scanf(" %c %d %d %d",&c,&x,&y,&z);
if(c=='M') {
Max.pb({z,{x,y}});
}
else {
Min.pb({z,{x,y}});
}
}
sort(all(Max),greater<iii>());
sort(all(Min));
for(auto x:Max) {
int n1=x.nd.st;
int n2=x.nd.nd;
int cst=x.st;
add[n1].insert(cst);
add[n2].insert(cst);
del[lca(n1,n2)].pb(cst);
}
for(auto x:Min) {
int n1=x.nd.st;
int n2=x.nd.nd;
int cst=x.st;
add[n1].insert(cst);
add[n2].insert(cst);
del[lca(n1,n2)].pb(cst);
}
dfs3(1,0);
for(int i=2;i<=n;i++) {
int rnd=rand()%2;
printf("%d %d %d\n",i,baba[0][i],rnd?xcost[i]:ncost[i]);
}
return 0;
assert(0);
}
Compilation message
minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:121:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d ",&n);
~~~~~^~~~~~~~~~
minmaxtree.cpp:125:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&x,&y);
~~~~~^~~~~~~~~~~~~~~
minmaxtree.cpp:135:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
~~~~~^~~~~~~~~
minmaxtree.cpp:141:10: 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 |
Incorrect |
12 ms |
6904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
395 ms |
30980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
237 ms |
30980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
6904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |