#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
using namespace std;
using ll = long long;
const int N = 5e5+5 , inf = 2e9 + 7;
const ll INF = 1e18 , mod = 1e9+7;
int ans[N] , n , mxson[N] , sz[N] , t[N] , m;
vector<int> g[N] , vec[N];
void precalc(int v , int pr){
if(v <= n) sz[v] = 1;
for(int to : g[v]){
if(to != pr){
precalc(to,v);
sz[v] += sz[to];
if(sz[to] > sz[mxson[v]]) mxson[v] = to;
}
}
}
void upd(int i, int x){
for(; i <= n; i |= (i + 1)) t[i] += x;
}
int get(int i){
int res = 0;
for(; i > 0; i = (i & (i + 1)) - 1) res += t[i];
return res;
}
void dfs(int v , int pr){
for(int to : g[v]){
if(to != pr && to != mxson[v]){
dfs(to,v);
for(int x : vec[to]) upd(x,-1);
}
}
if(mxson[v]) {
dfs(mxson[v],v);
vec[v] = vec[mxson[v]];
ans[v] += ans[mxson[v]];
}
int res = 0 , res1 = 0;
for(int to : g[v]){
int ob = get(n);
if(to != pr && to != mxson[v]) {
for(int x : vec[to]) {
int num = get(x);
res += num;
res1 += ob-num;
}
for(int x : vec[to]) {
upd(x,1);
vec[v].push_back(x);
}
ans[v] += ans[to];
}
}
if(v <= n) {
vec[v].push_back(v);
upd(v,1);
}
else {
ans[v] += min(res,res1);
}
}
int NUM;
void go(int v){
int x;
cin >> x;
if(x == 0){
int to = ++NUM;
g[v].push_back(to);
g[to].push_back(v);
go(to);
} else {
g[v].push_back(x);
g[x].push_back(v);
}
cin >> x;
if(x == 0){
int to = ++NUM;
g[v].push_back(to);
g[to].push_back(v);
go(to);
} else {
g[v].push_back(x);
g[x].push_back(v);
}
}
void solve(){
cin >> n;
NUM = n+1;
int a; cin >> a;
go(n+1);
precalc(n+1,0);
dfs(n+1,0);
cout << ans[n+1] << "\n";
}
/*
*/
signed main(){
ios;
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23840 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23748 KB |
Output is correct |
5 |
Correct |
12 ms |
23720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23840 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23852 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
24020 KB |
Output is correct |
2 |
Correct |
12 ms |
24040 KB |
Output is correct |
3 |
Correct |
11 ms |
23892 KB |
Output is correct |
4 |
Correct |
15 ms |
25428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
58440 KB |
Output is correct |
2 |
Correct |
20 ms |
25104 KB |
Output is correct |
3 |
Correct |
31 ms |
63748 KB |
Output is correct |
4 |
Runtime error |
32 ms |
65536 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
40124 KB |
Output is correct |
2 |
Runtime error |
44 ms |
65536 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
73 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
66 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
91 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
90 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
93 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |