QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#330645 | #8055. Balance | ucup-team987# | WA | 1ms | 3700kb | C++20 | 5.4kb | 2024-02-17 17:28:58 | 2024-02-17 17:28:59 |
Judging History
answer
#include<iostream>
#include<cassert>
using namespace std;
#include<vector>
#include<algorithm>
struct bridge{
int n;
vector<int>ord,low;
vector<bool>art;
vector<vector<int> >G;
bridge(int n_=0):n(n_),ord(n_,-1),low(n_),art(n_,false),G(n_){}
void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
}
bool operator[](int i)const{return art[i];}
bool is_bridge(int a,int b)const
{
if(ord[a]>ord[b])swap(a,b);
return ord[a]<low[b];
}
void dfs(int u,int p,int&k)
{
low[u]=ord[u]=k++;
bool now=false;
int pc=0;
for(int v:G[u])
{
if(ord[v]==-1)
{
dfs(v,u,k);
low[u]=min(low[u],low[v]);
now=now||ord[u]<=low[v];
}
else if(v!=p||pc++)
{
low[u]=min(low[u],ord[v]);
}
}
art[u]=now;
}
void build()
{
int k=0;
for(int u=0;u<n;u++)if(ord[u]==-1)
{
int cnt=0;
low[u]=ord[u]=k++;
for(int v:G[u])if(ord[v]==-1)
{
dfs(v,u,k);
cnt++;
}
if(cnt>=2)art[u]=true;
}
}
};
//Two Edge Connected Components require bridge
struct TECC:bridge{
vector<int>comp;
TECC(int n_=0):bridge(n_),comp(n_,-1){}
int operator[](int i)const{return comp[i];}
void dfs(int u,int p,int&k)
{
comp[u]=p==-1||is_bridge(p,u)?k++:comp[p];
for(int v:G[u])if(comp[v]==-1)dfs(v,u,k);
}
int build()
{
bridge::build();
int k=0;
for(int u=0;u<n;u++)if(comp[u]==-1)dfs(u,-1,k);
return k;
}
int build(vector<vector<int> >&H)
{
int k=build();
H.assign(k,vector<int>());
for(int u=0;u<n;u++)
{
for(int v:G[u])if(comp[u]!=comp[v])
{
H[comp[u]].push_back(comp[v]);
}
}
return k;
}
};
int N,M,K;
vector<vector<int> >G;
vector<int>Vs[1<<17];
int A[1<<17];
int sz[1<<17];
int dlm[1<<17];
vector<int>val;
int up[1<<17],down[1<<17];
int idx[1<<17],base;
void Up(int u,int I,bool f)
{
//cout<<"Up "<<Vs[u][0]<<" "<<I<<endl;
idx[u]=I;
if(!f)
{
for(int v:G[u])Up(v,I,false);
}
else
{
int mxi=-1,mx=-1;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
int UP=up[v];
if(dlm[sz[v]]!=-1)UP++;
if(mx<UP)
{
mx=UP;
mxi=i;
}
}
if(mxi!=-1)
{
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(i!=mxi)Up(v,I,false);
else
{
int nI=I;
if(dlm[sz[v]]!=-1)nI--;
Up(v,nI,true);
}
}
}
}
}
void Down(int u,int I,bool f)
{
idx[u]=I;
//cout<<"Down "<<Vs[u][0]<<" "<<I<<endl;
if(!f)
{
for(int v:G[u])Down(v,I,false);
}
else
{
int mxi=-1,mx=-1;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
int DOWN=down[v];
if(dlm[N-sz[v]]!=-1)DOWN++;
if(mx<DOWN)
{
mx=DOWN;
mxi=i;
}
}
if(mxi!=-1)
{
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(i!=mxi)Down(v,I,false);
else
{
int nI=I;
if(dlm[N-sz[v]]!=-1)nI++;
Down(v,nI,true);
}
}
}
}
}
bool dfs(int u,int p)
{
if(p!=-1)
{
int i=0;
while(G[u][i]!=p)i++;
if(i+1<G[u].size())swap(G[u][i],G[u][G[u].size()-1]);
G[u].pop_back();
}
up[u]=down[u]=0;
vector<int>UP(G[u].size()),DOWN(G[u].size());
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(dfs(v,u))return true;
{//up
UP[i]=up[v];
if(dlm[sz[v]]!=-1)UP[i]++;
up[u]=max(up[u],UP[i]);
}
{//down
DOWN[i]=down[v];
if(dlm[N-sz[v]]!=-1)DOWN[i]++;
down[u]=max(down[u],DOWN[i]);
}
sz[u]+=sz[v];
}
if(!G[u].empty())
{
int pup=0,pi=-1;
int pdown=0,pi2=-1;
for(int i=0;i<G[u].size();i++)
{
int ui=-1,vi=-1;
if(pup+DOWN[i]==val.size()-1)
{
//cout<<"pup found at "<<u<<" "<<Vs[u][0]<<endl;
//cout<<"with "<<pup<<" "<<DOWN[i]<<endl;
base=pup;
ui=pi,vi=i;
}
else if(pdown+UP[i]==val.size()-1)
{
//cout<<"pdown found at "<<u<<" "<<Vs[u][0]<<endl;
//cout<<"with "<<UP[i]<<" "<<pdown<<endl;
base=UP[i];
ui=i,vi=pi2;
}
if(ui!=-1||vi!=-1)
{
if(ui!=-1)
{//up
int v=G[u][ui];
int nI=base;
if(dlm[sz[v]]!=-1)nI--;
Up(v,nI,true);
}
if(vi!=-1)
{//down
int v=G[u][vi];
int nI=base;
if(dlm[N-sz[v]]!=-1)nI++;
Down(v,nI,true);
}
return true;
}
if(pup<UP[i])
{
pup=UP[i];
pi=i;
}
if(pdown<DOWN[i])
{
pdown=DOWN[i];
pi2=i;
}
}
}
sz[u]+=Vs[u].size();
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;cin>>T;
for(;T--;)
{
cin>>N>>M;
TECC tecc(N);
for(int i=0;i<M;i++)
{
int u,v;cin>>u>>v;
u--,v--;
tecc.add_edge(u,v);
}
K=tecc.build(G);
for(int i=0;i<K;i++)Vs[i].clear();
for(int i=0;i<N;i++)Vs[tecc[i]].push_back(i);
for(int i=0;i<N;i++)
{
cin>>A[i];
}
sort(A,A+N);
val.clear();
for(int i=0;i<=N;i++)dlm[i]=-1;
dlm[0]=0;
val.push_back(A[0]);
for(int i=1;i<N;i++)if(A[i-1]<A[i])
{
dlm[i]=val.size();
val.push_back(A[i]);
}
dlm[N]=val.size();
if(val.size()==1)
{
cout<<"Yes\n";
for(int i=0;i<N;i++)cout<<A[i]<<(i+1==N?"\n":" ");
continue;
}
for(int i=0;i<K;i++)idx[i]=-1;
if(dfs(0,-1))
{
cout<<"Yes\n";
for(int i=0;i<K;i++)if(idx[i]==-1)idx[i]=base;
for(int i=0;i<N;i++)
{
//cout<<"idx="<<idx[tecc[i]]<<endl;
cout<<val.at(idx[tecc[i]])<<(i+1==N?"\n":" ");
}
}
else
{
cout<<"No\n";
}
}
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3700kb
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 3 2 2 Yes 1 1 2 2 2 No
result:
wrong answer 3-th smallest numbers are not same (test case 4)