結果
| 問題 |
No.3189 Semifinal Stage
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-09-10 15:09:49 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 4,021 bytes |
| コンパイル時間 | 4,545 ms |
| コンパイル使用メモリ | 295,616 KB |
| 実行使用メモリ | 9,344 KB |
| 最終ジャッジ日時 | 2025-09-10 15:10:00 |
| 合計ジャッジ時間 | 10,932 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 1 TLE * 1 |
| other | -- * 30 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i< (n); ++i)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define fore(i, a) for(auto &i:a)
using ll = long long;
#include<atcoder/segtree>
using namespace atcoder;
void chmin(int &a, int b){if(b < a) a = b;}
class HLdecomposition{
private:
int V;
vector<vector<int> > G;
vector<int> stsize, parent, pathtop, pathbottom, in, out, dist;
int root;
void BuildStsize(int u, int p){
stsize[u] = 1, parent[u] = p;
for(int& v : G[u]){
if(v == p){
if(v == G[u].back()) break;
else swap(v, G[u].back());
}
dist[v] = dist[u]+1;
BuildStsize(v, u);
stsize[u] += stsize[v];
if(stsize[v] > stsize[G[u][0]]){
swap(v, G[u][0]);
}
}
}
void BuildPath(int u, int p, int& tm){
in[u] = tm++;
if(G[u].empty()){
pathbottom[pathtop[u]] = u;
}
for(int v : G[u]){
if(v == p) continue;
pathtop[v] = (v == G[u][0] ? pathtop[u] : v);
BuildPath(v, u, tm);
}
out[u] = tm;
}
public:
int get_pathbottom(int u){return pathbottom[pathtop[u]];}
int get_parent(int u){return parent[u];}
int get_pathtop(int u){return pathtop[u];}
int get_dist(int u){return dist[u];}
void add_edge(int u, int v){
G[u].push_back(v), G[v].push_back(u);
}
void build(int _root=0){
root = _root;
int tm = 0;
BuildStsize(root, -1);
pathtop[root] = root;
BuildPath(root, -1, tm);
}
//元の頂点のインデックスの配列上でのidを返す
inline int get(int a){
return in[a];
}
int lca(int a, int b){
int pa = pathtop[a], pb = pathtop[b];
while(pathtop[a] != pathtop[b]){
if(in[pa] > in[pb]){
a = parent[pa], pa = pathtop[a];
}else{
b = parent[pb], pb = pathtop[b];
}
}
if(in[a] > in[b]) swap(a, b);
return a;
}
void subtree_query(int a, const function< void(int, int) > &func){
func(in[a], out[a]);
}
// 例: hl.path_query(q, r, [&](int l, int r){ seg.range(l, r, s); })
// 例: hl.query(q, r, [&](int l, int r){ ans += seg.query(l, r); })
void path_query(int a, int b, const function< void(int, int) > &func){
int pa = pathtop[a], pb = pathtop[b];
while(pathtop[a] != pathtop[b]){
if(in[pa] > in[pb]){
func(in[pa], in[a] + 1);
a = parent[pa], pa = pathtop[a];
}else{
func(in[pb], in[b] + 1);
b = parent[pb], pb = pathtop[b];
}
}
if(in[a] > in[b]) swap(a, b);
func(in[a], in[b] + 1);
}
HLdecomposition(int node_size) : V(node_size), G(V), stsize(V, 0), parent(V, -1),
pathtop(V, -1), in(V, -1), out(V, -1), pathbottom(V, -1), dist(V, 0){}
};
const int INF = 1073741823;
int op(int a, int b){
return min(a, b);
}
int e(){
return INF;
}
int main() {
int n;
cin >> n;
HLdecomposition hld(n);
rep(i, n-1){
int u, v;
cin >> u >> v;
u--;v--;
hld.add_edge(u, v);
}
hld.build();
int q;cin >> q;
vector<pair<int,int>> qs(q);
rep(i, q){int t, a;cin >> t >> a;qs[i] = {t, --a};}
vector<set<pair<int,int>>> sets(n);
rep(i, n)sets[i].insert({INF,-1});
segtree<int, op, e> seg1(n), seg2(n);
rep(i, q){
auto[t, a] = qs[i];
a--;
if(t == 1){
pair<int,int> p = {hld.get_dist(a), a};
int pos = a;
while(pos != -1){
if(sets[pos].count(p))sets[pos].erase(p);
else sets[pos].insert(p);
auto[mind, _] = *sets[pos].begin();
seg1.set(hld.get(pos), mind);
seg2.set(hld.get(pos), mind-2*hld.get_dist(pos));
pos = hld.get_parent(hld.get_pathtop(pos));
}
}
else{
int pos = a;
int ans1 = INF;
int ans2 = INF;
while(pos != -1){
chmin(ans1, seg1.prod(hld.get(pos), hld.get(hld.get_pathbottom(pos))+1)+hld.get_dist(a)-2*hld.get_dist(pos));
chmin(ans2, seg2.prod(hld.get(hld.get_pathtop(pos)), hld.get(pos))+hld.get_dist(a));
}
cout << min(ans1, ans2) << endl;
}
}
}