QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#43542 | #4243. Good Coloring | crzsty | WA | 106ms | 9252kb | C++ | 1.4kb | 2022-08-09 18:15:58 | 2022-08-09 18:16:01 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int M=1e5+10;
int n,m,k;
int c[M],had[M];
vector <int> G[M],C[M];
int u,v,vis[M];
void dfs(int a){
if(c[a]==1){
printf("%d ",a);
return;
}
for(int u:G[a]){
if(c[u]==c[a]-1){
dfs(u);
break;
}
}
printf("%d ",a);
}
int bfs(int x){
queue <int> Q;
Q.push(x);
c[x]=1;
if(Q.empty()) return 0;
int nowcol=1;
while(!Q.empty()){
int a=Q.front();Q.pop();
had[a]=1;
if(c[a]>nowcol)nowcol=c[a];
vis[c[a]]=1;
for(int v:G[a]){
if(c[v]>=c[a]){
c[v]=c[a]+1;
Q.push(v);
}
}
}
return nowcol;
}
int main(){
int T;
for(scanf("%d",&T);T;T--){
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=k;i++)vis[i]=had[i]=0,C[i].clear();
for(int i=1;i<=n;i++){
scanf("%d",&c[i]);
G[i].clear();
C[c[i]].push_back(i);
}
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
int ans=0;
for(int i=1;i<=n;i++){
if(!had[i]){
ans=max(ans,bfs(i));
}
}
printf("%d ",ans);
for(int i=1;i<=n;i++){
printf("%d ",c[i]);
} printf("\n");
for(int i=1;i<=n;i++){
if(c[i]==ans){
dfs(i);
break;
}
} printf("\n");
}
return 0;
}
/*
2
3 3 3
1 2 3
1 2
2 3
3 1
3 1 3
1 2 3
1 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 9252kb
input:
2 3 3 3 1 2 3 1 2 2 3 3 1 3 1 3 1 2 3 1 2
output:
3 1 2 3 1 2 3 2 1 2 1 1 2
result:
ok good job (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 106ms
memory: 8584kb
input:
50000 5 7 5 5 2 3 1 5 4 3 3 1 5 2 5 4 5 3 2 4 4 1 6 5 5 1 1 4 5 5 2 2 3 3 5 6 1 2 4 1 5 7 5 6 4 6 3 5 6 5 3 5 4 5 6 7 2 1 2 3 6 7 7 4 3 4 2 1 3 4 1 6 7 3 4 3 6 3 5 6 1 2 3 2 4 5 6 5 3 3 4 4 1 4 5 2 3 5 2 3 1 4 2 5 1 6 6 3 2 3 2 2 1 1 2 6 3 5 2 5 6 3 4 5 4 6 7 5 2 1 2 1 1 2 2 1 5 4 5 3 7 5 1 2 3 2 7 ...
output:
4 1 1 4 3 2 2 5 4 3 3 1 1 2 2 3 2 2 3 5 4 1 2 1 4 3 2 3 3 6 5 4 4 1 3 2 1 3 3 1 4 1 3 2 4 2 1 3 2 4 3 1 1 3 3 2 2 2 5 3 2 1 2 1 1 2 2 1 1 2 4 1 1 1 4 2 3 1 1 5 6 4 3 1 2 2 2 3 1 2 5 3 1 3 3 2 2 3 1 4 2 4 1 2 2 3 1 3 3 1 1 2 1 2 2 3 1 3 7 5 1 3 5 2 4 3 6 1 4 2 5 3 4 1 1 2 4 ...
result:
wrong answer invalid path: there's no edge (4, 1) (test case 4)