-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD_Lost_Tree.cpp
More file actions
63 lines (53 loc) · 1.3 KB
/
D_Lost_Tree.cpp
File metadata and controls
63 lines (53 loc) · 1.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
#include <bits/stdc++.h>
using namespace std;
vector<int> query(int n, int r) {
cout << "? " << r << endl;
vector<int> dist(n + 1);
for(int i=1;i<=n;i++) cin >> dist[i];
return dist;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
set<pair<int, int>> edges;
queue<int> q;
q.push(1);
vector<int> vis(n + 1);
while(!q.empty()) {
int r = q.front();
q.pop();
vis[r] = 1;
vector<int> dist = query(n, r);
int even = 0, odd = 0;
for(int i=1;i<=n;i++) {
if(dist[i] % 2 == 0) even++;
else odd++;
}
for(int i=1;i<=n;i++) {
if(dist[i] == 1) {
edges.insert({min(i, r), max(i, r)});
}
}
if(edges.size() == n - 1) break;
if(odd < even) {
for(int i=1;i<=n;i++) {
if(vis[i]) continue;
if(dist[i] % 2 == 1) q.push(i);
}
}
else {
for(int i=1;i<=n;i++) {
if(vis[i]) continue;
if(dist[i] % 2 == 0) q.push(i);
}
}
}
cout << "!" << endl;
for(auto &[u, v] : edges) {
cout << u << " " << v << endl;
}
return 0;
}