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
|
const int MAXN = 2e5 + 3;
int interact(char oper, int u) {
printf("%c %d\n", oper, u);
fflush(stdout);
int res;
scanf("%d", &res);
return res;
}
vi adj[MAXN];
bool deleted[MAXN];
int fa[MAXN], size[MAXN], dep[MAXN], depx;
void dfs(int u) {
for (int v : adj[u]) {
if (v == fa[u]) {
continue;
}
fa[v] = u;
dep[v] = dep[u] + 1;
dfs(v);
}
}
int sub_size, root, root_key;
void get_root(int u, int from) {
int key = 0;
size[u] = 1;
for (int v : adj[u]) {
if (deleted[v] || v == from) {
continue;
}
get_root(v, u);
size[u] += size[v];
key = max(key, size[v]);
}
key = max(key, sub_size - key);
if (key < root_key) {
root = u;
root_key = key;
// debug("root = %d root_key = %d\n", root, root_key);
}
// debug("u = %d key = %d\n", u, key);
}
int gao(int u) {
if (size[u] == 1) {
return u;
}
sub_size = size[u];
root = u;
root_key = sub_size;
get_root(u, 0);
int dist = interact('d', root);
if (dist == 0) {
return root;
}
deleted[root] = true;
if (dep[root] + dist == depx) {
return gao(interact('s', root));
} else {
return gao(fa[root]);
}
}
int main() {
int n;
scanf("%d", &n);
FOR(i, 1, n) {
int u, v;
scanf("%d %d", &u, &v);
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1);
depx = interact('d', 1);
size[1] = n;
printf("! %d\n", gao(1));
return 0;
}
|