#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++)
{
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();
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13144 KB |
Output is correct |
2 |
Correct |
3 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13144 KB |
Output is correct |
4 |
Correct |
3 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
28424 KB |
Output is correct |
2 |
Correct |
121 ms |
30036 KB |
Output is correct |
3 |
Correct |
110 ms |
27652 KB |
Output is correct |
4 |
Correct |
85 ms |
28496 KB |
Output is correct |
5 |
Correct |
97 ms |
28328 KB |
Output is correct |
6 |
Correct |
82 ms |
26196 KB |
Output is correct |
7 |
Correct |
77 ms |
25480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
22100 KB |
Output is correct |
2 |
Correct |
55 ms |
22212 KB |
Output is correct |
3 |
Correct |
51 ms |
22864 KB |
Output is correct |
4 |
Correct |
53 ms |
24312 KB |
Output is correct |
5 |
Correct |
59 ms |
22444 KB |
Output is correct |
6 |
Correct |
62 ms |
23120 KB |
Output is correct |
7 |
Correct |
61 ms |
22100 KB |
Output is correct |
8 |
Correct |
58 ms |
21840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13144 KB |
Output is correct |
2 |
Correct |
3 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13144 KB |
Output is correct |
4 |
Correct |
3 ms |
13148 KB |
Output is correct |
5 |
Correct |
85 ms |
28424 KB |
Output is correct |
6 |
Correct |
121 ms |
30036 KB |
Output is correct |
7 |
Correct |
110 ms |
27652 KB |
Output is correct |
8 |
Correct |
85 ms |
28496 KB |
Output is correct |
9 |
Correct |
97 ms |
28328 KB |
Output is correct |
10 |
Correct |
82 ms |
26196 KB |
Output is correct |
11 |
Correct |
77 ms |
25480 KB |
Output is correct |
12 |
Correct |
58 ms |
22100 KB |
Output is correct |
13 |
Correct |
55 ms |
22212 KB |
Output is correct |
14 |
Correct |
51 ms |
22864 KB |
Output is correct |
15 |
Correct |
53 ms |
24312 KB |
Output is correct |
16 |
Correct |
59 ms |
22444 KB |
Output is correct |
17 |
Correct |
62 ms |
23120 KB |
Output is correct |
18 |
Correct |
61 ms |
22100 KB |
Output is correct |
19 |
Correct |
58 ms |
21840 KB |
Output is correct |
20 |
Correct |
131 ms |
30544 KB |
Output is correct |
21 |
Correct |
154 ms |
31080 KB |
Output is correct |
22 |
Correct |
153 ms |
30808 KB |
Output is correct |
23 |
Correct |
136 ms |
30332 KB |
Output is correct |
24 |
Correct |
87 ms |
28244 KB |
Output is correct |
25 |
Correct |
95 ms |
30136 KB |
Output is correct |
26 |
Correct |
92 ms |
28884 KB |
Output is correct |
27 |
Correct |
97 ms |
28108 KB |
Output is correct |
28 |
Correct |
94 ms |
26960 KB |
Output is correct |
29 |
Correct |
101 ms |
27732 KB |
Output is correct |
30 |
Correct |
109 ms |
27220 KB |
Output is correct |
31 |
Correct |
107 ms |
27728 KB |
Output is correct |
32 |
Correct |
99 ms |
26960 KB |
Output is correct |
33 |
Correct |
120 ms |
28496 KB |
Output is correct |
34 |
Correct |
116 ms |
27984 KB |
Output is correct |
35 |
Correct |
121 ms |
26816 KB |
Output is correct |