题目描述

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;
}

#649 (Div. 2)D. Ehab‘s Last Corollary相关推荐

  1. Codeforces Round 649 (Rated for Div. 2)D. Ehab s Last Corollary详细题解(图论+简单环)

    树 边 : 树边: 树边:深度优先森林中的边.如果结点v是因对(u,v)的探索而首先被发现,则(u,v)是一条树边. 后 向 边 : 后向边: 后向边:后向边(u,v)是将结点u连接到其在深度优先树中 ...

  2. Codeforces Round #649 (Div. 2)C. Ehab and Prefix MEXs[排列的构造]

    C. Ehab and Prefix MEXs 题目大意: 解题思路:题目说保证a数组是非递减的,那么如果某位置a[i]!=a[i−1]a[i]!=a[i-1]a[i]!=a[i−1]那么这个位置ii ...

  3. 图论 ---- Codeforces Round #649 (Div. 2)D题[dfs求环+深度分层求图中独立集]

    D. Ehab's Last Corollary 题目大意: 就是给你一个联通图,你有两种选择 1.你可以输出包含⌈k2⌉\lceil{k\over2}\rceil⌈2k​⌉个顶点得独立点集,什么是独 ...

  4. CF1364D Ehab‘s Last Corollary

    CF1364D Ehab's Last Corollary 题意: 给定一张n个点,m条边的图和一个正整数k 在这张图中找到其中一项: 1.一个大小小于k的简单回路 2.一个恰好为k/2(向上取整)的 ...

  5. div2 649 D. Ehab‘s Last Corollary

    题目: 给一个连通图,让你找出等于ceil(k/2)的独立点集或者小于等于k的环(按顺序输出),有这么一个存在证明:最小环r>k时,独立集可以在环中找到,r<=k时,直接输出环.当m==n ...

  6. Codeforces Round #649 D. Ehab‘s Last Corollary 【思维,二分图,环】

    题目链接 题意 给出一个无向图,要找出 数量大于 ⌈ K 2 ⌉ \lceil \frac{K}{2} \rceil ⌈2K​⌉ 的点独立集 长度不大于 K K K 的环 保证有一种解 题解 如果 m ...

  7. Codeforces Round #628 (Div. 2) F. Ehab‘s Last Theorem dfs树

    传送门 文章目录 题意: 思路: 题意: 给你个nnn个点mmm条边的图,可以选择完成以下两个任务中的一个: (1)(1)(1)找出大小恰好为n\sqrt nn​的一个独立集. (2)(2)(2)找出 ...

  8. Codeforces Round #628 (Div. 2) E. Ehab‘s REAL Number Theory Problem 巧妙的质因子建图

    传送门 文章目录 题意: 思路: 题意: 给你nnn个数,每个数的因子个数不超过777个,选出最少的数使其乘积为平方数. n≤1e5n\le 1e5n≤1e5 思路: 由于因子不超过777个,所以由约 ...

  9. Codeforces Round #649 (Div.2)题解

    文章目录 A - XXXXX B - Most socially-distanced subsequence C - Ehab and Prefix MEXs A - XXXXX 题意:这个题让你找从 ...

最新文章

  1. ASP.NET 如何catch存储过程中抛出的异常信息
  2. mvc ajax提交html标签,Mvc提交表单的四种方法全程详解
  3. mschart 控件
  4. JAVA音视频解决方案----开源jtt1078源码与沟通群
  5. 【The Economist】2018经济学人杂志经济学人双语版阅读分享资源
  6. 使用sap BO sap BO报表制作财务三栏明细账
  7. flv.js简单使用示例
  8. ASPack压缩可执行文件
  9. 代码随想录第十四天 二叉树基础 LeetCode 144、145、94
  10. 如何使用Unity制作一款自己喜欢玩的游戏demo(Unity萌新的进阶技巧)
  11. 工作之路---记录LZ如何在两年半的时间内升为PM
  12. AltiumDesigner中plane和layer区别
  13. 后台指标计算返回数据格式说明5 - DRAWTEXT_FIX格式
  14. 绘制多边形--scratch编程二级
  15. 生成16位卡号和激活码
  16. rust使用睡袋_rust怎么弄睡袋 | 手游网游页游攻略大全
  17. 静态路由的基本原理讲解
  18. 前端未来之路在何方?
  19. 声学工程师应知道的150个声学基础知识(全篇)
  20. python实现人脸识别系统设计_基于ROS的人脸识别系统设计与实现

热门文章

  1. 深度学习框架之paddlepaddle
  2. 布法罗大学计算机中国博士,【博士】计算机网络方向招生 - 计算机科学技术系(CS)版 - 北大未名BBS...
  3. Java常用类字符串异常处理
  4. 鼠标滑过显示红色禁用符号
  5. 数据结构考研:数据、数据元素、数据项、数据对象、数据结构的区别/详细解释(计算机/软件工程/王道论坛)
  6. 简要分析“区块链+物联网”有哪些坎?
  7. java security_java.security.NoSuchAlgorithmException
  8. JavaScript知识要点 - Web前端开发必备
  9. 别羡慕苹果的小部件了,安卓也有!
  10. 后端开发如何快速转前端开发