結果
| 問題 | No.3442 Good Vertex Connectivity |
| コンテスト | |
| ユーザー |
👑 potato167
|
| 提出日時 | 2026-01-04 16:32:28 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 1,142 ms / 3,000 ms |
| コード長 | 6,161 bytes |
| 記録 | |
| コンパイル時間 | 2,430 ms |
| コンパイル使用メモリ | 228,116 KB |
| 実行使用メモリ | 49,128 KB |
| 最終ジャッジ日時 | 2026-02-06 20:52:41 |
| 合計ジャッジ時間 | 41,042 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 69 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct NodeAgg {
int cnt = 0;
int first = -1; // vertex id
int last = -1; // vertex id
long long sum = 0; // sum of dist between consecutive active nodes in-order (linear)
};
static const int MAXLOG = 20;
int N;
vector<vector<int>> g;
vector<int> depthv;
int up[MAXLOG][200000 + 5];
int tin[200000 + 5], tout[200000 + 5], euler[200000 + 5];
int timer_ = 0;
bool is_ancestor(int a, int b){
// a is ancestor of b in rooted tree
return tin[a] <= tin[b] && tout[b] <= tout[a];
}
int jump_up(int v, int k){
for(int j=0;j<MAXLOG;j++){
if(k & (1<<j)) v = up[j][v];
}
return v;
}
int lca(int a, int b){
if(is_ancestor(a,b)) return a;
if(is_ancestor(b,a)) return b;
int v = a;
for(int j=MAXLOG-1;j>=0;j--){
int nv = up[j][v];
if(nv != 0 && !is_ancestor(nv, b)) v = nv;
}
return up[0][v];
}
long long dist_uv(int a, int b){
int c = lca(a,b);
return (long long)depthv[a] + depthv[b] - 2LL*depthv[c];
}
struct SegTree {
int n; // size (power of two)
vector<NodeAgg> st; // 1-indexed internal or 0-indexed iterative
static NodeAgg mergeAgg(const NodeAgg &L, const NodeAgg &R){
if(L.cnt == 0) return R;
if(R.cnt == 0) return L;
NodeAgg res;
res.cnt = L.cnt + R.cnt;
res.first = L.first;
res.last = R.last;
res.sum = L.sum + R.sum + dist_uv(L.last, R.first);
return res;
}
SegTree(int sz=0){ init(sz); }
void init(int sz){
n = 1;
while(n < sz) n <<= 1;
st.assign(2*n, NodeAgg());
}
void build_from_active(const vector<int> &active_by_tin){
// active_by_tin is size N+1 (1..N), position = tin vertex
for(int i=0;i<N;i++){
int v = euler[i]; // vertex at position i
NodeAgg leaf;
if(active_by_tin[v]){
leaf.cnt = 1;
leaf.first = leaf.last = v;
leaf.sum = 0;
}
st[n+i] = leaf;
}
for(int i=n-1;i>=1;i--){
st[i] = mergeAgg(st[i<<1], st[i<<1|1]);
}
}
void update_pos(int pos0, int vertex_or_minus1){
// pos0: 0-indexed position in euler order
NodeAgg leaf;
if(vertex_or_minus1 != -1){
leaf.cnt = 1;
leaf.first = leaf.last = vertex_or_minus1;
leaf.sum = 0;
}
int i = n + pos0;
st[i] = leaf;
for(i >>= 1; i >= 1; i >>= 1){
st[i] = mergeAgg(st[i<<1], st[i<<1|1]);
if(i==1) break;
}
}
NodeAgg query_range(int l, int r){
// [l, r) 0-indexed, order-sensitive
NodeAgg leftAcc, rightAcc;
for(l += n, r += n; l < r; l >>= 1, r >>= 1){
if(l & 1) leftAcc = mergeAgg(leftAcc, st[l++]);
if(r & 1) rightAcc = mergeAgg(st[--r], rightAcc);
}
return mergeAgg(leftAcc, rightAcc);
}
};
long long steiner_vertices_from_agg(const NodeAgg &agg){
if(agg.cnt == 0) return 0;
if(agg.cnt == 1) return 1;
long long cycle = agg.sum + dist_uv(agg.last, agg.first);
long long edges = cycle / 2;
return edges + 1;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
g.assign(N+1, {});
for(int i=0;i<N-1;i++){
int a,b; cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
vector<int> color(N+1);
for(int i=1;i<=N;i++) cin >> color[i];
depthv.assign(N+1, 0);
// DFS iterative to fill tin/tout, up[0], depth, euler array
// root at 1, parent of root = 0
vector<int> parent(N+1,0);
vector<int> it(N+1,0);
vector<int> stv;
stv.reserve(N);
stv.push_back(1);
parent[1]=0;
up[0][1]=0;
depthv[1]=0;
while(!stv.empty()){
int v = stv.back();
if(it[v]==0){
tin[v]=timer_;
euler[timer_] = v;
timer_++;
}
if(it[v] < (int)g[v].size()){
int to = g[v][it[v]++];
if(to==parent[v]) continue;
parent[to]=v;
up[0][to]=v;
depthv[to]=depthv[v]+1;
stv.push_back(to);
}else{
tout[v]=timer_-1;
stv.pop_back();
}
}
for(int j=1;j<MAXLOG;j++){
for(int v=1;v<=N;v++){
int mid = up[j-1][v];
up[j][v] = (mid==0?0:up[j-1][mid]);
}
}
// Build segtree over euler order positions 0..N-1
SegTree seg(N);
vector<int> active(N+1,0);
for(int i=1;i<=N;i++) active[i] = (color[i]==1);
seg.build_from_active(active);
auto query_subtree = [&](int u)->long long{
int l = tin[u];
int r = tout[u] + 1;
NodeAgg agg = seg.query_range(l, r);
return steiner_vertices_from_agg(agg);
};
auto query_complement_subtree = [&](int u)->long long{
int l = tin[u];
int r = tout[u] + 1;
NodeAgg left = seg.query_range(0, l);
NodeAgg right = seg.query_range(r, N);
NodeAgg merged = SegTree::mergeAgg(left, right);
return steiner_vertices_from_agg(merged);
};
int Q; cin >> Q;
while(Q--){
int t; cin >> t;
if(t==1){
int v; cin >> v;
color[v] ^= 1;
int pos = tin[v];
if(color[v]==1){
seg.update_pos(pos, v);
}else{
seg.update_pos(pos, -1);
}
}else{
int x,y; cin >> x >> y;
if(x==y){
NodeAgg agg = seg.query_range(0, N);
cout << steiner_vertices_from_agg(agg) << "\n";
continue;
}
if(is_ancestor(y, x)){
// neighbor z is the child of y on path to x
int z = jump_up(x, depthv[x] - depthv[y] - 1);
cout << query_complement_subtree(z) << "\n";
}else{
// component containing x is outside subtree(y), so S = subtree(y)
cout << query_subtree(y) << "\n";
}
}
}
return 0;
}
potato167