#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 70000 + 20;
int E1[maxn], E2[maxn], W[maxn], c[maxn];
vector<int> t[maxn], g[maxn];
set<pair<int,int>> mn[maxn], mx[maxn];
char Qtype[maxn];
int Qv[maxn], Qu[maxn], Qz[maxn], p[maxn];
bool visited[maxn];
int match[maxn], fmatch[maxn];
bool dfs(int v){
if (visited[v])
return 0;
visited[v] = 1;
for (auto u : g[v]){
if (match[u] == -1 or dfs(match[u])){
match[u] = v;
return true;
}
}
return false;
}
void dfssack(int v, int par = -1){
p[v] = par;
for (auto u : t[v]){
if (u == par)
continue;
dfssack(u,v);
if (mx[c[u]].size() + mn[c[u]].size() > mx[c[v]].size() + mn[c[u]].size())
swap(c[v],c[u]);
for (auto it : mn[c[u]]){
if (mn[c[v]].find(it) != mn[c[v]].end())
mn[c[v]].erase(it);
else
mn[c[v]].insert(it);
}
for (auto it : mx[c[u]]){
if (mx[c[v]].find(it) != mx[c[v]].end())
mx[c[v]].erase(it);
else
mx[c[v]].insert(it);
}
mn[c[u]].clear(), mx[c[u]].clear();
}
if (!mx[c[v]].empty()){
int idx = (*mx[c[v]].begin()).second;
g[v].push_back(idx);
W[v] = Qz[idx];
}
if (!mn[c[v]].empty()){
int idx = (*mn[c[v]].begin()).second;
g[v].push_back(idx);
W[v] = Qz[idx];
}
}
int main(){
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1; i <= n-1; i++){
cin >> E1[i] >> E2[i];
t[E1[i]].push_back(E2[i]);
t[E2[i]].push_back(E1[i]);
}
for (int i = 1; i <= n; i++)
c[i] = i;
int q;
cin >> q;
for (int i = 1; i <= q; i++){
cin >> Qtype[i] >> Qv[i] >> Qu[i] >> Qz[i];
if (Qtype[i] == 'm'){
mn[c[Qv[i]]].insert({-Qz[i],i});
mn[c[Qu[i]]].insert({-Qz[i],i});
}
else{
mx[c[Qv[i]]].insert({+Qz[i],i});
mx[c[Qu[i]]].insert({+Qz[i],i});
}
}
dfssack(1);
int maxMatch = 0;
memset(match, -1, sizeof match);
for (int i = 2; i <= n; i++){
memset(visited, 0, sizeof visited);
maxMatch += dfs(i);
}
for (int i = 1; i <= q; i++)
W[match[i]] = Qz[i];
for (int i = 2; i <= n; i++)
cout << p[i] << " " << i << " " << W[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
10624 KB |
Output is correct |
2 |
Correct |
6 ms |
10624 KB |
Output is correct |
3 |
Correct |
6 ms |
10624 KB |
Output is correct |
4 |
Correct |
8 ms |
10624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
27512 KB |
Output is correct |
2 |
Correct |
333 ms |
23032 KB |
Output is correct |
3 |
Correct |
324 ms |
27384 KB |
Output is correct |
4 |
Correct |
328 ms |
31096 KB |
Output is correct |
5 |
Correct |
340 ms |
27020 KB |
Output is correct |
6 |
Correct |
335 ms |
26232 KB |
Output is correct |
7 |
Correct |
318 ms |
25336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
246 ms |
18168 KB |
Output is correct |
2 |
Correct |
243 ms |
18300 KB |
Output is correct |
3 |
Correct |
256 ms |
22068 KB |
Output is correct |
4 |
Correct |
253 ms |
24056 KB |
Output is correct |
5 |
Correct |
271 ms |
19448 KB |
Output is correct |
6 |
Correct |
269 ms |
19960 KB |
Output is correct |
7 |
Correct |
248 ms |
18552 KB |
Output is correct |
8 |
Correct |
261 ms |
18424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
10624 KB |
Output is correct |
2 |
Correct |
6 ms |
10624 KB |
Output is correct |
3 |
Correct |
6 ms |
10624 KB |
Output is correct |
4 |
Correct |
8 ms |
10624 KB |
Output is correct |
5 |
Correct |
322 ms |
27512 KB |
Output is correct |
6 |
Correct |
333 ms |
23032 KB |
Output is correct |
7 |
Correct |
324 ms |
27384 KB |
Output is correct |
8 |
Correct |
328 ms |
31096 KB |
Output is correct |
9 |
Correct |
340 ms |
27020 KB |
Output is correct |
10 |
Correct |
335 ms |
26232 KB |
Output is correct |
11 |
Correct |
318 ms |
25336 KB |
Output is correct |
12 |
Correct |
246 ms |
18168 KB |
Output is correct |
13 |
Correct |
243 ms |
18300 KB |
Output is correct |
14 |
Correct |
256 ms |
22068 KB |
Output is correct |
15 |
Correct |
253 ms |
24056 KB |
Output is correct |
16 |
Correct |
271 ms |
19448 KB |
Output is correct |
17 |
Correct |
269 ms |
19960 KB |
Output is correct |
18 |
Correct |
248 ms |
18552 KB |
Output is correct |
19 |
Correct |
261 ms |
18424 KB |
Output is correct |
20 |
Correct |
354 ms |
25592 KB |
Output is correct |
21 |
Correct |
371 ms |
24740 KB |
Output is correct |
22 |
Correct |
397 ms |
26488 KB |
Output is correct |
23 |
Correct |
351 ms |
24952 KB |
Output is correct |
24 |
Correct |
328 ms |
30072 KB |
Output is correct |
25 |
Correct |
343 ms |
32504 KB |
Output is correct |
26 |
Correct |
342 ms |
31888 KB |
Output is correct |
27 |
Correct |
435 ms |
29688 KB |
Output is correct |
28 |
Correct |
329 ms |
26104 KB |
Output is correct |
29 |
Correct |
340 ms |
26232 KB |
Output is correct |
30 |
Correct |
340 ms |
24568 KB |
Output is correct |
31 |
Correct |
333 ms |
24568 KB |
Output is correct |
32 |
Correct |
343 ms |
25596 KB |
Output is correct |
33 |
Correct |
410 ms |
25288 KB |
Output is correct |
34 |
Correct |
376 ms |
25336 KB |
Output is correct |
35 |
Correct |
336 ms |
24056 KB |
Output is correct |