#include "catdog.h"
#include <bits/stdc++.h>
#define se second
#define fi first
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5 + 3;
const int inf = 1 << 30;
int n, A[N], p[N], des[N], d[N], psg[N];
int v[N][2], dp[N][2];
vector<int> G[N], H[N];
int dif(pii a, pii b){
if(a.se == 0 || b.se == 0) return 0;
return a.se != b.se;
}
struct CD{
set<pair<int, int>> s; int ans = 0, top = 0, bot = 0, tp = inf, bp = inf;
void insert(int v, int cd){
auto ptr = s.lower_bound({v, cd});
pii m1 = ptr != s.begin() ? *prev(ptr) : mp(0, 0);
pii p1 = ptr != s.end() ? *ptr : mp(0, 0);
ans += dif({v, cd}, m1) + dif({v, cd}, p1) - dif(m1, p1);
s.insert({v, cd});
top = s.empty() ? 0 : (s.begin())->se;
tp = s.empty() ? inf : (s.begin())->fi;
bot = s.empty() ? 0 : (--s.end())->se;
bp = s.empty() ? inf : (--s.end())->fi;
}
void erase(int v){
auto ptr = s.lower_bound({v, -1});
pii m1 = ptr != s.begin() ? *prev(ptr) : mp(0, 0);
pii p1 = ptr != s.end() && next(ptr) != s.end() ? *next(ptr) : mp(0, 0);
ans += dif(m1, p1) - dif(*ptr, m1) - dif(*ptr, p1);
s.erase(ptr);
top = s.empty() ? 0 : (s.begin())->se;
tp = s.empty() ? inf : (s.begin())->fi;
bot = s.empty() ? 0 : (--s.end())->se;
bp = s.empty() ? inf : (--s.end())->fi;
}
} V[N];
void dfs(int v, int par){
des[v] = v;
for(auto i : G[v]){
if(i != par){
p[i] = v;
d[i] = d[v] + 1;
dfs(i, v);
des[v] = (G[v].size() - (v != 1) == 1) ? des[i] : v;
}
}
}
void initialize(int n, vector<int> A, vector<int> B) {
::n = n;
for(int i = 0; i < n - 1; i++){
G[A[i]].pb(B[i]);
G[B[i]].pb(A[i]);
}
dfs(1, -1);
for(int i = 1; i <= n; i++) des[i] = i;
map<int, vector<int>> PC;
for(int i = 1; i <= n; i++){
PC[des[i]].push_back(i);
}
// for(int i = 1; i <= n; i++){
// cout << des[i] << ' ';
// }
// cout << endl;
for(auto& i : PC){
sort(i.se.begin(), i.se.end(), [](int a, int b){
return d[a] < d[b];
});
}
vector<int> sg;
for(auto i : PC){
sg.pb(i.se[0]);
}
for(auto i : sg){
H[p[i]].pb(des[i]);
psg[des[i]] = p[i];
}
}
void lift(int v){
while(true){
dp[v][0] = dp[v][1] = V[v].ans;
for(auto i : H[v]){
dp[v][0] += min(dp[i][0], dp[i][1] + 1);
dp[v][1] += min(dp[i][1], dp[i][0] + 1);
}
if(V[v].bp == d[v]){
if(V[v].bot == 1) dp[v][1] = inf;
else if(V[v].bot == 2) dp[v][0] = inf;
}else{
if(V[v].bot == 1) dp[v][1]++;
else if(V[v].bot == 2) dp[v][0]++;
}
if(V[v].top + V[v].bot == 3) swap(dp[v][0], dp[v][1]);
if(V[v].tp == d[psg[v]] + 1){
if(V[v].top == 1) dp[v][1] = inf;
else if(V[v].top == 2) dp[v][0] = inf;
}else{
if(V[v].top == 1) dp[v][1]++;
else if(V[v].top == 2) dp[v][0]++;
}
if(v == 0) return;
v = psg[v];
}
}
int sol(){
return min(dp[0][0], dp[0][1]);
}
void out(char c){
// cout << c << "<<<" << endl;
// for(int i = 0; i <= n; i++){
// cout << dp[i][0] << ' ' << dp[i][1] << endl;
// }
}
int cat(int v) {
A[v] = 1;
V[des[v]].insert(d[v], 1);
lift(des[v]);
out('c');
return sol();
}
int dog(int v) {
A[v] = 2;
V[des[v]].insert(d[v], 2);
lift(des[v]);
out('d');
return sol();
}
int neighbor(int v) {
A[v] = 0;
V[des[v]].erase(d[v]);
lift(des[v]);
out('n');
return sol();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12044 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12048 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12052 KB |
Output is correct |
9 |
Correct |
7 ms |
12052 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
12052 KB |
Output is correct |
12 |
Correct |
8 ms |
11988 KB |
Output is correct |
13 |
Correct |
8 ms |
12048 KB |
Output is correct |
14 |
Correct |
6 ms |
12052 KB |
Output is correct |
15 |
Correct |
9 ms |
12044 KB |
Output is correct |
16 |
Correct |
6 ms |
12048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12044 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12048 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12052 KB |
Output is correct |
9 |
Correct |
7 ms |
12052 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
12052 KB |
Output is correct |
12 |
Correct |
8 ms |
11988 KB |
Output is correct |
13 |
Correct |
8 ms |
12048 KB |
Output is correct |
14 |
Correct |
6 ms |
12052 KB |
Output is correct |
15 |
Correct |
9 ms |
12044 KB |
Output is correct |
16 |
Correct |
6 ms |
12048 KB |
Output is correct |
17 |
Correct |
7 ms |
12244 KB |
Output is correct |
18 |
Correct |
8 ms |
12292 KB |
Output is correct |
19 |
Correct |
7 ms |
12244 KB |
Output is correct |
20 |
Correct |
7 ms |
12056 KB |
Output is correct |
21 |
Correct |
9 ms |
12096 KB |
Output is correct |
22 |
Correct |
10 ms |
12052 KB |
Output is correct |
23 |
Correct |
10 ms |
12140 KB |
Output is correct |
24 |
Correct |
8 ms |
12244 KB |
Output is correct |
25 |
Correct |
7 ms |
12116 KB |
Output is correct |
26 |
Correct |
8 ms |
12060 KB |
Output is correct |
27 |
Correct |
8 ms |
12052 KB |
Output is correct |
28 |
Correct |
9 ms |
12180 KB |
Output is correct |
29 |
Correct |
11 ms |
12244 KB |
Output is correct |
30 |
Correct |
7 ms |
12116 KB |
Output is correct |
31 |
Correct |
7 ms |
12180 KB |
Output is correct |
32 |
Correct |
7 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12044 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12048 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12052 KB |
Output is correct |
9 |
Correct |
7 ms |
12052 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
7 ms |
12052 KB |
Output is correct |
12 |
Correct |
8 ms |
11988 KB |
Output is correct |
13 |
Correct |
8 ms |
12048 KB |
Output is correct |
14 |
Correct |
6 ms |
12052 KB |
Output is correct |
15 |
Correct |
9 ms |
12044 KB |
Output is correct |
16 |
Correct |
6 ms |
12048 KB |
Output is correct |
17 |
Correct |
7 ms |
12244 KB |
Output is correct |
18 |
Correct |
8 ms |
12292 KB |
Output is correct |
19 |
Correct |
7 ms |
12244 KB |
Output is correct |
20 |
Correct |
7 ms |
12056 KB |
Output is correct |
21 |
Correct |
9 ms |
12096 KB |
Output is correct |
22 |
Correct |
10 ms |
12052 KB |
Output is correct |
23 |
Correct |
10 ms |
12140 KB |
Output is correct |
24 |
Correct |
8 ms |
12244 KB |
Output is correct |
25 |
Correct |
7 ms |
12116 KB |
Output is correct |
26 |
Correct |
8 ms |
12060 KB |
Output is correct |
27 |
Correct |
8 ms |
12052 KB |
Output is correct |
28 |
Correct |
9 ms |
12180 KB |
Output is correct |
29 |
Correct |
11 ms |
12244 KB |
Output is correct |
30 |
Correct |
7 ms |
12116 KB |
Output is correct |
31 |
Correct |
7 ms |
12180 KB |
Output is correct |
32 |
Correct |
7 ms |
12116 KB |
Output is correct |
33 |
Correct |
280 ms |
23560 KB |
Output is correct |
34 |
Correct |
150 ms |
25364 KB |
Output is correct |
35 |
Correct |
314 ms |
20204 KB |
Output is correct |
36 |
Correct |
538 ms |
31772 KB |
Output is correct |
37 |
Correct |
30 ms |
18656 KB |
Output is correct |
38 |
Correct |
568 ms |
33928 KB |
Output is correct |
39 |
Correct |
526 ms |
33964 KB |
Output is correct |
40 |
Correct |
644 ms |
33908 KB |
Output is correct |
41 |
Correct |
608 ms |
33912 KB |
Output is correct |
42 |
Correct |
747 ms |
33928 KB |
Output is correct |
43 |
Correct |
625 ms |
33880 KB |
Output is correct |
44 |
Correct |
675 ms |
33936 KB |
Output is correct |
45 |
Correct |
851 ms |
33936 KB |
Output is correct |
46 |
Correct |
600 ms |
33944 KB |
Output is correct |
47 |
Correct |
634 ms |
33924 KB |
Output is correct |
48 |
Execution timed out |
3054 ms |
27060 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |