#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#define DIM 3005
using namespace std;
int n, i, j, tip, nr;
int a[DIM][DIM], r[DIM], viz[DIM], ff[DIM], c[DIM], niv[DIM];
vector<int> v[DIM];
pair<int, int> w[DIM];
int rad(int x){
while(r[x] > 0){
x = r[x];
}
return x;
}
void adauga(int x, int y){
int r1 = rad(x), r2 = rad(y);
if(r1 != r2){
v[x].push_back(y);
v[y].push_back(x);
if(r[r1] < r[r2]){
r[r1] += r[r2];
r[r2] = r1;
}
else{
r[r2] += r[r1];
r[r1] = r2;
}
}
}
void dfs(int nod){
viz[nod] = 1;
for(int i = 0; i < v[nod].size(); i++){
int vecin = v[nod][i];
if(viz[vecin] == 0){
niv[vecin] = 1 + niv[nod];
dfs(vecin);
}
}
}
void color(int nod, int num, int srs){
viz[nod] = 1;
if(nod != srs){
if(c[nod] == 0){
return;
}
ff[ c[nod] ]++;
if(ff[ c[nod] ] == 1){
num++;
}
if(a[srs][nod] == num && c[srs] == 0){
c[srs] = c[nod];
}
}
for(int i = 0; i < v[nod].size(); i++){
if(viz[ v[nod][i] ] == 0){
color(v[nod][i], num, srs);
}
}
ff[ c[nod] ]--;
}
int main(){
cin>> tip >> n;
for(i = 1; i <= n; i++){
for(j = 1; j <= n; j++){
cin>> a[i][j];
}
r[i] = -1;
}
for(i = 1; i <= n; i++){
for(j = i + 1; j <= n; j++){
if(a[i][j] == 1){
adauga(i, j);
}
}
}
for(i = 1; i <= n; i++){
for(j = i + 1; j <= n; j++){
if(a[i][j] == 2){
adauga(i, j);
}
}
}
dfs(1);
for(i = 1; i <= n; i++){
w[i] = make_pair(niv[i], i);
}
sort(w + 1, w + n + 1);
for(i = 1; i <= n; i++){
memset(viz, 0, sizeof(viz) );
memset(ff, 0, sizeof(ff) );
color(w[i].second, 0, w[i].second);
if(c[ w[i].second ] == 0){
c[ w[i].second ] = ++nr;
}
}
for(i = 1; i <= n; i++){
cout<< c[i] <<" ";
}
cout<<"\n";
for(i = 1; i < n; i++){
for(j = 0; j < v[i].size(); j++){
if(v[i][j] > i){
cout<< i <<" "<< v[i][j] <<"\n";
}
}
}
}
Compilation message
izlet.cpp: In function 'void dfs(int)':
izlet.cpp:34:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v[nod].size(); i++){
~~^~~~~~~~~~~~~~~
izlet.cpp: In function 'void color(int, int, int)':
izlet.cpp:56:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v[nod].size(); i++){
~~^~~~~~~~~~~~~~~
izlet.cpp: In function 'int main()':
izlet.cpp:103:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j = 0; j < v[i].size(); j++){
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Execution timed out |
2059 ms |
39872 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2040 ms |
38760 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Execution timed out |
2059 ms |
39872 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |