#include<bits/stdc++.h>
using namespace std;
int n,k;
vector<int> con_cuplaj[140005];
int tole[140005],tori[140005];
void add_edge(int xle, int xri)
{
con_cuplaj[xle].push_back(xri);
con_cuplaj[xri].push_back(xle);
}
int visited[140005],pas;
bool cupleaza(int nod)
{
if(visited[nod]==pas)
return 0;
visited[nod]=pas;
for(auto x:con_cuplaj[nod])
{
if(tole[x]==0)
{
tori[nod]=x;
tole[x]=nod;
return 1;
}
}
for(auto x:con_cuplaj[nod])
{
if(cupleaza(tole[x]))
{
tori[nod]=x;
tole[x]=nod;
return 1;
}
}
return 0;
}
void cuplaj()
{
while(1)
{
pas++;
bool bl=0;
for(int i=1;i<=k;i++)
if(tori[i]==0)
bl |= cupleaza(i);
if(!bl)
break;
}
}
vector<int> con[70005];
pair<int,int> lim[70005];
pair<int,int> care[70005];
int val[70005];
vector<pair<pair<int,int>,int>> baga[70005];
vector<pair<pair<int,int>,int>> scoate[70005];
int parent[70005];
int depth[70005];
int head[70005];
int pos[70005],curpos;
int siz[70005];
int ord[70005];
void dfs_init(int nod)
{
siz[nod]=1;
for(auto adj:con[nod])
{
if(adj!=parent[nod])
{
parent[adj] = nod;
depth[adj] = depth[nod]+1;
dfs_init(adj);
siz[nod] += siz[adj];
}
}
}
void decomp(int nod, int chead)
{
pos[nod]=++curpos;
ord[pos[nod]]=nod;
head[nod]=chead;
int maxc=-1,heavy=-1;
for(auto adj:con[nod])
{
if(adj!=parent[nod] && siz[adj]>maxc)
{
maxc=siz[adj];
heavy=adj;
}
}
if(heavy!=-1)
decomp(heavy,chead);
for(auto adj:con[nod])
if(adj!=parent[nod] && adj!=heavy)
decomp(adj,adj);
}
void add_val(int le, int ri, pair<pair<int,int>,int> newv)
{
if(le>ri)
return;
baga[le].push_back(newv);
scoate[ri+1].push_back(newv);
}
void add_hld(int a, int b, pair<pair<int,int>,int> newv)
{
for(;head[a]!=head[b];b=parent[head[b]])
{
if(depth[head[a]]>depth[head[b]])
swap(a,b);
add_val(pos[head[b]],pos[b],newv);
}
if(pos[a]>pos[b])
swap(a,b);
add_val(pos[a]+1,pos[b],newv);
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
int a,b,c;
for(int i=1;i<n;i++)
{
cin>>a>>b;
con[a].push_back(b);
con[b].push_back(a);
}
dfs_init(1);
decomp(1,1);
cin>>k;
char ch;
for(int i=1;i<=k;i++)
{
cin>>ch>>a>>b>>c;
val[i]=c;
if(ch=='M')
{
add_hld(a,b,{{c,i},1});
}
else
{
add_hld(a,b,{{c,i},0});
}
}
set<pair<int,int>> s[2];
for(int i=1;i<=n;i++)
{
for(auto x:scoate[i]) s[x.second].erase({x.first});
for(auto x:baga[i]) s[x.second].insert({x.first});
if(s[0].empty())
{
lim[ord[i]].first = -1;
care[ord[i]].first = -1;
}
else
{
lim[ord[i]].first = (*prev(s[0].end())).first;
care[ord[i]].first = (*prev(s[0].end())).second;
}
if(s[1].empty())
{
lim[ord[i]].second = -1;
care[ord[i]].second = -1;
}
else
{
lim[ord[i]].second = (*s[1].begin()).first;
care[ord[i]].second = (*s[1].begin()).second;
}
}
for(int i=1;i<=n;i++)
{
//cout<<i<<" "<<lim[i].first<<" "<<care[i].first<<" limita inferioara\n";
//cout<<i<<" "<<lim[i].second<<" "<<care[i].second<<" limita superioara\n\n";
if(care[i].first != -1) add_edge(care[i].first, k+i);
if(care[i].second != -1) add_edge(care[i].second, k+i);
}
cuplaj();
int cate=0;
for(int i=1;i<=k;i++)
{
if(tori[i]==0)
{
while(1);
}
}
for(int i=2;i<=n;i++)
{
cout<<i<<" "<<parent[i]<<" ";
if(tole[k+i]==0)
{
if(lim[i].first!=-1) cout<<lim[i].first<<"\n";
else if(lim[i].second!=-1) cout<<lim[i].second<<"\n";
else cout<<1<<"\n";
}
else
{
cout<<val[tole[k+i]]<<"\n";
}
}
return 0;
}
Compilation message
minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:180:9: warning: unused variable 'cate' [-Wunused-variable]
180 | int cate=0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
13148 KB |
Output is correct |
2 |
Correct |
3 ms |
13300 KB |
Output is correct |
3 |
Correct |
3 ms |
13148 KB |
Output is correct |
4 |
Correct |
3 ms |
13148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
28240 KB |
Output is correct |
2 |
Correct |
115 ms |
32340 KB |
Output is correct |
3 |
Correct |
114 ms |
29700 KB |
Output is correct |
4 |
Correct |
86 ms |
30800 KB |
Output is correct |
5 |
Correct |
104 ms |
30732 KB |
Output is correct |
6 |
Correct |
84 ms |
28752 KB |
Output is correct |
7 |
Correct |
74 ms |
27624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
21888 KB |
Output is correct |
2 |
Correct |
64 ms |
23332 KB |
Output is correct |
3 |
Correct |
52 ms |
23920 KB |
Output is correct |
4 |
Correct |
55 ms |
25548 KB |
Output is correct |
5 |
Correct |
61 ms |
23888 KB |
Output is correct |
6 |
Correct |
65 ms |
24624 KB |
Output is correct |
7 |
Correct |
66 ms |
23620 KB |
Output is correct |
8 |
Correct |
59 ms |
23380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
13148 KB |
Output is correct |
2 |
Correct |
3 ms |
13300 KB |
Output is correct |
3 |
Correct |
3 ms |
13148 KB |
Output is correct |
4 |
Correct |
3 ms |
13148 KB |
Output is correct |
5 |
Correct |
94 ms |
28240 KB |
Output is correct |
6 |
Correct |
115 ms |
32340 KB |
Output is correct |
7 |
Correct |
114 ms |
29700 KB |
Output is correct |
8 |
Correct |
86 ms |
30800 KB |
Output is correct |
9 |
Correct |
104 ms |
30732 KB |
Output is correct |
10 |
Correct |
84 ms |
28752 KB |
Output is correct |
11 |
Correct |
74 ms |
27624 KB |
Output is correct |
12 |
Correct |
65 ms |
21888 KB |
Output is correct |
13 |
Correct |
64 ms |
23332 KB |
Output is correct |
14 |
Correct |
52 ms |
23920 KB |
Output is correct |
15 |
Correct |
55 ms |
25548 KB |
Output is correct |
16 |
Correct |
61 ms |
23888 KB |
Output is correct |
17 |
Correct |
65 ms |
24624 KB |
Output is correct |
18 |
Correct |
66 ms |
23620 KB |
Output is correct |
19 |
Correct |
59 ms |
23380 KB |
Output is correct |
20 |
Correct |
151 ms |
33080 KB |
Output is correct |
21 |
Correct |
157 ms |
33148 KB |
Output is correct |
22 |
Correct |
146 ms |
32824 KB |
Output is correct |
23 |
Correct |
142 ms |
32328 KB |
Output is correct |
24 |
Correct |
90 ms |
30480 KB |
Output is correct |
25 |
Correct |
93 ms |
32592 KB |
Output is correct |
26 |
Correct |
94 ms |
31120 KB |
Output is correct |
27 |
Correct |
120 ms |
30652 KB |
Output is correct |
28 |
Correct |
102 ms |
29144 KB |
Output is correct |
29 |
Correct |
103 ms |
30032 KB |
Output is correct |
30 |
Correct |
111 ms |
29496 KB |
Output is correct |
31 |
Correct |
125 ms |
29680 KB |
Output is correct |
32 |
Correct |
106 ms |
29492 KB |
Output is correct |
33 |
Correct |
122 ms |
30800 KB |
Output is correct |
34 |
Correct |
131 ms |
30176 KB |
Output is correct |
35 |
Correct |
109 ms |
29136 KB |
Output is correct |