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<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;
vector<int>no(3003);
vector<int>tree[3003],T[3003];
vector<int>color(3003,-1);
vector<vector<int>>adj(3003,vector<int>(3003));
int F(int x){
if(no[x]==x)return x;
return no[x]=F(no[x]);
}
bool merge(int x,int y){
x=F(x);
y=F(y);
if(x==y)return false;
if(x<y)swap(x,y);
no[x]=y;
return true;
}
int dfs(int x,int pre,int X,set<int>& S){
int cnt=-1;
for(int i=0;i<T[x].size();i++){
int y=T[x][i];
if(y==pre)continue;
bool added=S.insert(color[y]).second;
if(adj[X][y]!=S.size()){
return color[y];
}
else {
cnt=max(dfs(y,x,X,S),cnt);
if(cnt>0)return cnt;
}
if(added)
S.erase(color[y]);
}
return cnt;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("q.gir","r",stdin);
// freopen("q.cik","w",stdout);
int k;
cin>>k;
int n;
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>adj[i][j];
}
}
for(int i=0;i<n;i++)no[i]=i;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j)continue;
if(adj[i][j]==1){
if(merge(i,j)){
tree[i].pb(j);
tree[j].pb(i);
}
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(adj[i][j]==2){
if(merge(i,j)){
tree[i].pb(j);
tree[j].pb(i);
}
}
}
}
color[0]=1;
queue<int>Q;
Q.push(0);
int ind=2;
while(!Q.empty()){
int x=Q.front();
Q.pop();
for(int i=0;i<tree[x].size();i++){
int y=tree[x][i];
if(color[y]<0){
Q.push(y);
T[x].pb(y);
T[y].pb(x);
set<int>S;S.insert(-1);
color[y]=dfs(y,-1,y,S);
if(color[y]==-1)color[y]=ind++;
}
}
}
for(int i=0;i<n;i++)cout<<color[i]<<" ";cout<<"\n";
for(int i=0;i<n;i++){
for(int j=0;j<tree[i].size();j++)if(i<tree[i][j])cout<<i+1<<" "<<tree[i][j]+1<<"\n";
}
}
Compilation message (stderr)
izlet.cpp: In function 'long long int dfs(long long int, long long int, long long int, std::set<long long int>&)':
izlet.cpp:29:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<T[x].size();i++){
~^~~~~~~~~~~~
izlet.cpp:33:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(adj[X][y]!=S.size()){
izlet.cpp: In function 'int32_t main()':
izlet.cpp:88:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<tree[x].size();i++){
~^~~~~~~~~~~~~~~
izlet.cpp:100:7: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for(int i=0;i<n;i++)cout<<color[i]<<" ";cout<<"\n";
^~~
izlet.cpp:100:47: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for(int i=0;i<n;i++)cout<<color[i]<<" ";cout<<"\n";
^~~~
izlet.cpp:102:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<tree[i].size();j++)if(i<tree[i][j])cout<<i+1<<" "<<tree[i][j]+1<<"\n";
~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |