#649 (Div. 2)D. Ehab‘s Last Corollary

article/2025/10/28 13:14:50

题目描述

Given a connected undirected graph with n vertices and an integer k, you have to either:
either find an independent set that has exactly ⌈k2⌉ vertices.
or find a simple cycle of length at most k.
An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn’t contain any vertex twice.
I have a proof that for any input you can always solve at least one of these problems, but it’s left as an exercise for the reader.

Input

The first line contains three integers n, m, and k (3≤k≤n≤105, n−1≤m≤2⋅105) — the number of vertices and edges in the graph, and the parameter k from the statement.
Each of the next m lines contains two integers u and v (1≤u,v≤n) that mean there’s an edge between vertices u and v. It’s guaranteed that the graph is connected and doesn’t contain any self-loops or multiple edges.

Output

If you choose to solve the first problem, then on the first line print 1, followed by a line containing ⌈k2⌉ distinct integers not exceeding n, the vertices in the desired independent set.
If you, however, choose to solve the second problem, then on the first line print 2, followed by a line containing one integer, c, representing the length of the found cycle, followed by a line containing c distinct integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.

Examples

input
4 4 3
1 2
2 3
3 4
4 1
output
1
1 3
input
4 5 3
1 2
2 3
3 4
4 1
2 4
output
2
3
2 3 4
input
4 6 3
1 2
2 3
3 4
4 1
1 3
2 4
output
2
3
1 2 3
input
5 4 5
1 2
1 3
2 4
2 5
output
1
1 4 5

Note

In the first sample:
在这里插入图片描述
Notice that printing the independent set {2,4} is also OK, but printing the cycle 1−2−3−4 isn’t, because its length must be at most 3.
In the second sample:
在这里插入图片描述
Notice that printing the independent set {1,3} or printing the cycle 2−1−4 is also OK.
In the third sample:
在这里插入图片描述
In the fourth sample:
在这里插入图片描述

题目分析

有两种情况:

  1. 图为树时(即图中没有环时)
    此时可以用两个独立集来分别记录树中的点,如果某个点在a[0][]中,那么与它相邻的点都在a[1][]中。
    因为n>k,所以两个独立集必定至少有一个中含有至少(k+1)/2个点,因此该问题必定有解。最后输出某个独立集中(k+1)/2个点即可。
  2. 图不为树时(即图中有环时)
    这样就需要找环了,我们可以用deep[i]数组记录第i个点的深度(方便找环的长度),用pre[i]数组记录第i个点的父节点(方便保存路径从而进行回溯找点)。pos表示找到的环的最后一个位置。
    我们可以用dfs来进行搜索,当u的某个子节点的深度不为0时,就说明从u到该子节点形成了一个闭合的环,这个环的点数即为两点深度的差+1。
    1)当最小环的点数minv小于等于k时
    那么答案即为方案2,从pos位置开始,回溯k次即可。
    2)当最小环的点数minv大于k时
    那么答案为方案1,从pos位置开始,回溯(k+1)/2次即可(每次输出要往后回溯两次)。
代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=1e5+5;
int n,m,k;
vector<int> h[N];	//邻接表存图
vector<int> a[2];	//独立集
int minv=1e9,pos;
int pre[N],deep[N];
void DFS(int u,int fa,int de)	//图为树的情况
{a[de%2].push_back(u);for(int it:h[u]){if(it==fa) continue;DFS(it,u,(de+1)%2);}
}
void dfs(int u,int fa,int de)	//图非树的情况
{pre[u]=fa;deep[u]=de;for(int it:h[u]){if(it==fa) continue;if(deep[it])	//如果u的字节的的深度不为0{	//查看该环是否合法/该环的点数是否小于当前最小值if(deep[u]-deep[it]>0&&deep[u]-deep[it]+1<minv){minv=deep[u]-deep[it]+1;pos=u;		//记录这个最后位置}}else dfs(it,u,de+1);}
}
int main()
{scanf("%d%d%d",&n,&m,&k);for(int i=0;i<m;i++){int a,b;scanf("%d%d",&a,&b);h[a].push_back(b);h[b].push_back(a);}if(m==n-1)		//当m==n-1时,图为树{DFS(1,-1,0);if(a[0].size()<a[1].size())swap(a[0],a[1]);puts("1");int t=(k+1)/2;for(int i=0;i<t;i++){printf("%d ",a[0][i]);}}else	//否则图为非树{dfs(1,-1,1);if(minv<=k)	//1)的情况{puts("2");cout<<minv<<endl;while(minv--){printf("%d ",pos);pos=pre[pos];}}else	//2)的情况{puts("1");int t=k+1>>1;while(t--){printf("%d ",pos);pos=pre[pos];pos=pre[pos];}}}return 0;
}

http://chatgpt.dhexx.cn/article/3hNTYQCO.shtml

相关文章

Ehabs Last Corollary

Given a connected undirected graph with n n n vertices and an integer k k k, you have to either: either find an independent set that has exactly ⌈ k 2 ⌉ ⌈\frac{k}{2}⌉ ⌈2k​⌉ vertices.or find a simple cycle of length at most k k k. An independen…

Latent Variables的理解

加入我们有X&#xff0c;Y两个随机变量&#xff0c;他们的概率分布如下。要直接用一个函数还表示这个分布是比较困难的。 但我们发现这个分布可以分成三个聚类。如果我们给每个聚类编号为。 那么就是简单的高斯函数了。 这里z就是 加入latent variable的意义在于&#xff0c…

Variable(变量)

深度学习入门之PyTorch 作者 廖星宇

对条件变量(condition variable)的讨论

作者&#xff1a;王东 1.1 什么是条件变量和条件等待&#xff1f; 简单的说&#xff1a; 条件变量(condition variable)是利用线程间共享的全局变量进行同步的一种机制&#xff0c;主要包括两个动作&#xff1a;一个线程等待某个条件为真&#xff0c;而将自己挂起&…

C++ condition_variable用法

概述 condition_variable类似于信号量机制&#xff0c;实现了线程的等待和唤醒。 函数接口&#xff1a; wait() :阻塞等待的同时释放锁&#xff08;原子操作&#xff09;&#xff0c;还可以添加阻塞判断函数&#xff0c;详见代码 notify_all() : 唤醒所有阻塞等待的线程 no…

variable命令两种不同的使用方式“v_“和““的区别

大家好&#xff0c;我是小马老师。 本文介绍variable命令两种不同的使用方式&#xff1a;“v_“和”&"。 在lammps模拟中&#xff0c;variable命令用的相对比较多&#xff0c;可以根据需要定义不同的变量。 在使用自定义变量或者调用自定义变量的时候&#xff0c;lamm…

条件变量(Condition Variable)详解

条件变量(Condtion Variable)是在多线程程序中用来实现“等待->唤醒”逻辑常用的方法。举个简单的例子&#xff0c;应用程序A中包含两个线程t1和t2。t1需要在bool变量test_cond为true时才能继续执行&#xff0c;而test_cond的值是由t2来改变的&#xff0c;这种情况下&#x…

Java Variable 变量

目录 变量1. 变量的作用域a. 类级变量b. 成员变量c. 局部变量 2. 基本数据类型a. 按内存占用级数b. 自动类型转换i. 十进制转二进制 c. 强制类型转换i. (XXX)ii. parseXXX() 3. 引用数据类型 变量 同时被 final 和 static 修饰的变量是常量。 1. 变量的作用域 变量的作用域分…

About Variables

Assessing Variable Types “It all began with a variable”, the storyteller began. Just kidding, no one starts their stories that way, even though variables are where all data stories begin. Variables define datasets. They are the characteristics or attr…

pytorch的Variable和Parameters的联系和区别

文章目录 前言一、Variable二、Parameter总结 前言 首先看一下官方文档&#xff1a; 一、Variable torch.autograd.Variable Variable是对Tensor的封装&#xff0c;操作与tensor基本一致&#xff0c;不同的是&#xff0c;每一个Variable被构建的时候&#xff0c;都包含三个…

关于variable的理解

引用莫烦大大的话来说&#xff0c;tensor是一个鸡蛋&#xff0c;而variable相当于一个篮子&#xff0c;把tensor装起来 其中variable有三个参数&#xff1a; data&#xff1a;存储了Tensor&#xff0c;是本体的数据 grad&#xff1a;保存了data的梯度&#xff0c;本事是个Varia…

深度学习——Variable(已经过时了!)

一、简介 深度学习中使用pytorch框架&#xff0c;使用的数据一般是torch中的tensor形式。但是在参数表示中&#xff0c;一般是用variable变量形式。 二、variable的使用 &#xff08;1&#xff09;如何将tensor转化为variable pytorch1.0之后tensor和variable没有区别了&am…

Pytorch的Variable详解

pytorch两个基本对象&#xff1a;Tensor&#xff08;张量&#xff09;和Variable&#xff08;变量&#xff09; 其中&#xff0c;tensor不能反向传播&#xff0c;variable可以反向传播。 tensor的算术运算和选取操作与numpy一样&#xff0c;一次你numpy相似的运算操作都可以迁…

13.2 用Patsy创建模型

1、patsy适合描述statsmodels的线性模型&#xff0c;其公式是一个特殊的字符串语法&#xff0c;表示为模型设计矩阵 2、patsy.dmatrices函数接收一个公式字符串和一个数据集&#xff0c;为线性模型设计矩阵 3、Pasty对象可以直接传递到算法&#xff0c;如下面的最小二乘回归 4、…

Could not find a version

目标检测—教你利用yolov5训练自己的目标检测模型 我自己的另一篇配置文件和代码地址博客总结 CondaHTTPError: HTTP 000 CONNECTION FAILED for url 的问题终极解决方案 配置环境 安装pytorch 利用Anaconda安装pytorch和paddle深度学习环境pycharm安装—免额外安装CUDA和c…

statsmodels遇到的坑!!!

ImportError: cannot import name cached_data ModuleNotFoundError: No module named patsy python SimpleExpSmoothing 错误 statsmodels安装包地址&#xff1a; Python Extension Packages for Windows - Christoph Gohlke (uci.edu) 现已完美解决~

python数据处理包_python数据处理包安装

1> 本机环境 C:\Python27>python Python 2.7.12rc1 (v2.7.12rc1:13912cd1e7e8, Jun 12 2016, 05:57:31) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.…

手把手教你用Python进行回归(附代码、学习资料)

作者&#xff1a; GURCHETAN SINGH 翻译&#xff1a;张逸 校对&#xff1a;丁楠雅 本文共5800字&#xff0c;建议阅读8分钟。本文从线性回归、多项式回归出发&#xff0c;带你用Python实现样条回归。 我刚开始学习数据科学时&#xff0c;第一个接触到的算法就是线性回归。在把…

【Python数据分析】实践编写篇2:用Python进行回归分析与相关分析

目录 一、前言 1.1 回归分析 1.2 相关分析 二、代码的编写 2.1 前期准备 2.2 编写代码 2.2.1 相关分析 2.2.2 一元线性回归分析 2.2.3 多元线性回归分析 2.2.4 广义线性回归分析 2.2.5 logistic回归分析 三、代码集合 一、前言 1.1 回归分析 是用于研究分析某一变量受其…

chatgpt赋能Python-python_patsy

Python Patsy: 一个用于统计建模的Python库 什么是Patsy&#xff1f; Patsy是一个Python库&#xff0c;用于进行统计建模和数据预处理。Patsy的主要目的是将数据转换为适合统计建模的格式。它是一个基于公式的语言&#xff0c;通过描述预测变量和目标变量之间的关系&#xff…