이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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:
https://github.com/mostafa-saad/MyCompetitiveProgramming/blob/master/Olympiad/JOI/JOIOC-13-synchronization.txt
a[u] = #of distinct vals in node u
what happens when an edge is activated?
new_value = a[u]+a[v]-common (common = #of guys that are counted in both a[u] and a[v])
then set all values in the connected component of u to this value
how to find common?
common = a[u] when nodes (u,v) where connected the last time
this is just the value of a[u] when edge (u,v) was previously deleted
handle the following operations:
1) connect (u,v)
2) disconnect (u,v)
3) find the value of a[u]
4) set all values in the cc of u to x
maybe use link-cut tree to handle? (but too hard to implement)
easier way is to handle these ops using a fenwick tree
for each cc, rep = the node with the lowest depth
to find the rep of u, just b.s over the largest ances which we can reach by passing through active edges
finding the #of active edges on the path can be done with a fenwick tree (path sum queries)
maintain the value of a[u] just in the root, so the 3rd and 4th ops are done (just get/set the value at the root of the cc)
ops 1 and 2 can be done by point updates on the fenwick tree
*/
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 fenwick {
int siz;
vector<T> tree;
fenwick() {
}
fenwick(int n) {
siz = n;
tree = vector<T>(n + 1);
}
int lsb(int x) {
return x & -x;
}
void build(vector<T> &a, int n) {
for (int i = 1; i <= n; ++i) {
int par = i + lsb(i);
tree[i] += a[i];
if (par <= siz) {
tree[par] += tree[i];
}
}
}
void pupd(int i, T v) {
while (i <= siz) {
tree[i] += v;
i += lsb(i);
}
}
T sum(int i) {
T res = 0;
while (i) {
res += tree[i];
i -= lsb(i);
}
return res;
}
T query(int l, int r) {
if (l > r) return 0;
T res = sum(r) - sum(l - 1);
return res;
}
};
vector<pll> adj[N];
struct lca_algo {
// LCA template (for graphs with 1-based indexing)
int LOG = 1;
vector<int> depth;
vector<vector<int>> up;
vector<int> tin, tout;
vector<int> edge_node;
int timer = 1;
lca_algo() {
}
lca_algo(int n) {
lca_init(n);
}
void lca_init(int n) {
while ((1 << LOG) < n) LOG++;
up = vector<vector<int>>(n + 1, vector<int>(LOG, 1));
depth = vector<int>(n + 1);
tin = vector<int>(n + 1);
tout = vector<int>(n + 1);
edge_node = vector<int>(n + 1);
lca_dfs(1, -1);
}
void lca_dfs(int node, int par) {
tin[node] = timer++;
for(auto [child,id] : adj[node]) {
if (child == par) conts;
up[child][0] = node;
rep1(j, LOG - 1) {
up[child][j] = up[up[child][j - 1]][j - 1];
}
depth[child] = depth[node] + 1;
edge_node[id] = child;
lca_dfs(child, node);
}
tout[node] = timer-1;
}
int lift(int u, int k) {
rep(j, LOG) {
if (k & (1 << j)) {
u = up[u][j];
}
}
return u;
}
int query(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
int k = depth[u] - depth[v];
u = lift(u, k);
if (u == v) return u;
rev(j, LOG - 1, 0) {
if (up[u][j] != up[v][j]) {
u = up[u][j];
v = up[v][j];
}
}
u = up[u][0];
return u;
}
int get_dis(int u, int v) {
int lca = query(u, v);
return depth[u] + depth[v] - 2 * depth[lca];
}
bool is_ances(int u, int v){
return tin[u] <= tin[v] and tout[u] >= tout[v];
}
};
void solve(int test_case)
{
ll n,m,q; cin >> n >> m >> q;
vector<pll> edges(n+5);
rep1(i,n-1){
ll u,v; cin >> u >> v;
adj[u].pb({v,i}), adj[v].pb({u,i});
edges[i] = {u,v};
}
vector<ll> a(n+5);
rep1(i,n) a[i] = 1;
lca_algo LCA(n);
fenwick<ll> fenw(n+5);
auto edge_change = [&](ll id, ll val){
ll u = LCA.edge_node[id];
fenw.pupd(LCA.tin[u],val);
fenw.pupd(LCA.tout[u]+1,-val);
};
auto path_sum = [&](ll u, ll v){
assert(LCA.is_ances(u,v));
return fenw.sum(LCA.tin[v])-fenw.sum(LCA.tin[u]);
};
auto find_cc = [&](ll u){
ll l = 0, r = LCA.depth[u];
ll mx_ances = -1;
while(l <= r){
ll mid = (l+r) >> 1;
ll ances = LCA.lift(u,mid);
ll len = LCA.depth[u]-LCA.depth[ances];
ll sum = path_sum(ances,u);
if(sum == len){
mx_ances = ances;
l = mid+1;
}
else{
r = mid-1;
}
}
assert(mx_ances != -1);
return mx_ances;
};
vector<bool> active(n+5);
vector<ll> edge_sub(n+5);
while(m--){
ll id; cin >> id;
auto [u,v] = edges[id];
ll pu = find_cc(u), pv = find_cc(v);
if(!active[id]){
active[id] = 1;
ll new_val = a[pu]+a[pv]-edge_sub[id];
edge_sub[id] = 0;
edge_change(id,1);
pu = find_cc(u);
a[pu] = new_val;
}
else{
active[id] = 0;
edge_sub[id] = a[pu];
edge_change(id,-1);
ll pu_new = find_cc(u), pv_new = find_cc(v);
a[pu_new] = a[pv_new] = a[pu];
}
}
while(q--){
ll u; cin >> u;
ll pu = find_cc(u);
ll ans = a[pu];
cout << ans << endl;
}
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |