-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtree_centroid_decomposition.cpp
More file actions
154 lines (129 loc) · 4.3 KB
/
Copy pathtree_centroid_decomposition.cpp
File metadata and controls
154 lines (129 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// 重心分解
//
// verified:
// 2018 第4回ドワンゴからの挑戦状 予選 E - ニワンゴくんの家探し
// https://atcoder.jp/contests/dwacon2018-prelims/tasks/dwacon2018_prelims_e
//
#include <bits/stdc++.h>
using namespace std;
// sizeSubtree[v] := v を根とする部分ツリーのサイズ (分割統治の毎ステップごとに再利用)
// isRemoved[v] := v が既に取り除かれたかどうか
// whoIsParent[v] := ツリーDP時に v の親が誰だったか
using Graph = vector<vector<int>>;
struct TreeCenteroid {
// input
Graph tree;
// results
vector<int> centroids;
// intermediate results
vector<int> sizeSubtree, isRemoved, whoIsParent;
// constructor
TreeCenteroid() { }
TreeCenteroid(const Graph &tree_) { init(tree_); }
// init
void init(const Graph &tree_) {
tree = tree_;
centroids.clear();
sizeSubtree.resize((int)tree.size());
whoIsParent.resize((int)tree.size());
isRemoved.assign((int)tree.size(), false);
for (int i = 0; i < (int)tree.size(); ++i) isRemoved[i] = false;
}
// subroutine
void sub_find_centroid(int v, int size, int p = -1) {
sizeSubtree[v] = 1;
whoIsParent[v] = p;
bool isCentroid = true;
for (auto ch : tree[v]) {
if (ch == p) continue;
if (isRemoved[ch]) continue;
sub_find_centroid(ch, size, v);
if (sizeSubtree[ch] > size / 2) isCentroid = false;
sizeSubtree[v] += sizeSubtree[ch];
}
if (size - sizeSubtree[v] > size / 2) isCentroid = false;
if (isCentroid) centroids.push_back(v);
}
// first: centroid, second: vectors of (adj-node, size of adj-tree)
pair<int, vector<pair<int,int>>> find_centroid(int root, int size) {
vector<pair<int, int>> subtrees;
centroids.clear();
sub_find_centroid(root, size);
int center = centroids[0];
//isRemoved[center] = true;
for (auto ch : tree[center]) {
if (isRemoved[ch]) continue;
if (ch == whoIsParent[center]) {
subtrees.push_back(make_pair(ch, size - sizeSubtree[center]));
}
else {
subtrees.push_back(make_pair(ch, sizeSubtree[ch]));
}
}
return make_pair(center, subtrees);
}
};
//------------------------------//
// Examples
//------------------------------//
bool cmp(pair<int,int> a, pair<int,int> b) {
swap(a.first, a.second);
swap(b.first, b.second);
return a > b;
}
void dwacon2018_prelims_E() {
// 入力
int N, Q;
cin >> N >> Q;
Graph tree(N);
for (int i = 0; i < N - 1; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
tree[a].push_back(b);
tree[b].push_back(a);
}
// 重心分解しながらクエリ処理
int curnode = 0, cursize = N, res;
TreeCenteroid tc(tree);
while (Q--) {
pair<int, vector<pair<int, int>>> c = tc.find_centroid(curnode, cursize);
int g = c.first;
vector<pair<int,int>> chs = c.second;
sort(chs.begin(), chs.end(), cmp);
if (chs.size() == 1) {
cout << "? " << g + 1 << " " << chs[0].first + 1 << endl;
int ans;
cin >> ans;
res = ans;
break;
} else {
cout << "? " << chs[0].first + 1 << " " << chs[1].first + 1 << endl;
int ans;
cin >> ans;
if (ans == chs[0].first + 1) {
tc.isRemoved[g] = true;
curnode = chs[0].first;
cursize = chs[0].second;
} else if (ans == chs[1].first + 1) {
tc.isRemoved[g] = true;
curnode = chs[1].first;
cursize = chs[1].second;
} else {
tc.isRemoved[chs[0].first] = true;
tc.isRemoved[chs[1].first] = true;
curnode = g;
cursize = cursize - chs[0].second - chs[1].second;
}
if (cursize == 1) {
res = curnode + 1;
break;
}
}
}
cout << "! " << res << endl;
}
int main() {
dwacon2018_prelims_E();
}