QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#353286 | #8055. Balance | ucup-team134# | TL | 0ms | 13220kb | C++17 | 3.4kb | 2024-03-14 01:05:07 | 2024-03-14 01:05:07 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=100050;
const int M=200050;
vector<pair<int,int>> G[N];
int cmp[N],csz,cmp_sz[N];
bool bridge[M];
int disc[N],low[N],tid;
void Bridges(int u,int p){
disc[u]=low[u]=++tid;
for(auto e:G[u]){
if(e.second!=p){
if(!disc[e.first]){
Bridges(e.first,e.second);
if(low[e.first]>disc[u]){
bridge[e.second]=true;
}
low[u]=min(low[u],low[e.first]);
}else{
low[u]=min(low[u],low[e.first]);
}
}
}
}
void Find(int u){
cmp[u]=csz;
cmp_sz[csz]++;
for(auto e:G[u]){
if(!bridge[e.second]){
if(!cmp[e.first]){
Find(e.first);
}
}
}
}
vector<int> E[N];
int sz[N];
int pref[N],suff[N],cnt[N];
int asz;
bool ok=false;
int U,V;
int L,R;
int dp_l[N],dp_r[N],best_l[N],best_r[N],par[N];
void DFS(int u,int p){
sz[u]=cmp_sz[u];
dp_l[u]=dp_r[u]=0;
best_l[u]=best_r[u]=u;
for(int v:E[u]){
if(v!=p){
par[v]=u;
DFS(v,u);
if(dp_l[u]+dp_r[v]==asz-1){
ok=true;
L=dp_l[u];
R=dp_r[v];
U=best_l[u];
V=best_r[v];
}
if(dp_r[u]+dp_l[v]==asz-1){
ok=true;
L=dp_l[v];
R=dp_r[u];
U=best_l[v];
V=best_r[u];
}
if(dp_l[v]>dp_l[u]){
dp_l[u]=dp_l[v];
best_l[u]=best_l[v];
}
if(dp_r[v]>dp_r[u]){
dp_r[u]=dp_r[v];
best_r[u]=best_l[v];
}
sz[u]+=sz[v];
}
}
if(sz[u]==pref[dp_l[u]+1]){
dp_l[u]++;
}
if(sz[u]==suff[dp_r[u]+1]){
dp_r[u]++;
}
}
int ans[N];
void Mark(int u,int p,int x){
ans[u]=x;
for(int v:E[u]){
if(v!=p && !ans[v]){
Mark(v,u,x);
}
}
}
int main(){
int t;
scanf("%i",&t);
while(t--){
int n,m;
scanf("%i %i",&n,&m);
for(int i=1;i<=m;i++){
int u,v;
scanf("%i %i",&u,&v);
G[u].pb({v,i});
G[v].pb({u,i});
}
for(int i=1,x;i<=n;i++){
scanf("%i",&x);
cnt[x]++;
}
asz=0;
for(int i=1;i<=n;i++){
if(cnt[i]!=0){
asz++;
pref[asz]=pref[asz-1]+cnt[i];
}
}
asz=0;
for(int i=n;i>=1;i--){
if(cnt[i]!=0){
asz++;
suff[asz]=suff[asz-1]+cnt[i];
}
}
Bridges(1,0);
//printf("Bridges: ");for(int i=1;i<=m;i++)printf("%i",bridge[i]);printf("\n");
for(int i=1;i<=n;i++){
if(!cmp[i]){
csz++;
Find(i);
}
}
for(int i=1;i<=n;i++){
for(auto e:G[i]){
if(bridge[e.second]){
E[cmp[i]].pb(cmp[e.first]);
}
}
}
DFS(1,0);
if(ok){
printf("Yes\n");
int j=0;
int tmp=U;
int mid;
for(int i=1;i<=n;i++){
if(cnt[i]>0){
j++;
if(j<=L){
while(sz[tmp]!=pref[j])tmp=par[tmp];
Mark(tmp,par[tmp],i);
}else{
mid=i;
break;
}
}
}
j=0;
tmp=V;
for(int i=n;i>=1;i--){
if(cnt[i]>0){
j++;
if(j<=R){
while(sz[tmp]!=suff[j])tmp=par[tmp];
Mark(tmp,par[tmp],i);
}
}
}
Mark(1,0,mid);
for(int i=1;i<=n;i++)printf("%i ",ans[cmp[i]]);
printf("\n");
}else printf("No\n");
for(int i=1;i<=n;i++){
E[i].clear();
G[i].clear();
cmp[i]=0;
cmp_sz[i]=0;
disc[i]=low[i]=0;
sz[i]=0;
pref[i]=suff[i]=0;
cnt[i]=0;
dp_l[i]=dp_r[i]=0;
best_l[i]=best_r[i]=0;
ans[i]=0;
par[i]=0;
}
csz=0;
asz=0;
for(int i=1;i<=m;i++){
bridge[i]=false;
}
ok=false;
U=0;
V=0;
L=0;
R=0;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13220kb
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 3 2 2 1 Yes 2 2 1 1 1 No
result:
ok ok (5 test cases)
Test #2:
score: -100
Time Limit Exceeded
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...