#include <bits/stdc++.h>
using namespace std;
const int N = 3e3 + 100;
int n, g, mat[N][N], par[N], col[N], cur_col;
set<int> reps;
vector<int> S[N];
vector<pair<int, int>> edges;
bool edge[N][N];
int root(int v){
return (par[v] == -1 ? v : par[v] = root(par[v]));
}
void merge(int u, int v){
if ((u = root(u)) == (v = root(v)))
return;
if (S[u].size() > S[v].size())
swap(u, v);
edges.push_back({u, v});
edge[u][v] = edge[v][u] = 1;
reps.erase(u);
par[u] = v;
for (int x : S[u]){
par[x] = v;
S[v].push_back(x);
}
S[u].clear();
}
int main(){
cin >> g >> n;
for (int i = 1; i <= n; i ++){
par[i] = -1, S[i] = {i};
reps.insert(i);
}
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
cin >> mat[i][j];
for (int i = 1; i <= n; i ++)
for (int j = i + 1; j <= n; j ++)
if (mat[i][j] == 1)
merge(i, j);
if (g == 1){
int p = *reps.begin();
for (int v : reps){
col[v] = (p == v) ? 1 : 2;
if (p != v) edges.push_back({p, v});
}
}
else{
vector<int> vec;
for (int x : reps)
vec.push_back(x);
col[vec[0]] = ++cur_col;
for (int i = 1; i < vec.size(); i ++){
edges.push_back({vec[i - 1], vec[i]});
bool done = 0;
for (int j = 0; j < i; j ++){
if (mat[vec[j]][vec[i - 1]] == mat[vec[j]][vec[i]]){
done = 1;
col[vec[i]] = col[vec[j]];
break;
}
}
if (!done)
col[vec[i]] = ++cur_col;
}
}
for (int v : reps)
for (int x : S[v])
col[x] = col[v];
for (int i = 1; i <= n; i ++)
cout << col[i] << " ";
cout << endl;
for (auto [u, v] : edges)
cout << u << " " << v << endl;
}
Compilation message
izlet.cpp: In function 'int main()':
izlet.cpp:59:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i = 1; i < vec.size(); i ++){
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
739 ms |
42188 KB |
Output is correct |
3 |
Correct |
951 ms |
38228 KB |
Output is correct |
4 |
Correct |
851 ms |
46536 KB |
Output is correct |
5 |
Correct |
740 ms |
46240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
871 ms |
46524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
739 ms |
42188 KB |
Output is correct |
3 |
Correct |
951 ms |
38228 KB |
Output is correct |
4 |
Correct |
851 ms |
46536 KB |
Output is correct |
5 |
Correct |
740 ms |
46240 KB |
Output is correct |
6 |
Incorrect |
871 ms |
46524 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |