#include <bits/stdc++.h>
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define f first
#define s second
using namespace std;
using ll = long long;
using pii = pair <int, int>;
const int N = 2e5 + 5, inf = 1e9;
int n, sz[N], d[N], ind[N], par[N], ct, tp[N], l[N], r[N], node[N];
vector <int> g[N], gg[N], add[N][2], del[N][2];
char C[N];
int U[N], V[N], Z[N], used[N], timer, Mt[N];
void calc(int v, int p = 0) {
sz[v] = 1;
par[v] = p;
for (auto to : g[v]) {
if (to != p) {
d[to] = d[v] + 1;
calc(to, v);
sz[v] += sz[to];
}
}
}
void dfs(int v, int p = 0, int top = 1) {
ind[v] = ++ct;
node[ind[v]] = v;
tp[v] = top;
int mx = 0, bg = -1;
for (auto to : g[v]) {
if (to != p && mx < sz[to])
mx = sz[to], bg = to;
}
if (bg != -1)
dfs(bg, v, top);
for (auto to : g[v]) {
if (to != p && to != bg)
dfs(to, v, to);
}
}
bool kuhn(int v) {
if (used[v] == timer)
return 0;
used[v] = timer;
for (auto to : gg[v]) {
if (Mt[to] == -1) {
Mt[to] = v;
return 1;
}
}
for (auto to : gg[v]) {
if (kuhn(Mt[to])) {
Mt[to] = v;
return 1;
}
}
return 0;
}
int main() {
ios :: sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
calc(1);
dfs(1);
int q;
cin >> q;
bool lol = 0;
for (int i = 0; i < q; ++i) {
char c;
int u, v, z;
cin >> c >> u >> v >> z;
lol |= (c == 'm');
C[i] = c;
U[i] = u;
V[i] = v;
Z[i] = z;
int mn = inf;
vector <pii> seg;
while (tp[u] != tp[v]) {
if (d[tp[u]] < d[tp[v]]) swap(u, v);
seg.pb({ind[tp[u]], ind[u]});
mn = min(mn, seg.back().f);
u = par[tp[u]];
}
if (d[u] > d[v]) swap(u, v);
seg.pb({ind[u], ind[v]});
mn = min(mn, seg.back().f);
for (auto [l, r] : seg) {
if (l == mn)
++l;
add[l][(c == 'm')].pb(z);
del[r + 1][(c == 'm')].pb(z);
}
}
multiset <int> mt[2];
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < 2; ++j) {
for (auto z : add[i][j])
mt[j].insert(z);
for (auto z : del[i][j])
mt[j].erase(mt[j].find(z));
}
r[i] = inf + 1;
l[i] = -1;
if (sz(mt[0]))
r[i] = *mt[0].begin();
if (sz(mt[1]))
l[i] = *mt[1].rbegin();
}
map <int, int> idx;
for (int i = 0; i < q; ++i) {
char c;
int u, v, z;
c = C[i];
u = U[i];
v = V[i];
z = Z[i];
idx[z] = i;
int mn = inf;
vector <pii> seg;
while (tp[u] != tp[v]) {
if (d[tp[u]] < d[tp[v]]) swap(u, v);
seg.pb({ind[tp[u]], ind[u]});
mn = min(mn, seg.back().f);
u = par[tp[u]];
}
if (d[u] > d[v]) swap(u, v);
seg.pb({ind[u], ind[v]});
mn = min(mn, seg.back().f);
}
for (int i = 1; i <= n; ++i) {
if (!par[i])
continue;
if (idx.count(l[ind[i]]))
gg[idx[l[ind[i]]]].pb(ind[i] + q);
if (idx.count(r[ind[i]]))
gg[idx[r[ind[i]]]].pb(ind[i] + q);
}
for (int i = 1; i <= n; ++i)
Mt[i + q] = -1;
for (int i = 0; i < q; ++i) {
++timer;
kuhn(i);
}
for (int i = 1; i <= n; ++i) {
if (!par[i])
continue;
if (Mt[ind[i] + q] == -1)
cout << i << ' ' << par[i] << ' ' << max(0, l[ind[i]]) << '\n';
else
cout << i << ' ' << par[i] << ' ' << Z[Mt[ind[i] + q]] << '\n';
}
return 0;
}
Compilation message
minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:125:8: warning: variable 'c' set but not used [-Wunused-but-set-variable]
125 | char c;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
28496 KB |
Output is correct |
2 |
Correct |
21 ms |
28712 KB |
Output is correct |
3 |
Correct |
27 ms |
28644 KB |
Output is correct |
4 |
Correct |
22 ms |
28496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
49736 KB |
Output is correct |
2 |
Correct |
228 ms |
46920 KB |
Output is correct |
3 |
Correct |
190 ms |
46896 KB |
Output is correct |
4 |
Correct |
171 ms |
49504 KB |
Output is correct |
5 |
Correct |
206 ms |
47944 KB |
Output is correct |
6 |
Correct |
194 ms |
48160 KB |
Output is correct |
7 |
Correct |
195 ms |
47732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
41116 KB |
Output is correct |
2 |
Correct |
126 ms |
41544 KB |
Output is correct |
3 |
Correct |
119 ms |
41716 KB |
Output is correct |
4 |
Correct |
107 ms |
42804 KB |
Output is correct |
5 |
Correct |
141 ms |
41728 KB |
Output is correct |
6 |
Correct |
128 ms |
42568 KB |
Output is correct |
7 |
Correct |
143 ms |
41620 KB |
Output is correct |
8 |
Correct |
123 ms |
41288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
28496 KB |
Output is correct |
2 |
Correct |
21 ms |
28712 KB |
Output is correct |
3 |
Correct |
27 ms |
28644 KB |
Output is correct |
4 |
Correct |
22 ms |
28496 KB |
Output is correct |
5 |
Correct |
199 ms |
49736 KB |
Output is correct |
6 |
Correct |
228 ms |
46920 KB |
Output is correct |
7 |
Correct |
190 ms |
46896 KB |
Output is correct |
8 |
Correct |
171 ms |
49504 KB |
Output is correct |
9 |
Correct |
206 ms |
47944 KB |
Output is correct |
10 |
Correct |
194 ms |
48160 KB |
Output is correct |
11 |
Correct |
195 ms |
47732 KB |
Output is correct |
12 |
Correct |
136 ms |
41116 KB |
Output is correct |
13 |
Correct |
126 ms |
41544 KB |
Output is correct |
14 |
Correct |
119 ms |
41716 KB |
Output is correct |
15 |
Correct |
107 ms |
42804 KB |
Output is correct |
16 |
Correct |
141 ms |
41728 KB |
Output is correct |
17 |
Correct |
128 ms |
42568 KB |
Output is correct |
18 |
Correct |
143 ms |
41620 KB |
Output is correct |
19 |
Correct |
123 ms |
41288 KB |
Output is correct |
20 |
Correct |
281 ms |
51148 KB |
Output is correct |
21 |
Correct |
272 ms |
48524 KB |
Output is correct |
22 |
Correct |
249 ms |
48500 KB |
Output is correct |
23 |
Correct |
230 ms |
48704 KB |
Output is correct |
24 |
Correct |
179 ms |
50760 KB |
Output is correct |
25 |
Correct |
193 ms |
52552 KB |
Output is correct |
26 |
Correct |
194 ms |
50396 KB |
Output is correct |
27 |
Correct |
220 ms |
49756 KB |
Output is correct |
28 |
Correct |
244 ms |
49508 KB |
Output is correct |
29 |
Correct |
261 ms |
49484 KB |
Output is correct |
30 |
Correct |
221 ms |
47224 KB |
Output is correct |
31 |
Correct |
215 ms |
47440 KB |
Output is correct |
32 |
Correct |
224 ms |
49484 KB |
Output is correct |
33 |
Correct |
232 ms |
48184 KB |
Output is correct |
34 |
Correct |
219 ms |
48196 KB |
Output is correct |
35 |
Correct |
206 ms |
47092 KB |
Output is correct |