-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathF.cpp
92 lines (81 loc) · 2.35 KB
/
F.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
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::vector f(n, std::vector<std::string>(n));
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
std::cin >> f[i][j];
f[j][i] = f[i][j];
}
}
for (int x = 1; x < n; x++) {
std::vector<bool> vis(n);
vis[0] = true;
vis[x] = true;
std::vector<std::pair<int, int>> edges;
edges.emplace_back(0, x);
auto dfs = [&](auto self, int u, int v) -> void {
for (int i = 0; i < n; i++) {
if (vis[i] || f[i][u][v] == '0') {
continue;
}
vis[i] = true;
edges.emplace_back(v, i);
self(self, v, i);
}
};
dfs(dfs, 0, x);
dfs(dfs, x, 0);
if (edges.size() != n - 1) {
continue;
}
std::vector<std::vector<int>> adj(n);
for (auto [u, v] : edges) {
adj[u].push_back(v);
adj[v].push_back(u);
}
std::vector dis(n, std::vector<int>(n));
for (int i = 0; i < n; i++) {
auto dfs = [&](auto self, int v, int p) -> void {
for (auto u : adj[v]) {
if (u == p) {
continue;
}
dis[i][u] = dis[i][v] + 1;
self(self, u, v);
}
};
dfs(dfs, i, -1);
}
bool ok = true;
for (int x = 0; x < n; x++) {
for (int y = x + 1; y < n; y++) {
for (int z = 0; z < n; z++) {
if ((f[x][y][z] == '1') != (dis[x][z] == dis[y][z])) {
ok = false;
}
}
}
}
if (ok) {
std::cout << "Yes\n";
for (auto [u, v] : edges) {
std::cout << u + 1 << " " << v + 1 << "\n";
}
return;
}
}
std::cout << "No\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}