This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e8;
const int N = 100002;
struct Table{
int a00, a01, a10, a11;
Table()
{
a01 = 1, a10 = 1;
}
};
Table Merge(Table &A, Table &B)
{
Table S;
S.a00 = min(min(A.a00 + B.a00, A.a01 + B.a10), min(A.a00 + B.a10, A.a01 + B.a00) + 1);
S.a01 = min(min(A.a00 + B.a01, A.a01 + B.a11), min(A.a00 + B.a11, A.a01 + B.a01) + 1);
S.a11 = min(min(A.a10 + B.a01, A.a11 + B.a11), min(A.a10 + B.a11, A.a11 + B.a01) + 1);
S.a10 = min(min(A.a10 + B.a00, A.a11 + B.a10), min(A.a10 + B.a10, A.a11 + B.a00) + 1);
return S;
}
struct Sollution{
int s;
vector<Table> tree;
Sollution(int _n)
{
for (s = 1; s < _n; s *= 2) ;
tree.resize(2 * s);
for (int i = s; i < s + _n; i++) tree[i].a01 = INF, tree[i].a10 = INF;
}
void Change(int i, int &cost0, int &cost1)
{
i += s;
tree[i].a00 += cost0;
tree[i].a11 += cost1;
for (i /= 2; i; i /= 2)
tree[i] = Merge(tree[2 * i], tree[2 * i + 1]);
}
void Cost(int &d0, int &d1)
{
int minc0 = min(tree[1].a00, tree[1].a01), minc1 = min(tree[1].a10, tree[1].a11);
d0 = min(minc0, minc1 + 1);
d1 = min(minc0 + 1, minc1);
}
};
ll n;
vector<int> graph[N];
ll x[N];
ll papa[N];
ll sz[N];
int main_child[N];
vector<int> other_child[N];
vector<vector<int> > route;
vector<Sollution> hld;
int route_pos[N];
int route_id[N];
void dfs1(int v, int p)
{
papa[v] = p;
sz[v] = 1;
for (auto u : graph[v]) if (u != p)
{
dfs1(u, v);
sz[v] += sz[u];
}
}
void dfs2(int v, int p)
{
int mxsz = 0, id = -1;
for (auto u : graph[v]) if (u != p)
{
if (sz[u] > mxsz)
{
mxsz = sz[u];
id = u;
}
}
main_child[v] = id;
for (auto u : graph[v]) if (u != p)
{
dfs2(u, v);
if (u != id) other_child[v].push_back(u);
}
}
int answer()
{
Table T = hld[0].tree[1];
return min(min(T.a00, T.a01), min(T.a10, T.a11));
}
void initialize(int n0, vector<int> a, vector<int> b)
{
n = n0;
for (int i = 0; i < n - 1; i++)
{
a[i]--, b[i]--;
graph[a[i]].push_back(b[i]);
graph[b[i]].push_back(a[i]);
}
for (int i = 0; i < n; i++)
{
x[i] = -1;
}
dfs1(0, 0);
dfs2(0, 0);
for (int i = 0; i < n; i++)
{
route_id[i] = -1;
route_pos[i] = -1;
}
for (int i = 0; i < n; i++)
{
if (route_id[i] == -1)
{
vector<int> r;
int x = i;
while (x != -1)
{
r.push_back(x);
x = main_child[x];
}
for (int j = 0; j < r.size(); j++)
{
route_id[r[j]] = route.size();
route_pos[r[j]] = j;
}
route.push_back(r);
hld.push_back(Sollution(r.size()));
}
}
}
void Change(int v, int cost0, int cost1)
{
int x = route_id[v];
int y = route_pos[v];
int oldd0, oldd1, d0, d1;
hld[x].Cost(oldd0, oldd1);
hld[x].Change(y, cost0, cost1);
hld[x].Cost(d0, d1);
v = route[x][0];
if (v == 0) return;
v = papa[v];
Change(v, d0 - oldd0, d1 - oldd1);
}
int cat(int v)
{
v--;
x[v] = 0;
Change(v, 0, INF);
return answer();
}
int dog(int v)
{
v--;
x[v] = 1;
Change(v, INF, 0);
return answer();
}
int neighbor(int v)
{
v--;
if (x[v] == 0) Change(v, 0, -INF);
if (x[v] == 1) Change(v, -INF, 0);
x[v] = -1;
return answer();
}
#ifdef LOCAL
int main()
{
int n;
cin >> n;
vector<int> a(n - 1), b(n - 1);
for (int i = 0; i < n - 1; i++)
{
cin >> a[i] >> b[i];
}
initialize(n, a, b);
int q;
cin >> q;
while (q--)
{
int t, v;
cin >> t >> v;
if (t == 1) cout << cat(v) << endl;
if (t == 2) cout << dog(v) << endl;
if (t == 3) cout << neighbor(v) << endl;
}
}
#endif // LOCAL
Compilation message (stderr)
catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:133:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < r.size(); j++)
~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |