#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#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++){
scanf("%d", &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:35: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:57: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:104:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j = 0; j < v[i].size(); j++){
~~^~~~~~~~~~~~~
izlet.cpp:68:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i][j]);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
986 ms |
36788 KB |
Output is correct |
3 |
Correct |
978 ms |
43568 KB |
Output is correct |
4 |
Correct |
1045 ms |
42920 KB |
Output is correct |
5 |
Correct |
991 ms |
43172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
949 ms |
36640 KB |
Output is correct |
2 |
Correct |
1058 ms |
42592 KB |
Output is correct |
3 |
Correct |
1052 ms |
42076 KB |
Output is correct |
4 |
Correct |
1067 ms |
43400 KB |
Output is correct |
5 |
Correct |
934 ms |
41220 KB |
Output is correct |
6 |
Correct |
960 ms |
40500 KB |
Output is correct |
7 |
Correct |
695 ms |
34852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
986 ms |
36788 KB |
Output is correct |
3 |
Correct |
978 ms |
43568 KB |
Output is correct |
4 |
Correct |
1045 ms |
42920 KB |
Output is correct |
5 |
Correct |
991 ms |
43172 KB |
Output is correct |
6 |
Correct |
949 ms |
36640 KB |
Output is correct |
7 |
Correct |
1058 ms |
42592 KB |
Output is correct |
8 |
Correct |
1052 ms |
42076 KB |
Output is correct |
9 |
Correct |
1067 ms |
43400 KB |
Output is correct |
10 |
Correct |
934 ms |
41220 KB |
Output is correct |
11 |
Correct |
960 ms |
40500 KB |
Output is correct |
12 |
Correct |
695 ms |
34852 KB |
Output is correct |
13 |
Correct |
1029 ms |
39760 KB |
Output is correct |
14 |
Correct |
1082 ms |
61340 KB |
Output is correct |
15 |
Correct |
975 ms |
53640 KB |
Output is correct |
16 |
Correct |
1071 ms |
58120 KB |
Output is correct |
17 |
Correct |
1057 ms |
59644 KB |
Output is correct |
18 |
Correct |
985 ms |
53552 KB |
Output is correct |
19 |
Correct |
991 ms |
53496 KB |
Output is correct |
20 |
Correct |
970 ms |
53536 KB |
Output is correct |
21 |
Correct |
995 ms |
54388 KB |
Output is correct |
22 |
Correct |
990 ms |
53816 KB |
Output is correct |
23 |
Correct |
1027 ms |
54392 KB |
Output is correct |
24 |
Correct |
1037 ms |
60552 KB |
Output is correct |
25 |
Correct |
981 ms |
53752 KB |
Output is correct |