QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#720244 | #8055. Balance | qzez# | WA | 96ms | 50340kb | C++14 | 3.3kb | 2024-11-07 11:22:55 | 2024-11-07 11:22:55 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;using pii=pair<int,int>;
const int N=5e5+5,INF=1e9+7;
namespace Debug{
void debug(char *c){
cerr<<endl;
}
template<class T,class...S>
void debug(char *c,T x,S...y){
for(;*c&&*c!=',';)cerr<<(*(c++));
cerr<<'='<<x<<',',debug(c+1,y...);
}
}
#ifdef LOCAL
#define gdb(...) Debug::debug((char*)#__VA_ARGS__,__VA_ARGS__)
#else
#define gdb(...) void()
#endif
int n;
vector<int> S[N],G[N];
int scc[N],m;
namespace Tarjan{
int dfn[N],low[N],dh,st[N],sh;
void clr(){
for(int i=0;i<=n+1;i++) dfn[i]=low[i]=st[i]=0;dh=sh=m=0;
}
void tarjan(int x,int La){
dfn[x]=low[x]=++dh;st[++sh]=x;
int flag=0;
for(int i:S[x])if(i^La||flag){
if(!dfn[i]) tarjan(i,x),low[x]=min(low[x],low[i]);
else low[x]=min(low[x],dfn[i]);
}else flag=1;
if(low[x]>=dfn[x]){
++m;
while(st[sh+1]^x) scc[st[sh]]=m,sh--;
}
}
}
int A[N],B[N];
int siz[N],sum[N],fa[N],bg[N],bh,en[N],d[N];
void make(int x,int La){
sum[x]=siz[x];fa[x]=La;bg[x]=++bh;d[x]=d[La]+1;
for(int i:G[x]) if(i^La) make(i,x),sum[x]+=sum[i];
en[x]=bh;
}
int col[N];
void push(int x,int La,int y,int w){
if(x==y) return;
col[x]=w;
for(int i:G[x]) if(i^La) push(i,x,y,w);
}
int check(int x,int La,int *A,int l,int r){
if(l>r) return 1;
int pt=r;
for(int i=l;i<r;i++) if(A[i]^A[i+1]){pt=i;break;}
if(pt==r) return push(x,La,0,A[l]),1;
queue<pii> Q;Q.emplace(x,La);
gdb(x,La,l,r);
while(!Q.empty()){
auto [a,b]=Q.front();Q.pop();
int ss=(b==fa[a]?sum[a]:n-sum[b]);
gdb(a,b,ss,pt);
if(ss==(r-pt)&&check(a,b,A,pt+1,r)){
push(x,La,a,A[l]);
return 1;
}
for(int i:G[a])if(i^b){
int ss=(a==fa[i]?sum[i]:n-sum[a]);
if(ss>=r-pt) Q.emplace(i,a);
}
}
return 0;
}
void print(){
puts("Yes");
for(int i=1;i<=n;i++) printf("%d%c",col[scc[i]]," \n"[i==n]);
}
void Solve(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) S[i].clear(),G[i].clear();
while(m--){
int x,y;scanf("%d%d",&x,&y);
S[x].push_back(y);S[y].push_back(x);
}
Tarjan::clr();
Tarjan::tarjan(1,0);
for(int i=1;i<=n;i++) for(int j:S[i]) if(scc[i]^scc[j]){
G[scc[i]].push_back(scc[j]);
}
for(int i=1;i<=n;i++) scanf("%d",&A[i]);
sort(A+1,A+n+1);
copy(A+1,A+n+1,B+1);reverse(B+1,B+n+1);
for(int i=1;i<=m;i++) siz[i]=0;
for(int i=1;i<=n;i++) siz[scc[i]]++;
bh=0;
make(1,0);
int p1=n;
for(int i=n-1;i*2>=n;i--) if(A[i]^A[i+1]) p1=i;
for(int i=1;i<=n;i++) if(sum[i]==p1&&check(i,fa[i],B,n-p1+1,n)&&check(fa[i],i,A,p1+1,n)){
return print();
}
int p2=n;
for(int i=n-1;i*2>=n;i--) if(B[i]^B[i+1]) p2=i;
for(int i=1;i<=n;i++) if(sum[i]==p2&&check(i,fa[i],A,n-p2+1,n)&&check(fa[i],i,B,p2+1,n)){
return print();
}
p1=p2=0;
for(int i=1;i*2<=n;i++) if(A[i]^A[i+1]) p1=i;
for(int i=1;i*2<=n;i++) if(B[i]^B[i+1]) p2=i;
vector<int> s1,s2;
for(int i=1;i<=n;i++) if(sum[i]==p1&&check(i,fa[i],B,n-p1+1,n)) s1.push_back(i);
for(int i=1;i<=n;i++) if(sum[i]==p2&&check(i,fa[i],A,n-p2+1,n)) s2.push_back(i);
for(int i:s1) for(int j:s2){
int x=i,y=j;
if(d[x]>d[y]) swap(x,y);
if(bg[x]<=bg[y]&&bg[y]<=en[x]) continue;
fill(col+1,col+m+1,A[p1+1]);
check(i,fa[i],B,n-p1+1,n);
check(j,fa[j],A,n-p2+1,n);
return print();
}
puts("No");
}
int main(){
int t=1;
scanf("%d",&t);
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 50340kb
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 3 1 2 2 Yes 2 2 1 1 1 No
result:
ok ok (5 test cases)
Test #2:
score: -100
Wrong Answer
time: 96ms
memory: 50176kb
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 3 1 Yes 3 2 2 2 2 Yes 1 1 1 Yes 3 3 3 3 Yes 3 3 3 3 Yes 2 2 2 2 Yes 2 2 2 2 2 No Yes 1 1 Yes 1 1 Yes 1 1 Yes 1 1 1 1 Yes 1 1 1 1 1 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 Yes 2 2 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...
result:
wrong answer 1-th smallest numbers are not same (test case 2)