【JZOJ 杂题选讲】CF1325F

题目

给你一个n个点,m条边的无向连通图。无自环无重边。
令k=根号n上取整。
抉择:找到一个大小为k的独立集;找到一个大小至少为k的简单环。
N<=1e5,m<=2e5

思路

每次删掉度数最小的点和它的邻居,直到最小的点度数>=k-1

如果你爸所有点删完,那证明你选出了独立集,否则,在剩下的点中,一定有大于k的简单环

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+77;
bool bz[N];
int fa[N],dep[N],n,m,k;
vector<int> G[N];
void dfs(int x,int f,int d) 
{
	dep[x] = d;
	fa[x] = f;
	for(auto y:G[x]) 
	{
		if(dep[y]==0)
			dfs(y,x,d+1);
		if(dep[x]-dep[y]+1>=k) 
		{
			cout<<2<<endl;
			cout<<dep[x]-dep[y]+1<<endl;
			cout<<y<<" ";
			int z = x;
			while(z!=y) 
			{
				cout<<z<<" ";
				z = fa[z];
			}
			exit(0);
		}
	}
	if(!bz[x])
		for(auto y:G[x]) 
		{
			bz[y] = 1;
		} 
}

int main() 
{
	scanf("%d%d",&n,&m);
	while(k*k<n) k++;
	for(int i=0,a,b;i<m;i++) 
	{
		scanf("%d%d",&a,&b);
		G[a].push_back(b);
		G[b].push_back(a);
	}
	dfs(1,0,1);
	cout<<1<<endl;
	for(int i=1; k; i++) 
	{
		if(bz[i]) continue;
		cout<<i<<" ";
		k--;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值