It’s the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let’s just stick with finding either.

Given a connected graph with n vertices, you can choose to either:

find an independent set that has exactly ⌈n−−√⌉ vertices.
find a simple cycle of length at least ⌈n−−√⌉.
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 you can always solve one of these problems, but it’s too long to fit this margin.

Input
The first line contains two integers n and m (5≤n≤105, n−1≤m≤2⋅105) — the number of vertices and edges in the graph.

Each of the next m lines contains two space-separated 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 ⌈n−−√⌉ 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 integers not exceeding n, the vertices in the desired cycle, in the order they appear in the cycle.

Examples
inputCopy
6 6
1 3
3 4
4 2
2 6
5 6
5 1
outputCopy
1
1 6 4
inputCopy
6 8
1 3
3 4
4 2
2 6
5 6
5 1
1 4
2 5
outputCopy
2
4
1 5 2 4
inputCopy
5 4
1 2
1 3
2 4
2 5
outputCopy
1
3 4 5

思路:和codeforces 649div2 D题差不多,都是dfs树的思路,但是这个题目比较难一些。
这个题目是dfs树找最大环。如果最大环符合第二个条件的话,就输出这个树中的点。如果不符合的话,就需要讨论了。
图片来源

代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;const int maxx=1e5+100;
const int maxm=2e5+100;
struct edge{int to,next;
}e[maxm<<1];
int head[maxm<<1];
int f[maxx],dis[maxx],vis[maxx];
vector<int> p[maxx];
int n,m,tot,ans=0,pos;inline void init()
{memset(f,0,sizeof(f));memset(dis,0,sizeof(dis));memset(vis,0,sizeof(vis));memset(head,-1,sizeof(head));tot=0;
}
inline void add(int u,int v)
{e[tot].to=v,e[tot].next=head[u],head[u]=tot++;
}
inline void dfs(int u,int fa,int h,int cor,int num)
{vis[u]=1;f[u]=fa;dis[u]=h;p[h%(num-1)].push_back(u);for(int i=head[u];i!=-1;i=e[i].next){int to=e[i].to;if(to==fa) continue;else if(vis[to]){if(ans<abs(dis[u]-dis[to])+1){ans=abs(dis[u]-dis[to])+1;pos=u;}}else dfs(to,u,h+1,cor^1,num);}
}
int main()
{scanf("%d%d",&n,&m);init();int x,y;for(int i=1;i<=m;i++){scanf("%d%d",&x,&y);add(x,y);add(y,x);}int num=sqrt(n);if(num*num<n) num+=1;dfs(1,0,0,0,num);if(m==n-1||ans<num){cout<<1<<endl;for(int i=0;i<num;i++){if(p[i].size()>=num){for(int j=0;j<num;j++) cout<<p[i][j]<<" ";cout<<endl;return 0;}}}else{cout<<2<<endl<<ans<<endl;for(;ans;ans--) cout<<pos<<" ",pos=f[pos];cout<<endl;}
}

努力加油a啊,(o)/~

Last Theorem CodeForces - 1325F(dfs树找最大环+思维)相关推荐

  1. CodeForces - 1325F Ehabs Last Theorem(dfs树找最大环)

    题目链接:点击查看 题目大意:给出一个由 n 个结点和 m 条边构成的无向图,再给出一个 k ,需要在图中完成下面任意一种操作: 找到一个大小恰好为   的独立集 找到一个大小至少为  的环 题目分析 ...

  2. CodeForces - 1364D Ehabs Last Corollary(dfs树找最小环)

    题目链接:点击查看 题目大意:给出一个由 n 个结点和 m 条边构成的无向图,再给出一个 k ,需要在图中完成下面任意一种操作: 找到一个大小恰好为  的独立集 找到一个大小不超过 k 的环 题目分析 ...

  3. Last Corollary CodeForces - 1364D(dfs树找最小环)

    思路:对于所给的图形来说,可以分为树图和非树图. 两种图的做法不一样,因为树图是没有环的,只有第二种选择. 对于树图来说,我们找出树的每一层有哪几个点,并且保存起来.然后分别查看(0,2,4-)层的总 ...

  4. Codeforces 19E DFS 树

    题意 传送门 Codeforces 19E Fairy 题解 若图中不存在非二分图的连通分量,则任意一边删除后仍是二分图:若图中存在大于一个非二分图的连通分量,则不可能通过删除一条边使之变为二分图.故 ...

  5. Codeforces 962F DFS 树

    题意 传送门 Codeforces 962F Simple Cycles Edges 题解 任一个简单环,都可以通过取 DFSDFSDFS 树单条非树边与树边构成的环的集合 SSS 的任意子集异或得到 ...

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

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

  7. 图论复习——dfs树,点双,边双,强连通分量

    知识点 dfs树 对一个图运行 dfs 算法,每个点uuu的父亲定义为第一次遍历uuu时的前驱结点,若无则为根. 无向图的 dfs树 没有横叉边. 有向图的 dfs树 横叉边方向唯一,总是从后访问的点 ...

  8. 【BZOJ1064】[Noi2008]假面舞会 DFS树

    [BZOJ1064][Noi2008]假面舞会 Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择 ...

  9. 【BZOJ4424】Cf19E Fairy DFS树

    [BZOJ4424]Cf19E Fairy Description 给定 n 个点,m 条边的无向图,可以从图中删除一条边,问删除哪些边可以使图变成一个二分图. Input 第 1 行包含两个整数 n ...

最新文章

  1. python中的随机函数怎么用_Python中随机数的使用方法有那些?
  2. 模拟usb重新插拔_Android 10带来黑科技 检测手机USB接口是否有液体或者是否过热...
  3. 深度丨人工智能的最大未解之谜是什么?
  4. 心得体悟帖---15、我的灵魂
  5. 最新动态,电信屏蔽Godaddy部分DNS服务
  6. Swift中文件和图片上传处理
  7. HTTP和HTTPS回顾
  8. ES6 知识点及常考面试题
  9. c#12星座速配代码_原来12星座的软件工程师是这样的
  10. java多行字符串变量_java – Scala – 如何在多行字符串文字中使用变量
  11. 【数据结构】图的存储结构—邻接矩阵
  12. VSCode配置cpp环境
  13. 正则表达式与自动机c语言,用有限自动机实现正则表达式的匹配
  14. Android-原笔迹钢笔手写的探索与开发
  15. 信息安全技术 实验四 木马及远程控制技术
  16. 记录String.valueOf()和toString()注意问题
  17. 腾讯云部署DevOps
  18. 速腾聚创激光雷达部署
  19. JZOJ5454. 【NOIP2017提高A组冲刺11.5】仔细的检查
  20. 手动删除百度全家桶流氓软件

热门文章

  1. IOS基础之NSString,NSMutableString,NSArray的基本使用
  2. Java之文件流操作的文件读写
  3. kruskal算法java_克鲁斯卡尔算法(Kruskal)的java实现
  4. js byte数组_这么骚的 js 代码,不怕被揍么
  5. GAN实现半监督学习
  6. 福建信息技术学院计算机系男生宿舍怎么样,广西职业技术学院宿舍怎么样
  7. 迭代求斐波那契数列python_python中的迭代器(以斐波那契数列为主讲解)
  8. Android开发之动态添加WebView实现进度条标题栏展示效果
  9. html搜索框美化代码单词,CSS 漂亮搜索框美化代码
  10. android 双人黑白棋开发博客,黑白棋 - 软件资讯 - 课堂党年级博客