QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#453837 | #8055. Balance | ucup-team3282 | WA | 0ms | 14172kb | C++14 | 3.3kb | 2024-06-24 12:52:02 | 2024-06-24 12:52:02 |
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;
// cout<<"11 "<<u<<' '<<v<<endl;
}
if(a[sz[v]]!=a[sz[v]+1]){
val[t.second^1]=1;
// cout<<"11 "<<v<<' '<<u<<endl;
}
}
}
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];
deque <int> q;
void bfs(){
q.push_back(x);
while(!q.empty()){
int u=q.front();
q.pop_front();
for(int v:dcc[u])
ans[v]=a[++cc];
for(auto t:g1[u]){
int v=t.first,w=val[t.second];
if(v==fa[u])
continue;
if(f[v]&&w)
q.push_back(v);
else
q.push_front(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 i=1;i<=n;i++)cout<<bel[i]<<' ';cout<<endl;
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);
cout<<x<<' '<<y<<endl;
if(d>=c-1){
cout<<"Yes"<<endl;
for(int i=1;i<=dccc;i++)
fa[i]=0;
dfs3(x);
for(int p=y;p!=x;p=fa[p])
f[p]=1;
bfs();
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;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 14172kb
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:
5 4 3 2 1 5 1 Yes 1 2 3 4 5 5 1 2 3 4 4 1 No 5 1 2 3 4 4 1 Yes 2 3 2 2 1 2 2 1 1 1 1 2 Yes 2 2 1 1 1 1 1 1 1 No
result:
wrong answer Token parameter [name=yesno] equals to "5", doesn't correspond to pattern "Yes|No" (test case 1)