QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#356196 | #8055. Balance | zhouhuanyi | RE | 0ms | 0kb | C++14 | 5.0kb | 2024-03-17 16:39:11 | 2024-03-17 16:39:11 |
answer
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define N 400000
using namespace std;
const int inf=(int)(1e9);
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
struct reads
{
int num,data;
};
int T,n,m,cater,sx,sy,dst,fa[N+1],ans[N+1],cl[N+1],sz[N+1],belong[N+1],rt[N+1],dfn[N+1],low[N+1],leng,a[N+1],tong[N+1],length,num[N+1],num2[N+1],dp[N+1],dp2[N+1],rk[N+1],rk2[N+1];
bool used[N+1],cut[N+1];
vector<reads>E[N+1];
vector<int>ES[N+1];
vector<int>v[N+1];
vector<int>st[N+1];
int find(int x)
{
if (rt[x]==x) return x;
return rt[x]=find(rt[x]);
}
void unionn(int x,int y)
{
rt[x]=y;
return;
}
void add(int x,int y,int z)
{
E[x].push_back((reads){y,z}),E[y].push_back((reads){x,z});
return;
}
void add_edge(int x,int y)
{
ES[x].push_back(y),ES[y].push_back(x);
return;
}
void tarjan(int x,int pv)
{
dfn[x]=low[x]=++leng;
for (int i=0;i<E[x].size();++i)
{
if (!dfn[E[x][i].num])
{
tarjan(E[x][i].num,E[x][i].data),low[x]=min(low[x],low[E[x][i].num]);
if (low[E[x][i].num]>dfn[x]) cut[E[x][i].data]=1;
}
else if (E[x][i].data!=pv) low[x]=min(low[x],dfn[E[x][i].num]);
}
return;
}
void dfs(int x)
{
used[x]=1,sz[x]=v[x].size();
for (int i=0;i<ES[x].size();++i)
if (!used[ES[x][i]])
{
if (x==1) cl[ES[x][i]]=ES[x][i];
else cl[ES[x][i]]=cl[x];
fa[ES[x][i]]=x,dfs(ES[x][i]),sz[x]+=sz[ES[x][i]],dp[x]=max(dp[x],dp[ES[x][i]]),dp2[x]=min(dp2[x],dp2[ES[x][i]]);
}
if (num[sz[x]]&&dp[x]+1>=num[sz[x]]) dp[x]=max(dp[x],num[sz[x]]),rk[x]=num[sz[x]];
if (num2[sz[x]]&&dp2[x]-1<=num2[sz[x]]) dp2[x]=min(dp2[x],num2[sz[x]]),rk2[x]=num2[sz[x]];
return;
}
void dfs2(int x,int d)
{
for (int i=0;i<v[x].size();++i) ans[v[x][i]]=a[tong[d]];
for (int i=0;i<ES[x].size();++i)
if (fa[ES[x][i]]==x)
dfs2(ES[x][i],d);
return;
}
void dfs3(int x,int d)
{
bool op=(d!=1);
for (int i=0;i<v[x].size();++i) ans[v[x][i]]=a[tong[d]];
for (int i=0;i<ES[x].size();++i)
if (fa[ES[x][i]]==x)
{
if (dp[ES[x][i]]==d-1&&op) dfs3(ES[x][i],d-(rk[ES[x][i]]==d-1)),op=0;
else dfs2(ES[x][i],d);
}
return;
}
void dfs4(int x,int d)
{
bool op=(d!=length);
for (int i=0;i<v[x].size();++i) ans[v[x][i]]=a[tong[d]];
for (int i=0;i<ES[x].size();++i)
if (fa[ES[x][i]]==x)
{
if (dp2[ES[x][i]]==d+1&&op) dfs4(ES[x][i],d+(rk2[ES[x][i]]==d+1)),op=0;
else dfs2(ES[x][i],d);
}
return;
}
void dfs5(int x,int d)
{
if (rk[x]==d)
{
dfs3(x,d);
return;
}
for (int i=0;i<ES[x].size();++i)
if (fa[ES[x][i]]==x&&dp[ES[x][i]]==d)
{
dfs5(ES[x][i],d);
return;
}
return;
}
void dfs6(int x,int d)
{
if (rk2[x]==d)
{
dfs4(x,d);
return;
}
for (int i=0;i<ES[x].size();++i)
if (fa[ES[x][i]]==x&&dp2[ES[x][i]]==d)
{
dfs6(ES[x][i],d);
return;
}
return;
}
int main()
{
freopen("I.in","r",stdin);
freopen("I.out","w",stdout);
int x,y,maxn,maxer,minn,miner;
T=read();
while (T--)
{
n=read(),m=read(),leng=length=cater=sx=sy=dst=0;
for (int i=1;i<=n;++i) E[i].clear(),dfn[i]=low[i]=num[i]=num2[i]=0,rt[i]=i,st[i].clear();
for (int i=1;i<=m;++i) x=read(),y=read(),add(x,y,i),cut[i]=0;
tarjan(1,0);
for (int i=1;i<=n;++i) a[i]=read();
sort(a+1,a+n+1);
for (int i=1;i<=n;++i)
if (i==n||a[i]!=a[i+1])
tong[++length]=i;
for (int i=1;i<=length;++i) num[tong[i]]=i,num2[n-tong[i-1]]=i;
for (int i=1;i<=n;++i)
for (int j=0;j<E[i].size();++j)
if (i<E[i][j].num&&!cut[E[i][j].data]&&find(i)!=find(E[i][j].num))
unionn(find(i),find(E[i][j].num));
for (int i=1;i<=n;++i)
if (find(i)==i)
belong[i]=++cater;
for (int i=1;i<=cater;++i) ES[i].clear(),v[i].clear(),used[i]=dp[i]=rk[i]=fa[i]=0,dp2[i]=rk2[i]=length+1;
for (int i=1;i<=n;++i) v[belong[find(i)]].push_back(i);
for (int i=1;i<=n;++i)
for (int j=0;j<E[i].size();++j)
if (i<E[i][j].num&&cut[E[i][j].data])
add_edge(belong[find(i)],belong[find(E[i][j].num)]);
dfs(1);
if (length==1) sx=sy=cater+1,dst=1;
for (int i=2;i<=cater;++i)
{
if (rk[i]==length-1) sx=i,sy=cater+1,dst=length;
if (rk2[i]==2) sx=cater+1,sy=i,dst=1;
}
if (!sx)
{
for (int i=1;i<=cater;++i)
{
maxn=-inf,minn=inf,maxer=miner=0;
for (int j=0;j<ES[i].size();++j)
if (fa[ES[i][j]]==i)
{
if (maxn+2>=dp2[ES[i][j]]) sx=maxer,sy=ES[i][j],dst=maxn+1;
if (minn-2<=dp[ES[i][j]]) sx=ES[i][j],sy=miner,dst=minn-1;
if (dp[ES[i][j]]>maxn) maxn=dp[ES[i][j]],maxer=ES[i][j];
if (dp2[ES[i][j]]<minn) minn=dp2[ES[i][j]],miner=ES[i][j];
}
}
}
if (!sx)
{
puts("No");
continue;
}
for (int i=1;i<=n;++i) ans[i]=a[tong[dst]];
if (dst!=1) dfs5(sx,dst-1);
if (dst!=length) dfs6(sy,dst+1);
puts("Yes");
for (int i=1;i<=n;++i) printf("%d ",ans[i]);
puts("");
sort(ans+1,ans+n+1);
for (int i=1;i<=n;++i)
if (a[i]!=ans[i])
{
cerr<<1/0<<endl;
exit(0);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
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