QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#453815 | #8055. Balance | ucup-team3282 | WA | 171ms | 15212kb | C++14 | 3.3kb | 2024-06-24 12:18:19 | 2024-06-24 12:18:19 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int T;
int n,m;
vector <pair<int,int> > g[maxn];
int a[maxn],tmp[maxn],c;
int dfn[maxn],low[maxn],dfnc=0;
vector <pair<int,int> > g1[maxn];
int val[maxn],ec=0;
stack <int> s;
vector <int> dcc[maxn];
int dccc=0,bel[maxn];
void tarjan(int u,int fid){
dfn[u]=low[u]=++dfnc;
s.push(u);
for(auto t:g[u]){
int v=t.first,id=t.second;
if(!dfn[v]){
tarjan(v,id);
low[u]=min(low[v],low[u]);
}
else if(id!=(fid^1)){
low[u]=min(dfn[v],low[u]);
}
}
if(low[u]==dfn[u]){
dccc++;
int x;
do{
x=s.top();
s.pop();
dcc[dccc].push_back(x);
bel[x]=dccc;
}while(x!=u);
}
}
int fa[maxn],sz[maxn];
void dfs1(int u){
sz[u]=dcc[u].size();
for(auto &t:g1[u]){
int v=t.first;
if(v==fa[u])
continue;
fa[v]=u;
dfs1(v);
sz[u]+=sz[v];
if(a[n-sz[v]]!=a[n-sz[v]+1]){
val[t.second]=1;
}
if(a[sz[v]]!=a[sz[v]+1]){
val[t.second^1]=1;
}
}
}
int dp[maxn][2],p[maxn][2];
int d=0;
int x,y;
void dfs2(int u){
dp[u][0]=dp[u][1]=0;
p[u][0]=p[u][1]=u;
for(auto t:g1[u]){
int v=t.first,id=t.second;
if(v==fa[u])
continue;
dfs2(v);
if(dp[u][1]+val[id]+dp[v][0]>=d){
d=dp[u][1]+val[id]+dp[v][0];
x=p[u][1];y=p[v][0];
}
if(dp[v][1]+val[id^1]+dp[u][0]>=d){
d=dp[v][1]+val[id^1]+dp[u][0];
x=p[v][1];y=p[u][0];
}
if(val[id]+dp[v][0]>=dp[u][0]){
dp[u][0]=val[id]+dp[v][0];
p[u][0]=p[v][0];
}
if(dp[v][1]+val[id^1]>=dp[u][1]){
dp[u][1]=dp[v][1]+val[id^1];
p[u][1]=p[v][1];
}
}
}
bool f[maxn];
void dfs3(int u){
for(auto t:g1[u]){
int v=t.first;
if(v==fa[u])
continue;
fa[v]=u;
dfs3(v);
}
}
int cc=0;
int ans[maxn];
void dfs4(int u){
for(int x:dcc[u])
ans[x]=a[++cc];
for(auto t:g1[u]){
int v=t.first,w=val[t.second];
if(v==fa[u])
continue;
if(f[v]&&w)
continue;
dfs4(v);
}
for(auto t:g1[u]){
int v=t.first,w=val[t.second];
if(v==fa[u])
continue;
if(f[v]&&w)
dfs4(v);
}
}
int main(){
ios::sync_with_stdio(0);
cin>>T;
while(T--){
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
g[u].push_back({v,i*2-2});
g[v].push_back({u,i*2-1});
}
for(int i=1;i<=n;i++)
cin>>a[i];
tarjan(1,-1);
for(int u=1;u<=n;u++){
for(auto t:g[u]){
int v=t.first;
if(bel[u]<bel[v]){
g1[bel[u]].push_back({bel[v],ec++});
g1[bel[v]].push_back({bel[u],ec++});
}
}
}
sort(a+1,a+n+1);
dfs1(1);
for(int i=1;i<=n;i++)
tmp[i]=a[i];
c=unique(tmp+1,tmp+n+1)-(tmp+1);
d=0;x=y=1;
dfs2(1);
if(d>=c-1){
cout<<"Yes"<<endl;
for(int i=1;i<=dccc;i++)
fa[i]=0;
dfs3(x);
// for(int i=1;i<=n;i++)
// cout<<fa[i]<<' ';
// cout<<endl;
// cout<<x<<' '<<y<<endl;
for(int p=y;p!=x;p=fa[p]){
f[p]=1;
assert(p!=0);
}
dfs4(x);
for(int i=1;i<=n;i++)
cout<<ans[i]<<' ';
cout<<endl;
}
else{
cout<<"No"<<endl;
}
for(int i=1;i<=n;i++)
g[i].clear();
for(int i=1;i<=n;i++)
dfn[i]=low[i]=0;
s=stack<int>();
for(int i=1;i<=dccc;i++){
dcc[i].clear();
g1[i].clear();
fa[i]=sz[i]=f[i]=0;
}
for(int i=0;i<ec;i++)
val[i]=0;
dccc=dfnc=ec=cc=0;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 14252kb
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 2 2 1 Yes 2 2 1 1 1 No
result:
ok ok (5 test cases)
Test #2:
score: 0
Accepted
time: 111ms
memory: 14556kb
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 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 Yes 1 3 1 1 1 Yes 1 1 1 Yes 1 2 Yes 1 1 1 1 1 Yes 1 2 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 Yes 1 1 1 2 No Yes 1 1 No Yes 1 1 N...
result:
ok ok (100000 test cases)
Test #3:
score: -100
Wrong Answer
time: 171ms
memory: 15212kb
input:
83335 9 17 1 4 8 9 5 2 1 3 2 7 1 7 2 8 6 7 2 4 1 8 5 8 7 1 8 6 4 6 4 7 6 9 7 9 7 3 4 4 7 4 2 4 8 6 9 3 1 1 2 3 5 1 2 3 4 4 5 6 3 6 1 6 2 4 3 2 2 1 3 9 8 9 3 5 7 5 9 2 6 1 8 4 1 4 2 4 3 4 2 5 3 4 5 4 5 4 6 7 2 1 1 5 6 1 3 1 6 5 2 4 5 3 2 1 2 1 2 1 4 6 2 1 4 2 1 4 1 2 1 4 3 1 2 2 4 2 6 10 2 3 3 5 2 1 ...
output:
No No Yes 3 4 4 4 5 4 5 2 5 No Yes 2 2 4 2 No Yes 2 3 3 3 Yes 2 2 1 No Yes 1 1 1 1 1 No Yes 1 1 1 Yes 1 1 3 3 3 Yes 1 1 Yes 1 1 Yes 1 1 1 1 Yes 3 1 3 No Yes 1 1 1 1 1 1 1 1 Yes 1 1 1 1 1 1 1 No Yes 1 1 No Yes 1 1 1 1 1 Yes 2 1 1 2 1 No Yes 1 2 3 1 3 3 3 1 No No No No No No No No No ...
result:
wrong answer max - min = 2, but sum of difference = 4 (test case 1197)