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<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 (stderr)
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]);
~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |