QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#330774 | #8055. Balance | ucup-team1303# | WA | 93ms | 11020kb | C++14 | 2.6kb | 2024-02-17 18:55:35 | 2024-02-17 18:55:36 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int fa[301000];
int F(int x){
if(x==fa[x])return x;
return fa[x]=F(fa[x]);
}
vector<int>g[301000];
int n,m,ff[301000],d[301000],U[300100],V[301000],sz[301000];
bool od[301000];
void dfs(int x){sz[x]=1;for(int v:g[x])if(v!=ff[x])ff[v]=x,d[v]=d[x]+1,dfs(v),sz[x]+=sz[v];}
int a[301000];
int r1[301000],r2[301000],b1[301000],b2[301000],e,c1[300100],c2[301000];
int p1[300100],p2[300100];
int t1[301000],t2[301000];
int cg(int a,int b){return (sz[a]<sz[b])?b:a;}
void dfs2(int x){
b1[x]=b2[x]=0;
for(int v:g[x])if(v!=ff[x]){
dfs2(v);
b1[x]=cg(b1[x],b1[v]);
b2[x]=cg(b2[x],b2[v]);
}
if(x!=F(x))return;
int s=sz[x];
if(r1[s]&&(r1[s]==1||sz[b1[x]]==p1[r1[s]-1]))t1[x]=(r1[s]==1)?-1:b1[x],b1[x]=x;
if(r2[s]&&(r2[s]==1||sz[b2[x]]==p2[r2[s]-1]))t2[x]=(r2[s]==1)?-1:b2[x],b2[x]=x;
}
int col[301000],gs[301000];
void cos(int x,int z){
if(col[x])return;col[x]=z;
for(int v:g[x])if(v!=ff[x])cos(v,z);
}
void pain(int z,int x){
int om=((z==1)?r1[sz[x]]:r2[sz[x]]),S=om;
while(x!=-1)gs[om--]=x,x=(z==1)?t1[x]:t2[x];
for(int i=1;i<=S;i++)cos(gs[i],(z==1)?c1[i]:c2[i]);
}
#define pb push_back
void print(){
puts("Yes");
for(int i=1;i<=n;i++)printf("%d ",col[i]);puts("");
}
void sol(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)col[i]=0,fa[i]=i,g[i].clear(),r1[i]=r2[i]=b1[i]=b2[i]=t1[i]=t2[i]=p1[i]=p2[i]=c1[i]=c2[i]=0;
for(int i=1,u,v;i<=m;i++){
scanf("%d%d",&u,&v),U[i]=u,V[i]=v;int a=F(u),b=F(v);
if(a!=b)g[u].pb(v),g[v].pb(u),od[i]=1,fa[a]=b;
else od[i]=0;
}
dfs(1);
for(int i=1;i<=n;i++)fa[i]=i;
for(int i=1;i<=m;i++)if(!od[i]){
int u=F(U[i]),v=F(V[i]);
while(u!=v){
if(d[u]<d[v])swap(u,v);
fa[u]=F(ff[u]);
u=fa[u];
}
}
for(int i=1;i<=n;i++)scanf("%d",&a[i]);sort(a+1,a+n+1);
e=0;
for(int i=1;i<=n;i++)if(i==n||a[i]!=a[i+1])e++,r1[i]=e,p1[e]=i,c1[e]=a[i];
reverse(a+1,a+n+1);
e=0;
for(int i=1;i<=n;i++)if(i==n||a[i]!=a[i+1])e++,r2[i]=e,p2[e]=i,c2[e]=a[i];
dfs2(1);
if(b1[1]==1){pain(1,1);print();return;}
if(b2[1]==1){pain(2,1),print();return;}
for(int i=1;i<=n;i++){
int me=0;
for(int v:g[i])if(v!=fa[i]){
if(me&&b2[v]&&r1[sz[me]]+r2[sz[b2[v]]]==e-1){
pain(1,me);
pain(2,b2[v]);
cos(1,c1[r1[sz[me]]+1]);
print();return;
}
me=cg(me,b1[v]);
}
reverse(g[i].begin(),g[i].end()),me=0;
for(int v:g[i])if(v!=fa[i]){
if(me&&b2[v]&&r1[sz[me]]+r2[sz[b2[v]]]==e-1){
pain(1,me);
pain(2,b2[v]);
cos(1,c1[r1[sz[me]]+1]);
print();return;
}
me=cg(me,b1[v]);
}
}
puts("No");
}
int main(){
int T;scanf("%d",&T);while(T--)sol();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 11020kb
input:
5 5 4 1 2 2 3 3 4 4 5 1 2 3 4 5 5 4 1 2 1 3 1 4 1 5 1 2 3 4 5 5 4 1 2 1 3 1 4 1 5 1 2 2 2 3 5 6 1 2 1 2 2 3 3 4 4 5 3 5 1 2 1 2 1 2 2 1 2 1 2 1 2
output:
Yes 5 4 3 2 1 No Yes 2 1 3 2 2 Yes 2 2 1 1 1 No
result:
ok ok (5 test cases)
Test #2:
score: -100
Wrong Answer
time: 93ms
memory: 11016kb
input:
100000 4 3 4 2 1 3 2 1 2 1 3 2 5 7 2 5 5 3 2 3 5 1 4 3 2 4 4 3 1 3 1 1 2 3 2 2 1 2 3 1 1 1 4 7 3 4 1 4 2 3 4 3 4 2 3 1 4 3 2 3 3 2 4 6 2 4 3 1 1 2 1 2 2 3 4 2 1 1 3 3 4 7 3 2 4 2 1 2 3 4 3 2 2 4 3 4 2 1 1 1 5 5 3 2 1 5 4 5 4 3 2 1 1 2 2 3 2 3 5 2 3 1 3 3 1 3 1 2 1 3 2 3 2 3 2 1 2 1 1 2 1 1 2 3 2 1 1...
output:
Yes 2 2 1 3 No Yes 1 1 1 No No Yes 2 1 1 1 No No Yes 1 1 Yes 1 1 Yes 1 1 Yes 1 1 1 1 No Yes 1 1 1 1 1 Yes 1 3 1 1 1 Yes 1 1 1 Yes 2 1 Yes 1 1 1 1 1 Yes 2 1 No Yes 1 1 Yes 1 1 1 Yes 1 1 Yes 1 1 1 1 Yes 1 1 Yes 2 2 2 2 2 Yes 1 1 1 1 1 Yes 1 1 Yes 1 1 1 2 No Yes 1 1 No Yes 1 1 N...
result:
wrong answer 4-th smallest numbers are not same (test case 210)