Ehabs Last Corollary

article/2025/10/28 13:31:30

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 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 n n, m m m, and k k k ( 3 ≤ k ≤ n ≤ 1 0 5 , n − 1 ≤ m ≤ 2 ⋅ 1 0 5 ) (3≤k≤n≤10^5, n−1≤m≤2⋅10^5) (3kn105,n1m2105) — the number of vertices and edges in the graph, and the parameter k k k from the statement.

Each of the next m m m lines contains two integers u u u and v v v ( 1 ≤ u , v ≤ n ) (1≤u,v≤n) (1u,vn) that mean there’s an edge between vertices u u u and v v 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 1 1, followed by a line containing ⌈ k 2 ⌉ ⌈k_2⌉ k2 distinct integers not exceeding n n n, the vertices in the desired independent set.

If you, however, choose to solve the second problem, then on the first line print 2 2 2, followed by a line containing one integer, c c c, representing the length of the found cycle, followed by a line containing c c c distinct integers not exceeding n n 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 {2,4} 2,4 is also OK, but printing the cycle 1 − 2 − 3 − 4 1−2−3−4 1234 isn’t, because its length must be at most 3 3 3.

In the second sample:

在这里插入图片描述

Notice that printing the independent set 1 , 3 {1,3} 1,3 or printing the cycle 2 − 1 − 4 2−1−4 214 is also OK.

In the third sample:

在这里插入图片描述
In the fourth sample:

在这里插入图片描述
水题一道。
分情况讨论,当所给图为一棵树的时候对其黑白染色,使得相同的颜色不相邻,从染色数较多的颜色对应的点中选 ⌈ k 2 ⌉ ⌈\frac{k}{2}⌉ 2k个输出即可。
否则先将图连成一棵树,然后选出端点深度最小的边与树构成一个简单环,如果环上的点的数量小于等于 k k k,则将环输出,否则将环上的点间隔输出 ⌈ k 2 ⌉ ⌈\frac{k}{2}⌉ 2k个。

#include<bits/stdc++.h>#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;inline int qr() {int f = 0, fu = 1;char c = getchar();while (c < '0' || c > '9') {if (c == '-')fu = -1;c = getchar();}while (c >= '0' && c <= '9') {f = (f << 3) + (f << 1) + c - 48;c = getchar();}return f * fu;
}const int N = 1e5 + 10, M = 2e5 + 10;
int head[N], ver[M << 1], Next[M << 1], tot;struct Union_Find {int fa[N];void init(int n) {repi(i, 0, n) fa[i] = i;}int find(int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);}void unit(int x, int y) {x = find(x), y = find(y);if (x != y) fa[y] = x;}bool same(int x, int y) {return find(x) == find(y);}
} uf;inline void add(int x, int y) {ver[++tot] = y;Next[tot] = head[x];head[x] = tot;
}vector<pii > e;
int n, m, k;
int col[N];struct LCA {int t, f[N][20], d[N];void dfs(int x) {for (int i = head[x]; i; i = Next[i]) {int y = ver[i];if (d[y])continue;d[y] = d[x] + 1;f[y][0] = x;for (int j = 1; j <= t; j++)f[y][j] = f[f[y][j - 1]][j - 1];dfs(y);}}inline int lca(int x, int y) {if (d[x] > d[y])swap(x, y);for (int i = t; i >= 0; i--)if (d[f[y][i]] >= d[x])y = f[y][i];if (x == y)return x;for (int i = t; i >= 0; i--)if (f[x][i] != f[y][i])x = f[x][i], y = f[y][i];return f[x][0];}int dis(int x, int y) {return d[x] + d[y] - 2 * d[lca(x, y)];}
} L;void colour(int x, int c) {col[x] = c;reps(x) {int y = ver[i];if (col[y])continue;colour(y, 3 - c);}
}int main() {n = qr(), m = qr(), k = qr();if (m == n - 1) {k = ceil((k * 1.0) / 2.0);repi(i, 1, n - 1) {int x = qr(), y = qr();add(x, y), add(y, x);}colour(1, 1);int cnt[3] = {0};repi(i, 1, n)cnt[col[i]]++;int c;c = cnt[1] > cnt[2] ? 1 : 2;puts("1");int ct = 0;repi(i, 1, n)if (col[i] == c) {printf("%d ", i), ct++;if (ct == k)break;}return 0;}uf.init(n);while (m--) {int x = qr(), y = qr();if (uf.same(x, y))e.pb({x, y});else add(x, y), add(y, x), uf.unit(x, y);}L.d[1] = 1, L.t = log2(n) + 1, L.dfs(1);pii res = {0, 0};for (auto it:e)if (!res.fi || L.d[it.fi] < L.d[res.fi] || L.d[it.fi] == L.d[res.fi] && L.d[it.se] < L.d[res.se])res = it;int x = res.fi, y = res.se;int lca = L.lca(x, y);vi seq;while (x != lca)seq.pb(x), x = L.f[x][0];seq.pb(lca);vi tmp;while (y != lca)tmp.pb(y), y = L.f[y][0];reverse(tmp.begin(), tmp.end());for (auto it:tmp)seq.pb(it);if (seq.size() <= k) {puts("2");pi(seq.size());for (auto it:seq)printf("%d ", it);} else {puts("1");k = ceil((k * 1.0) / 2.0);int ct = 0;for (int i = 0; i <= seq.size() - 1; i += 2) {printf("%d ", seq[i]), ct++;if (ct == k)break;}}return 0;
}

http://chatgpt.dhexx.cn/article/1GdGygLk.shtml

相关文章

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…

Simple Linear Regression:ONE

前言 对于一些库的说明 numpy&#xff1a;支持矩阵运算&#xff0c;在矩阵运算这方面可以完全替代基于向量编程的matlab pandas&#xff1a;这个是一个数据存储库&#xff0c;是以表单&#xff08;dataframe&#xff09;为基本单位&#xff0c;这个库的好处在于行列 索引并不是…