-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD.cpp
247 lines (213 loc) · 5.13 KB
/
D.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
03/03/2020
islingr
*/
#include <cassert>
#include <cmath>
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <complex>
#include <chrono>
#include <random>
#include <utility>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front
#define bk back
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define rep(i, a, b) for (auto i = (a); i < (b); ++i) // [a, b)
#define per(i, a, b) for (auto i = (b) - 1; i >= (a); --i) // (b, a]
#define trav(e, x) for (auto &e : x)
template<class T>
bool ckmin(T& a, const T& b) {
return a > b ? a = b, 1 : 0;
}
template<class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template <class T> using ost = tree<T, null_type, less<T>,
rb_tree_tag, tree_order_statistics_node_update>;
#define ook order_of_key
#define fbo find_by_order
namespace helper {
template<class T, class = decltype(begin(declval<T>()))>
inline constexpr auto is_iterable(int) { return true_type{}; }
template<class>
inline constexpr auto is_iterable(unsigned) { return false_type{}; }
}
namespace input {
void re(string &s) { cin >> s; }
template<class T> void re(T& x);
template<class P, class Q> void re(pair<P, Q>&);
template<class T, class... Ts> void re(T& t, Ts&... ts);
template<class T> void re(T& x, false_type) { cin >> x; }
template<class T> void re(T& x, true_type) {
for (auto &v : x) re(v);
}
template<class T> void re(T& x) {
re(x, helper::is_iterable<T>(0));
}
template<class P, class Q>
void re(pair<P, Q> &p) {
re(p.first, p.second);
}
template<class T, class... Ts> void re(T& t, Ts&... ts) {
re(t);
re(ts...);
}
}
namespace output {
template <typename T> void pr(const T&);
template<class T, class... Ts> void pr(const T&, const Ts&...);
template<class S, class T>
void pr(const pair<S, T> &p) {
pr("(", p.first, ", ", p.second, ")");
}
template<class T>
void pr(queue<T> q) {
pr("{"); bool c = false;
while (!q.empty())
pr(c ? ", " : "", q.front()),
q.pop(),
c = true;
pr("}");
}
template<class T>
void pr(priority_queue<T> q) {
pr("{"); bool c = false;
while (!q.empty())
pr(c ? ", " : "", q.top()),
q.pop(),
c = true;
pr("}");
}
template<class T>
void pr(stack<T> s) {
pr("{"); bool c = false;
while (!s.empty())
pr(c ? ", " : "", s.top()),
s.pop(),
c = true;
pr("}");
}
void pr(const string &s) { cout << s; }
template<typename T>
void pr(const T& x, false_type) { cout << x; }
template<typename T>
void pr(const T& x, true_type) {
pr("{"); bool f = false;
for (const auto &v : x)
pr(f ? ", " : "", v),
f = true;
pr("}");
}
template <typename T>
void pr(const T& x) {
pr(x, helper::is_iterable<T>(0));
}
template<class T, class... Ts>
void pr(const T& t, const Ts&... ts) { pr(t); pr(ts...); }
void pc() { pr("]\n"); } // debug w/ commas
template<class T, class... Ts>
void pc(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(", ");
pc(ts...);
}
#define dbg(x...) pr("[", #x, "] = ["), pc(x);
}
struct IO {
IO(bool b) {
if (b) fastio();
cout << fixed << setprecision(numeric_limits<ld>::digits10);
}
IO(bool b, string s) : IO(b) {
setIn(s + ".in");
setOut(s + ".out");
}
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }
};
using namespace input;
using namespace output;
IO io = IO(false);
const int N = 1 << 10;
vi p, deg;
vi g[N];
bool vis[N];
int ask(int u, int v) {
++u; ++v;
cout << "? " << u << " " << v << endl; cout.flush();
int l; cin >> l;
assert(l != -1);
--l;
return l;
}
signed main() {
int n; re(n);
deg.rsz(n, 0);
rep(i, 1, n) {
int u, v; cin >> u >> v; --u; --v;
g[u].pb(v);
g[v].pb(u);
++deg[u]; ++deg[v];
}
int r = -1;
while (true) {
int u = -1, v = -1;
rep(i, 0, n) if (!vis[i] && deg[i] == 1) u = i;
per(i, 0, n) if (!vis[i] && deg[i] == 1) v = i;
if (u == -1) {
rep(x, 0, n) if (!vis[x]) r = x;
break;
}
if (u == v) {
r = u;
break;
}
int w = ask(u, v);
if (w == u || w == v) {
r = w;
break;
}
trav(v, g[u]) --deg[v];
trav(u, g[v]) --deg[u];
vis[u] = vis[v] = true;
}
cout << "! " << ++r << endl; cout.flush();
return 0;
}