#include <algorithm>
#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
typedef pair <int, int> pii;
const int N = 3e3 + 9;
int n, ind;
vector <pii> ans;
int par[N];
bool del[N];
int col[N];
int dis[N][N];
vector <int> G[N];
int cnt;
int mark[N];
int root (int v) {
return par[v] == v? v: par[v] = root(par[v]);
}
void dfs(int v, int p, int u) {
cnt += !mark[col[v]];
mark[col[v]]++;
if (dis[v][u] < cnt)
assert(false);
if (dis[v][u] > cnt + 1)
assert(false);
if (dis[v][u] == cnt) {
col[u] = min(col[u], col[v]);
mark[col[v]]--;
cnt -= !mark[col[v]];
return ;
}
for (int k: G[v])
if (k != p && col[k] != -1)
dfs(k, v, u);
mark[col[v]]--;
cnt -= !mark[col[v]];
}
void calc(int v, int p = -1) {
col[v] = ind;
if (p != -1)
dfs(p, v, v);
ind += col[v] == ind;
for (int u: G[v])
if (u != p)
calc(u, v);
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> dis[i][j];
for (int i = 0; i < n; i++)
par[i] = i, col[i] = -1;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (dis[i][j] == 1 && root(i) != root(j)) {
ans.push_back({root(i), root(j)});
del[root(j)] = true;
par[root(j)] = root(i);
}
for (int i = 0; i < n; i++)
if (del[i] == false)
for (int j = i + 1; j < n; j++)
if (del[j] == false && root(i) != root(j))
if (dis[i][j] == 2) {
ans.push_back({i, j});
G[i].push_back(j);
G[j].push_back(i);
par[root(j)] = root(i);
}
calc(0);
for (int i = 0; i < n; i++)
if (col[i] == -1)
col[i] = col[par[i]];
for (int i = 0; i < n; i++)
cout << col[i] + 1 << " ";
cout << "\n";
for (pii e: ans)
cout << e.first + 1 << " " << e.second + 1 << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
572 ms |
45024 KB |
Output is correct |
3 |
Correct |
621 ms |
45048 KB |
Output is correct |
4 |
Correct |
492 ms |
36472 KB |
Output is correct |
5 |
Correct |
585 ms |
36088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
566 ms |
36776 KB |
Output is correct |
2 |
Correct |
531 ms |
36284 KB |
Output is correct |
3 |
Correct |
806 ms |
43768 KB |
Output is correct |
4 |
Correct |
809 ms |
40088 KB |
Output is correct |
5 |
Correct |
538 ms |
44120 KB |
Output is correct |
6 |
Correct |
561 ms |
37880 KB |
Output is correct |
7 |
Correct |
445 ms |
32504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
572 ms |
45024 KB |
Output is correct |
3 |
Correct |
621 ms |
45048 KB |
Output is correct |
4 |
Correct |
492 ms |
36472 KB |
Output is correct |
5 |
Correct |
585 ms |
36088 KB |
Output is correct |
6 |
Correct |
566 ms |
36776 KB |
Output is correct |
7 |
Correct |
531 ms |
36284 KB |
Output is correct |
8 |
Correct |
806 ms |
43768 KB |
Output is correct |
9 |
Correct |
809 ms |
40088 KB |
Output is correct |
10 |
Correct |
538 ms |
44120 KB |
Output is correct |
11 |
Correct |
561 ms |
37880 KB |
Output is correct |
12 |
Correct |
445 ms |
32504 KB |
Output is correct |
13 |
Correct |
612 ms |
37848 KB |
Output is correct |
14 |
Correct |
791 ms |
37880 KB |
Output is correct |
15 |
Correct |
524 ms |
47736 KB |
Output is correct |
16 |
Correct |
609 ms |
37628 KB |
Output is correct |
17 |
Correct |
607 ms |
37752 KB |
Output is correct |
18 |
Correct |
562 ms |
50232 KB |
Output is correct |
19 |
Correct |
615 ms |
50040 KB |
Output is correct |
20 |
Correct |
564 ms |
50032 KB |
Output is correct |
21 |
Correct |
588 ms |
37496 KB |
Output is correct |
22 |
Correct |
631 ms |
41464 KB |
Output is correct |
23 |
Correct |
589 ms |
37496 KB |
Output is correct |
24 |
Correct |
624 ms |
37624 KB |
Output is correct |
25 |
Correct |
551 ms |
45704 KB |
Output is correct |