QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#422201 | #8055. Balance | Eric_cai | WA | 82ms | 76724kb | C++14 | 3.8kb | 2024-05-26 22:59:45 | 2024-05-26 22:59:45 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
vector<int> g[maxn],tr[maxn];
int n,m,num[maxn],sump[maxn],sums[maxn];
int dfn[maxn],low[maxn],times;
stack<int> st;
int dot,sz[maxn],id[maxn];
void tarjan(int now,int fa)
{
st.push(now);
dfn[now]=low[now]=++times;
int fl=0,tp;
for(int i=0;i<g[now].size();i++)
{
if(g[now][i]==fa && fl==0)
{
fl=1;
continue;
}
if(dfn[g[now][i]]==0)
{
tarjan(g[now][i],now);
low[now]=min(low[now],low[g[now][i]]);
}
else low[now]=min(low[now],dfn[g[now][i]]);
}
if(low[now]>dfn[fa])
{
dot++;
while(!st.empty())
{
tp=st.top();
st.pop();
sz[dot]++,id[tp]=dot;
if(tp==now) break;
}
}
}
int tpre[maxn],tsuf[maxn],cnt;
int f[maxn][2],fr[maxn][2],a[maxn],h[maxn][2],gr[maxn][2];
void dfs(int now,int fa)
{
for(int i=0;i<tr[now].size();i++)
{
if(tr[now][i]==fa) continue;
dfs(tr[now][i],now);
sz[now]+=sz[tr[now][i]];
}
if(tpre[sz[now]]==1) f[now][0]=1;
if(tsuf[sz[now]]==1) f[now][1]=1;
for(int i=0;i<tr[now].size();i++)
{
if(tr[now][i]==fa) continue;
if(h[now][0]<h[tr[now][i]][0])
h[now][0]=h[tr[now][i]][0],gr[now][0]=gr[tr[now][i]][0];
if(h[now][1]<h[tr[now][i]][1])
h[now][1]=h[tr[now][i]][1],gr[now][1]=gr[tr[now][i]][1];
if(tpre[sz[now]]==h[tr[now][i]][0]+1)
f[now][0]=tpre[sz[now]],fr[now][0]=gr[tr[now][i]][0];
if(tsuf[sz[now]]==h[tr[now][i]][1]+1)
f[now][1]=tsuf[sz[now]],fr[now][1]=gr[tr[now][i]][1];
}
if(h[now][0]<f[now][0]) h[now][0]=f[now][0],gr[now][0]=now;
if(h[now][1]<f[now][1]) h[now][1]=f[now][1],gr[now][1]=now;
//cout<<now<<' '<<fa<<' '<<sz[now]<<' '<<f[now][0]<<' '<<fr[now][0]<<' '<<f[now][1]<<' '<<fr[now][1]<<'\n';
}
int prt[maxn],val[maxn],w[maxn];
void get_w(int now,int fa)
{
if(w[now]==0) w[now]=w[fa];
for(int i=0;i<tr[now].size();i++)
if(tr[now][i]!=fa) get_w(tr[now][i],now);
}
void clear()
{
while(!st.empty()) st.pop();
cnt=dot=times=0;
for(int i=1;i<=n;i++)
{
g[i].clear(),tr[i].clear();
num[i]=sump[i]=sums[i]=0;
dfn[i]=low[i]=sz[i]=id[i]=0;
tpre[i]=tsuf[i]=f[i][0]=f[i][1]=fr[i][0]=fr[i][1]=0;
a[i]=prt[i]=val[i]=w[i]=h[i][0]=h[i][1]=gr[i][0]=gr[i][1]=0;
}
}
int main()
{
int u,v,T,s0,s1,pos,d;
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
for(int i=1;i<=n;i++)
{
cin>>a[i];
num[a[i]]++;
}
for(int i=1;i<=n;i++)
{
sump[i]=sump[i-1]+num[i];
if(num[i]!=0)
{
tpre[sump[i]]=++cnt;
val[cnt]=i;
}
}
cnt=0;
for(int i=n;i>=1;i--)
{
sums[i]=sums[i+1]+num[i];
if(num[i]!=0) tsuf[sums[i]]=++cnt;
}
tarjan(1,0);
for(int now=1;now<=n;now++)
for(int i=0;i<g[now].size();i++)
if(id[now]!=id[g[now][i]]) tr[id[now]].push_back(id[g[now][i]]);
dfs(1,0);
for(int i=0;i<=n;i++) tpre[i]=tsuf[i]=0;
tpre[0]=tsuf[n+1]=-1,s0=s1=0;
for(int i=1;i<=n;i++) f[i][1]=cnt-f[i][1]+1;
for(int i=0;i<tr[1].size();i++)
{
u=tr[1][i];
if(f[u][0]!=0 && tsuf[f[u][0]+2]!=0) s0=u,s1=tsuf[f[u][0]+2],d=f[u][0]+1;
if(f[u][1]>=2 && tpre[f[u][1]-2]!=0) s0=tpre[f[u][1]-2],s1=u,d=f[u][1]-1;
tpre[f[u][0]]=tsuf[f[u][1]]=tr[1][i];
}
if(s0==0 && s1==0 && cnt>1) cout<<"No\n";
else
{
cout<<"Yes\n";
if(cnt==1)
{
for(int i=1;i<=n;i++) cout<<val[1]<<' ';
cout<<'\n';
clear();
continue;
}
if(s0==-1) s0=0;
if(s1==-1) s1=0;
w[1]=d;
pos=d-1;
while(s0!=0)
{
w[s0]=val[pos];
s0=fr[s0][0],pos--;
}
pos=d+1;
while(s1!=0)
{
w[s1]=val[pos];
s1=fr[s1][1],pos++;
}
get_w(1,0);
for(int i=1;i<=n;i++) cout<<w[id[i]]<<' ';
cout<<'\n';
}
clear();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 11ms
memory: 76196kb
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 2 2 3 Yes 2 2 1 1 1 No
result:
ok ok (5 test cases)
Test #2:
score: -100
Wrong Answer
time: 82ms
memory: 76724kb
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 No 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 No No Yes 1 1 No Yes 1 1 No No No No No No No No...
result:
wrong answer ans finds an answer, but out doesn't (test case 15)