#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
edi
https://oj.uz/submission/374896
tree dp for 38 points:
r[u] = sum(min(r[v],b[v]+1))
b[u] = sum(min(r[v]+1,b[v]))
key idea:
when a node is changed, only the dp values of its parents are affected
only update parents
=> hld
if the problem was on a line, we could use a segtree
each node [l,r] contains dp[x][y], which denotes the min cost to achieve comp[l] = x and comp[r] = y
can be merged easily
how to extend this idea to a tree?
each node belongs to exactly 1 chain in the hld
when processing a chain, only dp values in the chain will change
some nodes on the chain may have children that belong to other chains
for such children, their values wont change, so their contribution to dp[u][0/1] is fixed
because these values dont change, we can put them in the segtree leaf that denotes u
i.e for the segtree leaf that denotes u,
dp[0][0] = sum(min(r[v],b[v]+1)), v doesnt belong to the same chain as u
dp[1][1] = sum(min(r[v]+1,b[v])), v doesnt belong to the same chain as u
dp[0][1] = dp[1][0] = inf (range only contains 1 node, so starting comp = ending comp)
with all these values in the segtree, find the value at the root of the chain
now when we move up, we move to another chain
so we have to update the new dp values of the parent of the current chain (which may or may not be the root of the new chain)
tnis can be done by adding/subtracting some value from the dp values of the parent and then doing a point update on the segtree
repeat the same process for all chains
at the end of the process, we would have updated the dp chain that contains the root of the tree (which is 1)
when we want to get the answer, just find the dp of the chain that contains the root and return the min value of dp[x][y]
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
template<typename T>
struct segtree {
// https://codeforces.com/blog/entry/18051
/*=======================================================*/
struct data {
int dp[2][2];
bool active;
data(){
rep(i,2){
rep(j,2){
dp[i][j] = inf1;
}
}
active = false;
}
};
data neutral = data();
data merge(data &left, data &right) {
if(!left.active and !right.active) return left;
if(!right.active) return left;
if(!left.active) return right;
data curr;
curr.active = true;
rep(i,2){
rep(j,2){
rep(k,2){
rep(l,2){
amin(curr.dp[i][l],left.dp[i][j]+right.dp[k][l]+(j!=k));
}
}
}
}
return curr;
}
void create(int i, T v) {
}
void modify(int i, T v) {
tr[i] = neutral;
tr[i].dp[0][0] = v.ff;
tr[i].dp[1][1] = v.ss;
tr[i].active = true;
}
/*=======================================================*/
int n;
vector<data> tr;
segtree() {
}
segtree(int siz) {
init(siz);
}
void init(int siz) {
n = siz;
tr.assign(2 * n, neutral);
}
void build(vector<T> &a, int siz) {
rep(i, siz) create(i + n, a[i]);
rev(i, n - 1, 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]);
}
void pupd(int i, T v) {
modify(i + n, v);
for (i = (i + n) >> 1; i; i >>= 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]);
}
data query(int l, int r) {
data resl = neutral, resr = neutral;
for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
if (l & 1) resl = merge(resl, tr[l++]);
if (!(r & 1)) resr = merge(tr[r--], resr);
}
return merge(resl, resr);
}
};
vector<int> adj[N];
vector<int> a(N); // 0 = none, 1 = cat, 2 = dog
vector<int> subsiz(N);
vector<int> depth(N), par(N);
void dfs1(int u, int p){
subsiz[u] = 1;
if(p != -1) par[u] = p;
trav(v,adj[u]){
if(v == p) conts;
depth[v] = depth[u]+1;
dfs1(v,u);
subsiz[u] += subsiz[v];
}
}
vector<int> pos(N), head(N), chain_siz(N);
int timer = 1;
void dfs2(int u, int p, int h){
pos[u] = timer++;
head[u] = h;
chain_siz[h]++;
pii mx = {-inf1,-1};
trav(v,adj[u]){
if(v == p) conts;
pii px = {subsiz[v],v};
amax(mx,px);
}
int heavy = mx.ss;
if(heavy != -1){
dfs2(heavy,u,h);
}
trav(v,adj[u]){
if(v == p or v == heavy) conts;
dfs2(v,u,v);
}
}
segtree<pii> st;
void initialize(int n, std::vector<int> A, std::vector<int> B) {
rep(i,n-1){
int u = A[i], v = B[i];
adj[u].pb(v), adj[v].pb(u);
}
dfs1(1,-1);
dfs2(1,-1,1);
st = segtree<pii>(n+5);
rep1(i,n) st.pupd(i,{0,0});
}
vector<int> sum1(N), sum2(N);
int get_ans(){
auto dp = st.query(pos[1],pos[1]+chain_siz[1]-1).dp;
int ans = inf1;
rep(i,2){
rep(j,2){
amin(ans,dp[i][j]);
}
}
return ans;
}
void rem(int u){
while(u){
if(u == head[u]){
auto dp = st.query(pos[u],pos[u]+chain_siz[u]-1).dp;
int cat = min(dp[0][0],dp[0][1]);
int dog = min(dp[1][0],dp[1][1]);
sum1[par[u]] -= min(cat,dog+1);
sum2[par[u]] -= min(cat+1,dog);
u = par[u];
}
else{
u = head[u];
}
}
}
void add(int u){
while(u){
{
pii px = {sum1[u],sum2[u]};
if(a[u] == 1){
px.ss = inf1;
}
else if(a[u] == 2){
px.ff = inf1;
}
st.pupd(pos[u],px);
}
if(u == head[u]){
auto dp = st.query(pos[u],pos[u]+chain_siz[u]-1).dp;
int cat = min(dp[0][0],dp[0][1]);
int dog = min(dp[1][0],dp[1][1]);
sum1[par[u]] += min(cat,dog+1);
sum2[par[u]] += min(cat+1,dog);
u = par[u];
}
else{
u = head[u];
}
}
}
void change_state(int u, int val){
rem(u);
a[u] = val;
add(u);
}
int cat(int v) {
change_state(v,1);
return get_ans();
}
int dog(int v) {
change_state(v,2);
return get_ans();
}
int neighbor(int v) {
change_state(v,0);
return get_ans();
}
Compilation message
catdog.cpp: In function 'int get_ans()':
catdog.cpp:254:17: warning: '<anonymous>[0]' is used uninitialized in this function [-Wuninitialized]
254 | amin(ans,dp[i][j]);
| ~~~~^~~~~~~~~~~~~~
catdog.cpp:254:17: warning: '<anonymous>[1]' is used uninitialized in this function [-Wuninitialized]
catdog.cpp:254:17: warning: '*((void*)(&<anonymous>)+8)[0]' is used uninitialized in this function [-Wuninitialized]
catdog.cpp:254:17: warning: '*((void*)(&<anonymous>)+8)[1]' is used uninitialized in this function [-Wuninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
3 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
3 ms |
6232 KB |
Output is correct |
5 |
Correct |
3 ms |
6236 KB |
Output is correct |
6 |
Correct |
3 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
3 ms |
6232 KB |
Output is correct |
9 |
Correct |
3 ms |
6236 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
3 ms |
6232 KB |
Output is correct |
13 |
Correct |
3 ms |
6236 KB |
Output is correct |
14 |
Correct |
3 ms |
6236 KB |
Output is correct |
15 |
Correct |
3 ms |
6228 KB |
Output is correct |
16 |
Correct |
3 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
3 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
3 ms |
6232 KB |
Output is correct |
5 |
Correct |
3 ms |
6236 KB |
Output is correct |
6 |
Correct |
3 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
3 ms |
6232 KB |
Output is correct |
9 |
Correct |
3 ms |
6236 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
3 ms |
6232 KB |
Output is correct |
13 |
Correct |
3 ms |
6236 KB |
Output is correct |
14 |
Correct |
3 ms |
6236 KB |
Output is correct |
15 |
Correct |
3 ms |
6228 KB |
Output is correct |
16 |
Correct |
3 ms |
6236 KB |
Output is correct |
17 |
Correct |
4 ms |
6236 KB |
Output is correct |
18 |
Correct |
6 ms |
6236 KB |
Output is correct |
19 |
Correct |
3 ms |
6236 KB |
Output is correct |
20 |
Correct |
2 ms |
6236 KB |
Output is correct |
21 |
Correct |
3 ms |
6232 KB |
Output is correct |
22 |
Correct |
3 ms |
6316 KB |
Output is correct |
23 |
Correct |
4 ms |
6236 KB |
Output is correct |
24 |
Correct |
4 ms |
6236 KB |
Output is correct |
25 |
Correct |
4 ms |
6236 KB |
Output is correct |
26 |
Correct |
4 ms |
6236 KB |
Output is correct |
27 |
Correct |
3 ms |
6236 KB |
Output is correct |
28 |
Correct |
3 ms |
6236 KB |
Output is correct |
29 |
Correct |
4 ms |
6236 KB |
Output is correct |
30 |
Correct |
3 ms |
6236 KB |
Output is correct |
31 |
Correct |
3 ms |
6236 KB |
Output is correct |
32 |
Correct |
3 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6232 KB |
Output is correct |
2 |
Correct |
3 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
3 ms |
6232 KB |
Output is correct |
5 |
Correct |
3 ms |
6236 KB |
Output is correct |
6 |
Correct |
3 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6236 KB |
Output is correct |
8 |
Correct |
3 ms |
6232 KB |
Output is correct |
9 |
Correct |
3 ms |
6236 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
3 ms |
6232 KB |
Output is correct |
13 |
Correct |
3 ms |
6236 KB |
Output is correct |
14 |
Correct |
3 ms |
6236 KB |
Output is correct |
15 |
Correct |
3 ms |
6228 KB |
Output is correct |
16 |
Correct |
3 ms |
6236 KB |
Output is correct |
17 |
Correct |
4 ms |
6236 KB |
Output is correct |
18 |
Correct |
6 ms |
6236 KB |
Output is correct |
19 |
Correct |
3 ms |
6236 KB |
Output is correct |
20 |
Correct |
2 ms |
6236 KB |
Output is correct |
21 |
Correct |
3 ms |
6232 KB |
Output is correct |
22 |
Correct |
3 ms |
6316 KB |
Output is correct |
23 |
Correct |
4 ms |
6236 KB |
Output is correct |
24 |
Correct |
4 ms |
6236 KB |
Output is correct |
25 |
Correct |
4 ms |
6236 KB |
Output is correct |
26 |
Correct |
4 ms |
6236 KB |
Output is correct |
27 |
Correct |
3 ms |
6236 KB |
Output is correct |
28 |
Correct |
3 ms |
6236 KB |
Output is correct |
29 |
Correct |
4 ms |
6236 KB |
Output is correct |
30 |
Correct |
3 ms |
6236 KB |
Output is correct |
31 |
Correct |
3 ms |
6236 KB |
Output is correct |
32 |
Correct |
3 ms |
6236 KB |
Output is correct |
33 |
Correct |
313 ms |
11392 KB |
Output is correct |
34 |
Correct |
107 ms |
11856 KB |
Output is correct |
35 |
Correct |
292 ms |
10148 KB |
Output is correct |
36 |
Correct |
473 ms |
15004 KB |
Output is correct |
37 |
Correct |
20 ms |
8792 KB |
Output is correct |
38 |
Correct |
551 ms |
16000 KB |
Output is correct |
39 |
Correct |
508 ms |
15936 KB |
Output is correct |
40 |
Correct |
484 ms |
15936 KB |
Output is correct |
41 |
Correct |
512 ms |
15888 KB |
Output is correct |
42 |
Correct |
495 ms |
16060 KB |
Output is correct |
43 |
Correct |
483 ms |
15936 KB |
Output is correct |
44 |
Correct |
480 ms |
16068 KB |
Output is correct |
45 |
Correct |
478 ms |
15952 KB |
Output is correct |
46 |
Correct |
489 ms |
15768 KB |
Output is correct |
47 |
Correct |
487 ms |
15760 KB |
Output is correct |
48 |
Correct |
129 ms |
13260 KB |
Output is correct |
49 |
Correct |
142 ms |
15048 KB |
Output is correct |
50 |
Correct |
50 ms |
8272 KB |
Output is correct |
51 |
Correct |
57 ms |
9688 KB |
Output is correct |
52 |
Correct |
25 ms |
8028 KB |
Output is correct |
53 |
Correct |
215 ms |
15068 KB |
Output is correct |
54 |
Correct |
170 ms |
10076 KB |
Output is correct |
55 |
Correct |
423 ms |
13132 KB |
Output is correct |
56 |
Correct |
261 ms |
10652 KB |
Output is correct |
57 |
Correct |
352 ms |
14420 KB |
Output is correct |
58 |
Correct |
30 ms |
9948 KB |
Output is correct |
59 |
Correct |
58 ms |
9304 KB |
Output is correct |
60 |
Correct |
129 ms |
14116 KB |
Output is correct |
61 |
Correct |
143 ms |
14516 KB |
Output is correct |
62 |
Correct |
91 ms |
12660 KB |
Output is correct |
63 |
Correct |
64 ms |
12656 KB |
Output is correct |
64 |
Correct |
68 ms |
13660 KB |
Output is correct |
65 |
Correct |
86 ms |
18104 KB |
Output is correct |
66 |
Correct |
97 ms |
9316 KB |
Output is correct |
67 |
Correct |
89 ms |
14772 KB |
Output is correct |
68 |
Correct |
190 ms |
18416 KB |
Output is correct |
69 |
Correct |
46 ms |
7504 KB |
Output is correct |
70 |
Correct |
11 ms |
6516 KB |
Output is correct |
71 |
Correct |
87 ms |
11652 KB |
Output is correct |
72 |
Correct |
132 ms |
16412 KB |
Output is correct |
73 |
Correct |
281 ms |
20308 KB |
Output is correct |
74 |
Correct |
301 ms |
18008 KB |
Output is correct |
75 |
Correct |
233 ms |
20208 KB |
Output is correct |
76 |
Correct |
216 ms |
19472 KB |
Output is correct |
77 |
Correct |
302 ms |
18632 KB |
Output is correct |