QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#306643 | #8055. Balance | superguymj | WA | 65ms | 17912kb | C++14 | 3.3kb | 2024-01-16 23:29:00 | 2024-01-16 23:29:01 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x; i<=y; ++i)
#define repd(i,x,y) for(int i=x; i>=y; --i)
#define mid ((l+r)>>1)
#define pb push_back
using namespace std;
const int N=100005;
int n,m,h[N],cnt,jdg;
int dfn[N],low[N],lim,inst[N],stk[N];
int tot,id[N],siz[N],Fa[N];
int f[N],g[N],pf[N],pg[N];
int vis[N],ans[N],L[N],R[N],c[N],k,a[N],sum[N];
struct edge{int v,n;} e[N<<2];
vector <int> cty[N],vt[N];
void init()
{
rep(i,0,n+1)
{
h[i]=-1,dfn[i]=low[i]=0;
inst[i]=stk[i]=0;
id[i]=siz[i]=Fa[i]=0;
f[i]=g[i]=pf[i]=pg[i]=0;
vis[i]=ans[i]=L[i]=R[i]=c[i]=sum[i]=0;
cty[i].clear(),vt[i].clear();
}
cnt=lim=tot=k=jdg=0;
}
int getint()
{
char ch;
while(!isdigit(ch=getchar()));
int x=ch-48;
while(isdigit(ch=getchar())) x=x*10+ch-48;
return x;
}
void addedge(int u,int v)
{
e[cnt]=(edge){v,h[u]},h[u]=cnt++;
e[cnt]=(edge){u,h[v]},h[v]=cnt++;
}
void tarjan(int x,int fa)
{
dfn[x]=low[x]=++lim;
inst[stk[++*stk]=x]=1;
for(int i=h[x]; i!=-1; i=e[i].n) if((i^1)!=fa)
{
int v=e[i].v;
if(!dfn[v]) tarjan(v,i),low[x]=min(low[x],low[v]);
else if(inst[v]) low[x]=min(low[x],dfn[v]);
}
if(dfn[x]==low[x])
{
++tot,cty[tot]={x},id[x]=tot,inst[x]=0;
while(stk[*stk]!=x) cty[tot].pb(stk[*stk]),id[stk[*stk]]=tot,inst[stk[(*stk)--]]=0;
--*stk;
}
}
void dfs(int x,int fa,int L[],int f[],int pf[])
{
siz[x]=cty[x].size(),Fa[x]=fa;
for(auto v:vt[x]) if(v!=fa)
{
dfs(v,x,L,f,pf),siz[x]+=siz[v];
if(f[pf[x]]<f[pf[v]])
pf[x]=pf[v];
}
int i=L[siz[x]];
if(i && f[pf[x]]+1==i) f[pf[x]=x]=i;
}
void findAns(int x,int l,int tp,int dec=1)
{
if(vis[x]) return;
vis[x]=1;
int cor=tp==1?c[l]:c[k-l+1];
for(auto v:cty[x]) ans[v]=cor;
if(l>1 && dec)
{
for(auto v:vt[x]) if(v!=Fa[x])
{
int p=tp==1?pf[v]:pg[v];
if(tp==1 && f[p]+1==l || tp==-1 && g[p]+1==l)
{
findAns(p,l-1,tp);
break;
}
}
}
for(auto v:vt[x]) if(v!=Fa[x]) findAns(v,l,tp,0);
}
void findLca(int x)
{
int Pf=0,Pg=0;
for(auto v:vt[x]) if(v!=Fa[x])
{
if(f[pf[v]]+g[Pg]==k-1)
{
jdg=1,findAns(pf[v],f[pf[v]],1),findAns(Pg,g[Pg],-1);
rep(i,1,n) if(ans[i]==0) ans[i]=c[f[pf[v]]+1];
return;
}
if(g[pg[v]]+f[Pf]==k-1)
{
jdg=1,findAns(Pf,f[Pf],1),findAns(pg[v],g[pg[v]],-1);
rep(i,1,n) if(ans[i]==0) ans[i]=c[f[Pf]+1];
return;
}
if(f[Pf]<f[pf[v]]) Pf=pf[v];
if(g[Pg]<g[pg[v]]) Pg=pg[v];
findLca(v);
}
}
void solve()
{
n=getint(),m=getint();
init();
rep(i,1,m) addedge(getint(),getint());
rep(i,1,n) a[i]=getint();
sort(a+1,a+1+n),k=0;
rep(i,1,n)
{
if(i==1 || a[i]!=a[i-1]) c[++k]=a[i];
++sum[k];
}
rep(i,1,n) if(!dfn[i]) tarjan(i,-1);
rep(x,1,n) for(int j=h[x]; j!=-1; j=e[j].n)
if(id[x]<id[e[j].v])
{
int u=id[x],v=id[e[j].v];
vt[u].pb(v),vt[v].pb(u);
}
int lst=0;
rep(i,1,k) lst+=sum[i],L[lst]=i;
reverse(sum+1,sum+1+k),lst=0;
rep(i,1,k) lst+=sum[i],R[lst]=i;
dfs(1,0,L,f,pf);
if(f[1]==k)
jdg=1,findAns(1,k,1);
else
{
dfs(1,0,R,g,pg);
if(g[1]==k)
jdg=1,findAns(1,k,-1);
else
findLca(1);
}
if(jdg)
{
puts("Yes");
rep(i,1,n) printf("%d ",ans[i]); puts("");
}
else puts("No");
}
int main()
{
int T=getint();
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15220kb
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 1 2 3 4 5 No Yes 2 1 2 2 3 Yes 2 2 1 1 1 No
result:
ok ok (5 test cases)
Test #2:
score: 0
Accepted
time: 47ms
memory: 17912kb
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 1 2 Yes 1 1 1 1 1 Yes 1 2 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 2 1 1 No Yes 1 1 No Yes 1 1 N...
result:
ok ok (100000 test cases)
Test #3:
score: 0
Accepted
time: 65ms
memory: 14952kb
input:
83335 9 17 1 4 8 9 5 2 1 3 2 7 1 7 2 8 6 7 2 4 1 8 5 8 7 1 8 6 4 6 4 7 6 9 7 9 7 3 4 4 7 4 2 4 8 6 9 3 1 1 2 3 5 1 2 3 4 4 5 6 3 6 1 6 2 4 3 2 2 1 3 9 8 9 3 5 7 5 9 2 6 1 8 4 1 4 2 4 3 4 2 5 3 4 5 4 5 4 6 7 2 1 1 5 6 1 3 1 6 5 2 4 5 3 2 1 2 1 2 1 4 6 2 1 4 2 1 4 1 2 1 4 3 1 2 2 4 2 6 10 2 3 3 5 2 1 ...
output:
No No Yes 4 3 4 4 5 2 5 4 5 No Yes 2 2 4 2 No Yes 3 3 2 3 Yes 2 1 2 No Yes 1 1 1 1 1 No Yes 1 1 1 Yes 1 1 3 3 3 Yes 1 1 Yes 1 1 Yes 1 1 1 1 Yes 3 1 3 No Yes 1 1 1 1 1 1 1 1 Yes 1 1 1 1 1 1 1 No Yes 1 1 No Yes 1 1 1 1 1 Yes 2 1 1 2 1 No Yes 1 2 3 1 3 3 3 1 No No No No No No No No No ...
result:
ok ok (83335 test cases)
Test #4:
score: -100
Wrong Answer
time: 64ms
memory: 15148kb
input:
58877 11 15 10 8 4 5 8 4 9 1 3 6 5 2 4 11 3 6 11 5 5 2 9 6 1 5 5 7 5 9 8 4 1 1 1 1 1 1 1 1 1 1 1 6 11 3 4 6 1 1 3 6 1 2 6 1 2 6 2 2 1 3 6 4 5 1 3 2 4 3 2 4 4 12 21 3 10 9 10 4 6 12 10 7 8 5 9 11 9 5 8 4 6 4 9 8 2 10 3 3 4 7 6 1 2 1 8 6 12 8 5 3 1 6 4 12 8 5 2 1 4 3 5 3 1 4 6 5 1 10 9 10 7 3 2 1 4 7 ...
output:
Yes 1 1 1 1 1 1 1 1 1 1 1 No No No No Yes 1 1 No No No Yes 1 1 1 1 No No No No No No Yes 1 1 1 1 1 No Yes 1 1 1 1 No No Yes 1 1 1 2 2 No No No No Yes 1 1 1 1 1 1 1 1 1 1 1 1 1 No No Yes 1 1 1 No No No No Yes 1 1 No Yes 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 No No No No No No No No No No Yes 1 1 No...
result:
wrong answer 11-th smallest numbers are not same (test case 29473)