This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3010;
int n, a[maxn][maxn], fa[maxn];
vector<int> G[maxn];
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> *new int >> n;
iota(fa + 1, fa + n + 1, 1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
for (int x : {1, 2}) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (a[i][j] == x && find(i) ^ find(j)) {
fa[find(i)] = find(j);
G[i].push_back(j), G[j].push_back(i);
}
}
}
}
iota(fa + 1, fa + n + 1, 1);
for (int i = 1; i <= n; i++) {
for (int j : G[i]) {
auto dfs = [&](auto self, int u, int p) -> void {
int x = (u == j ? 0 : a[j][p]) + 1;
if (a[i][u] == x && a[i][p] == x && a[j][u] == x) {
fa[find(i)] = find(u);
}
for (int v : G[u]) {
if (v ^ p) self(self, v, u);
}
};
dfs(dfs, j, i);
}
}
for (int i = 1; i <= n; i++) {
cout << find(i) << " \n"[i == n];
}
for (int i = 1; i <= n; i++) {
for (int j : G[i]) if (i < j) {
cout << i << " " << j << "\n";
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |