#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e5+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e13;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=1000696969;
const ll p=70032301;
const ull p2=913;
const int L=18;
int t[N],match[(N<<1)],pod[N],val[N],oj[N],ans[N];
pair<int,int>przedzial[N],w[N];
vi g[N],graf[N],graf2[(N<<1)];
bool vis[(N<<1)];
unordered_map<int,int>num;
void add(int x,set<int>* s){
auto it=s->lower_bound(x);
if(it!=(s->end()) and *it==x) (*s).erase(x);
else (*s).insert(x);
}
set<int>* dfs(int v,int o,int p){
oj[v]=o;
pod[v]=1;
vector<set<int>* >curr;
int maxi=0,byl=0;
for(auto u:graf[v]){
if(u==o) continue;
set<int>* a=dfs(u,v,p);
curr.push_back(a);
pod[v]+=pod[u];
maxi=max(maxi,pod[u]);
}
set<int>* pom;
int k=-1;
for(auto u:graf[v]){
if(u==o) continue;
k++;
if(maxi==pod[u] and !byl){
byl=1;
pom=curr[k];
}
}
byl=0,k=-1;
for(auto u:graf[v]){
if(u==o) continue;
k++;
if(maxi==pod[u] and !byl){
byl=1;
continue;
}
for(auto x:(*curr[k])) add(x,pom);
}
if(maxi==0) pom=new set<int>;
for(auto u:g[v]) add(u,pom);
if(p){
if(pom->size()){
auto it=pom->end();
it--;
przedzial[v].fi=*it;
}else przedzial[v].fi=-1;
}else{
if(pom->size()) przedzial[v].se=*(pom->begin());
else przedzial[v].se=-1;
}
return pom;
}
bool dfs2(int v){
vis[v]=1;
for(auto u:graf2[v]){
if(!match[u]){
match[u]=v,match[v]=u;
return 1;
}
}
for(auto u:graf2[v]){
if(!vis[match[u]] and dfs2(match[u])){
match[v]=u,match[u]=v;
return 1;
}
}
return 0;
}
void solve(){
int n;
cin>>n;
for(int a,b,i=1;i<n;i++){
cin>>a>>b;
graf[a].push_back(b),graf[b].push_back(a);
}
int m;
cin>>m;
for(int a,b,c,i=1;i<=m;i++){
char xd;
cin>>xd>>a>>b>>c;
num[c]=i;
if(xd=='m') g[a].push_back(c),g[b].push_back(c);
else a*=-1;
w[i]={a,b},val[i]=c;
}
dfs(1,1,1);
for(int i=1;i<=n;i++) g[i].clear();
for(int i=1;i<=m;i++){
if(w[i].fi<0) g[w[i].fi*(-1)].push_back(val[i]),g[w[i].se].push_back(val[i]);
}
dfs(1,1,0);
//for(int i=1;i<=n;i++) cout<<przedzial[i].fi<<" "<<przedzial[i].se<<"\n";
for(int i=2;i<=n;i++){
if(przedzial[i].fi!=-1) graf2[i].push_back(num[przedzial[i].fi]+n),
graf2[num[przedzial[i].fi]+n].push_back(i);
if(przedzial[i].se!=-1) graf2[i].push_back(num[przedzial[i].se]+n),
graf2[num[przedzial[i].se]+n].push_back(i);
}
while(true){
bool b=0;
for(int i=2;i<=n;i++)
if(!vis[i] and !match[i] and dfs2(i)) b=1;
for(int i=1;i<=n+m;i++) vis[i]=0;
if(!b) break;
}
for(int i=2;i<=n;i++){
if(przedzial[i].fi==-1 and przedzial[i].se==-1) ans[i]=0;
else{
if(przedzial[i].fi!=-1) ans[i]=przedzial[i].fi;
else ans[i]=przedzial[i].se;
}
}
for(int i=1;i<=m;i++) ans[match[i+n]]=val[i];
for(int i=2;i<=n;i++) cout<<i<<" "<<oj[i]<<" "<<ans[i]<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int tt=1;
while(tt--) solve();
}
Compilation message
minmaxtree.cpp: In function 'std::set<int>* dfs(int, int, int)':
minmaxtree.cpp:60:35: warning: 'pom' may be used uninitialized in this function [-Wmaybe-uninitialized]
60 | for(auto x:(*curr[k])) add(x,pom);
| ~~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
34976 KB |
Output is correct |
2 |
Correct |
174 ms |
43720 KB |
Output is correct |
3 |
Correct |
183 ms |
36688 KB |
Output is correct |
4 |
Correct |
160 ms |
36332 KB |
Output is correct |
5 |
Correct |
166 ms |
37128 KB |
Output is correct |
6 |
Correct |
149 ms |
34136 KB |
Output is correct |
7 |
Correct |
143 ms |
32220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
28440 KB |
Output is correct |
2 |
Correct |
113 ms |
28644 KB |
Output is correct |
3 |
Correct |
97 ms |
25172 KB |
Output is correct |
4 |
Correct |
94 ms |
28120 KB |
Output is correct |
5 |
Correct |
110 ms |
25620 KB |
Output is correct |
6 |
Correct |
112 ms |
26636 KB |
Output is correct |
7 |
Correct |
110 ms |
25056 KB |
Output is correct |
8 |
Correct |
106 ms |
24884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
153 ms |
34976 KB |
Output is correct |
6 |
Correct |
174 ms |
43720 KB |
Output is correct |
7 |
Correct |
183 ms |
36688 KB |
Output is correct |
8 |
Correct |
160 ms |
36332 KB |
Output is correct |
9 |
Correct |
166 ms |
37128 KB |
Output is correct |
10 |
Correct |
149 ms |
34136 KB |
Output is correct |
11 |
Correct |
143 ms |
32220 KB |
Output is correct |
12 |
Correct |
104 ms |
28440 KB |
Output is correct |
13 |
Correct |
113 ms |
28644 KB |
Output is correct |
14 |
Correct |
97 ms |
25172 KB |
Output is correct |
15 |
Correct |
94 ms |
28120 KB |
Output is correct |
16 |
Correct |
110 ms |
25620 KB |
Output is correct |
17 |
Correct |
112 ms |
26636 KB |
Output is correct |
18 |
Correct |
110 ms |
25056 KB |
Output is correct |
19 |
Correct |
106 ms |
24884 KB |
Output is correct |
20 |
Correct |
198 ms |
44872 KB |
Output is correct |
21 |
Correct |
201 ms |
45568 KB |
Output is correct |
22 |
Correct |
197 ms |
45020 KB |
Output is correct |
23 |
Correct |
193 ms |
43988 KB |
Output is correct |
24 |
Correct |
173 ms |
34668 KB |
Output is correct |
25 |
Correct |
183 ms |
38592 KB |
Output is correct |
26 |
Correct |
183 ms |
37076 KB |
Output is correct |
27 |
Correct |
193 ms |
34752 KB |
Output is correct |
28 |
Correct |
176 ms |
35164 KB |
Output is correct |
29 |
Correct |
179 ms |
36124 KB |
Output is correct |
30 |
Correct |
176 ms |
35108 KB |
Output is correct |
31 |
Correct |
212 ms |
35560 KB |
Output is correct |
32 |
Correct |
187 ms |
36228 KB |
Output is correct |
33 |
Correct |
198 ms |
39640 KB |
Output is correct |
34 |
Correct |
208 ms |
38876 KB |
Output is correct |
35 |
Correct |
188 ms |
36012 KB |
Output is correct |