QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#421417#961. Smol Vertex CovercqbzlyWA 0ms3608kbC++203.1kb2024-05-25 18:19:302024-05-25 18:19:32

Judging History

你现在查看的是最新测评结果

  • [2024-05-25 18:19:32]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3608kb
  • [2024-05-25 18:19:30]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define inf 0x3f3f3f3f
using namespace std;
const int N=505;
int T,n,m,cnt,used[N],to[N],bl[N],fa[N],sb;
int low[N],dfn[N],num,fk[N],vs[N];
stack<int>stk;
vector<int>G[N];
vector<int>G0[N]; 
pair<int,int>a[N*N];
pair<int,int>p[N];
mt19937 gen(114514);
bool dfs(int u){
	used[u]=1;
	shuffle(G[u].begin(),G[u].end(),gen);
	for(auto v:G[u]){
		if(used[v])continue;
		used[v]=1;
		int x=to[v];
		to[u]=v,to[v]=u,to[x]=0;
		if(!x||dfs(x))return 1;
		to[v]=x,to[x]=v,to[u]=0;
	}
	return 0;
}
void init(){
	for(int i=1;i<=2*cnt;i++)fa[i]=i,dfn[i]=low[i]=fk[i]=vs[i]=0,G0[i].clear();num=sb=0;
}
void add(int x,int y){
	G0[x].pb(y);
}
void work(int u,int v){
	if(!bl[u]){
		if(v==p[bl[v]].fi)add(bl[v]+cnt,bl[v]);
		else add(bl[v],bl[v]+cnt);
	}
	else if(!bl[v]){
		if(u==p[bl[u]].fi)add(bl[u]+cnt,bl[u]);
		else add(bl[u],bl[u]+cnt);
	}
	else{
		int x=(u==p[bl[u]].se),y=(v==p[bl[v]].se);
		add(bl[u]+(1-x)*cnt,bl[v]+y*cnt);
		add(bl[v]+(1-y)*cnt,bl[u]+x*cnt); 
	}
}
void tarjan(int u){
	low[u]=dfn[u]=++num,stk.push(u),vs[u]=1;
	for(auto v:G0[u]){
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(vs[v]){
			low[u]=min(low[u],dfn[v]);
		}
	}
	if(low[u]==dfn[u]){
		int tmp=0;sb++;
		do{
			tmp=stk.top(),stk.pop();
			fk[tmp]=sb,vs[tmp]=0;
		}while(tmp!=u);
	}
}
int main(){
	//freopen("data.in","r",stdin);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>T;
	while(T--){
		cin>>n>>m;for(int i=1;i<=n;i++)G[i].clear(),bl[i]=0;
		for(int i=1;i<=m;i++){
			int u,v;cin>>u>>v;
			G[u].pb(v),G[v].pb(u);
			//cout<<"edge:"<<u<<" "<<v<<"\n";
			a[i]={u,v};
		}
		for(int i=1;i<=n;i++)to[i]=0;
		for(int i=1;i<=n;i++){
			if(!to[i]){
				for(int j=1;j<=n;j++)used[j]=0;
				dfs(i);
			}
		}
		cnt=0;
		for(int i=1;i<=n;i++){
			if(to[i]&&i<to[i]){
				p[++cnt]={i,to[i]};
				bl[i]=bl[to[i]]=cnt;
			}
		}
		// for(int i=1;i<=n;i++){
		// 	cout<<p[i].fi<<" "<<p[i].se<<"\n";
		// }
		init();
		for(int i=1;i<=m;i++){
			int u=a[i].fi,v=a[i].se;
			if(bl[u]==bl[v])continue;
			work(u,v);
		}
		for(int i=1;i<=cnt*2;i++)if(!dfn[i])tarjan(i);
		bool ok=1;
		for(int i=1;i<=cnt;i++)if(fk[i]==fk[i+cnt])ok=0;
		if(ok){
			cout<<cnt<<"\n";
			for(int i=1;i<=cnt;i++){
				if(fk[i]<fk[i+cnt])cout<<p[i].fi<<" ";
				else cout<<p[i].se<<" ";
			}
			cout<<"\n";
		}
		else{
			bool flg=0;
			for(int i=1;i<=cnt;i++){
				init();
				for(int j=1;j<=m;j++){
					int u=a[j].fi,v=a[j].se;
					if(bl[u]==i||bl[v]==i||bl[u]==bl[v])continue;
					work(u,v);
				}
				for(int j=1;j<=cnt*2;j++)if(!dfn[j])tarjan(j);
				bool ok=1;
				for(int j=1;j<=cnt;j++)if(fk[j]==fk[j+cnt])ok=0;
				if(ok){
					flg=1;
					cout<<cnt+1<<"\n";
					for(int j=1;j<=cnt;j++){
						if(j==i)cout<<p[j].fi<<" "<<p[j].se<<" ";
						else if(fk[j]<fk[j+cnt])cout<<p[j].fi<<" ";
						else cout<<p[j].se<<" ";
					}
					cout<<"\n";
					break;
				}
			}
			if(!flg)cout<<"not smol"<<"\n";
		}
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3608kb

input:

5 5
1 2
2 3
3 4
4 5
1 5

output:

0

0

0

0

0


result:

wrong answer not a vertex cover