QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#420132#961. Smol Vertex Cover_qwqUwUWA 1ms4004kbC++142.9kb2024-05-24 14:49:132024-05-24 14:49:14

Judging History

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

  • [2024-05-24 14:49:14]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4004kb
  • [2024-05-24 14:49:13]
  • 提交

answer

#include<bits/stdc++.h>
#define pb push_back
#define P make_pair
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
inline ll read(){
	ll x=0,c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0' && c<='9')x=x*10 + c-'0',c=getchar();
	return x;
}
const int N=502;
int n,m,lk[N],vis[N<<1],pre[N],fa[N];
vector<int>G[N];
inline void link(int u,int v){lk[u]=v,lk[v]=u;}
inline void up(int u){if(u)up(lk[pre[u]]),link(u,pre[u]);}
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
int cnt,col[N<<1];
inline int lca(int u,int v){
	++cnt;
	while(1){
		if(col[u=find(u)] == cnt)return u;
		if(u)col[u]=cnt;
		u=pre[lk[u]]; swap(u,v);
	}
}
queue<int>q;
inline void shrink(int u,int v,int p){
	while(find(u)!=p){
		pre[u]=v,v=lk[u];fa[u]=fa[v]=p;u=pre[v];
		if(vis[v] == 2)vis[v]=1,q.push(v);
	}
}
inline bool blossom(int s){
	rep(i,1,n)fa[i]=i,vis[i]=pre[i]=0;
	q=queue<int>();q.push(s);vis[s]=1;
	while(!q.empty()){
		int u=q.front();q.pop();
		for(auto v:G[u]){
			if(!vis[v]){
				vis[v]=2;pre[v]=u;
				if(!lk[v]){up(v);return 1;}
				vis[lk[v]]=1;q.push(lk[v]);
			}
			else if(vis[v] == 1){
				int p=lca(u,v);
				shrink(u,v,p);
				shrink(v,u,p);
			}
		}
	}
	return 0;
}
vector<int>G2[N<<1];
int dfn[N<<1],low[N<<1],tim,dcc;
inline void get(){
	rep(i,1,n)vis[i]=0;
}
stack<int,vector<int>>st;
inline void dfs(int u){
	dfn[u]=low[u]=++tim;st.push(u);vis[u]=1;
	for(auto v:G2[u]){
		if(!dfn[v]){
			dfs(v);
			low[u]=min(low[u],low[v]);
		}
		else if(vis[v])low[u]=min(low[u],dfn[v]);
	}
	if(dfn[u] == low[u]){
		++dcc;
		while(st.top() != u){
			int v=st.top();
			col[v]=dcc,vis[v]=0;
			st.pop();
		}
		st.pop();
		col[u]=dcc,vis[u]=0;
	}
}
vector<int>vec[N<<1];
inline bool make(){
	rep(i,1,n<<1)vis[i]=col[i]=0;
	rep(i,1,n<<1)if(!dfn[i])dfs(i);
	rep(i,1,n)if(col[i] == col[i+n])return 0;
	rep(i,1,n<<1)vis[i]=1;
	rep(i,1,dcc)vec[i].clear();
	rep(i,1,n<<1)vec[col[i]].pb(i);
	rep(i,1,dcc)if(vis[i]){
		for(auto v:vec[i]) vis[col[v>n?v-n:v+n]]=0;
	}
	return 1;
}
inline void solve(){
	rep(i,1,n)G[i].clear(),vis[i]=lk[i]=0;
	n=read(),m=read();
	int M=0;
	rep(i,1,m){
		int u=read(),v=read();
		G[u].pb(v),G[v].pb(u);
		if(!lk[u] && !lk[v])link(u,v),++M;
	}
	rep(i,1,n)if(!lk[i])M += blossom(i);
	rep(j,0,n){
		tim=dcc=0;
		rep(i,1,n<<1){
			dfn[i]=low[i]=0;
			G2[i].clear();
		}
		rep(i,1,n){
			if(i==j)G2[i+n].pb(i);
			else if(!lk[i])G2[i].pb(i+n);
			for(auto k:G[i]){
				if(k==lk[i])G2[i].pb(k+n);
				G2[i+n].pb(k);
			}
		}
		if(make()){
			printf("%d\n",M+(j>0));
			rep(i,1,n)if(vis[col[i]])printf("%d ",i);
			printf("\n");
			return ;
		}
	}
	printf("not smol\n");
}
int main(){
	//freopen("data.in","r",stdin);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int T=1;while(T--)solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3844kb

input:

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

output:

3
1 3 5 

result:

ok vertex cover of size 3

Test #2:

score: 0
Accepted
time: 0ms
memory: 4004kb

input:

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

output:

not smol

result:

ok not smol

Test #3:

score: 0
Accepted
time: 0ms
memory: 3840kb

input:

3 0

output:

0


result:

ok vertex cover of size 0

Test #4:

score: 0
Accepted
time: 0ms
memory: 3904kb

input:

10 10
2 5
3 8
3 10
6 9
1 4
2 6
2 3
4 6
7 10
4 7

output:

5
1 2 3 6 7 

result:

ok vertex cover of size 5

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 3720kb

input:

10 20
1 9
3 6
3 7
8 9
3 8
1 4
5 10
7 10
4 6
7 9
9 10
2 7
1 6
5 8
2 9
1 7
5 7
3 10
2 6
4 10

output:

not smol

result:

wrong answer vertex cover is smol, but participant did not print it