D. Ehab’s Last Corollary


题目大意:

就是给你一个联通图,你有两种选择
1.你可以输出包含⌈k2⌉\lceil{k\over2}\rceil⌈2k​⌉个顶点得独立点集,什么是独立点集:集合中任意两点都没有边相连
2.你要找到顶点数<=k<=k<=k的环


解题思路:

求环很简单就是dfs一遍如果深度大的点指向了深度小的点那么就是有环,现在是怎么求独立集?
我们一个想法就是将有边相连的点就将它们分开那么我们就可以按深度进形分层,如果两个点有边按照dfs序肯定不在同一层


#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define hash Hash
#define next Next
#define count Count
#define pb push_back
#define f first
#define s second
using namespace std;
const int N = 4e5+10, mod = 1e9 + 7;
const double eps = 1e-10;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
template<typename T> void read(T &x)
{x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args)
{read(first);read(args...);
}
vector<int> G[N], ans, d[N];
int n, m, k;
int dep[N], path[N];void find(int u, int v)
{while(u != v){ans.pb(u);u = path[u];}ans.pb(u);cout << 2 << "\n";cout << ans.size() << "\n";for(auto it : ans)cout << it << " ";exit(0);return;
}void dfs(int deep, int u)
{dep[u] = deep;d[deep].pb(u);for(auto it : G[u]){if(dep[it] == -1) {path[it] = u;dfs(deep+1,it);}else if(it != path[u] && dep[u] - dep[it] + 1 <= k && dep[u] > dep[it]) find(u,it);}return;
}int main()
{ms(dep,-1);read(n,m,k);while(m --){int l, r;read(l,r);G[l].pb(r); G[r].pb(l);}dfs(0,1);int need = (k+1)/2;for(int i = 0; i <= n && need; i += 2)for(auto it : d[i]){ans.pb(it);if(--need == 0) break;}if(need){need = (k+1)/2;ans.clear();for(int i = 1; i <= n && need; i += 2)for(auto it : d[i]){ans.pb(it);if(--need == 0) break;}}cout << "1\n";for(auto i : ans)cout << i << " ";return 0;
}

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

  1. Codeforces Round 700 (Div. 2) B题 英雄杀怪兽

    Codeforces Round 700 (Div. 2) B题 链接: https://codeforces.com/contest/1480/problem/B 大致意思: n组数据,每组数据的第 ...

  2. Codeforces Round #774 (Div. 2)E题题解

    Codeforces Round #774 (Div. 2) E. Power Board 题目陈述 有一个n×m(1≤n,m≤106)n\times m(1\le n,m\le10^6)n×m(1≤ ...

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

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

  4. Codeforces Round #807 (Div. 2)补题

    C. Mark and His Unfinished Essay https://codeforces.com/contest/1705/problem/C 会卡long long,下面解法62ms过 ...

  5. Codeforces Round #723 (Div. 2)补题

    水题,只需要将序列分成两部分即可,一部分是大的,一部分是小的. #include <cstdio> #include <iostream> #include <algor ...

  6. Codeforces Round #552 (Div. 3) C题

    题目链接 http://codeforces.com/contest/1154/problem/C 题目 C. Gourmet Cat time limit per test 1 second mem ...

  7. 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 ...

  8. 双指针--Codeforces Round #645 (Div. 2) d题

    D. The Best Vacation 题目大意: 算出连续x天最多的拥抱,一个月第i号就有i个拥抱 思路:双指针,扫描过去(每个月每个月的计算,最后超出的部分再一天一天算) 代码 : #inclu ...

  9. 思维--找规律--Codeforces Round #645 (Div. 2) c题

    C. Celex Update 题目大意:给出两点的坐标,找出不同的路径的总数(路径数字总和不同) 思路:根据观察向下走比向右走的增加幅度加1,所以在第i步 向下 对sum的影响是 n-i+1 所以最 ...

最新文章

  1. 图片加载完成再执行事件
  2. Button的使用(十):ImageButton
  3. 二.Sql语言的分类及运算符
  4. word List40
  5. [导入]javascript总结
  6. 如何在SQL Server 2016中使用R导入/导出CSV文件
  7. 操,escape sequence的输入方法我以前找过一次,这次又忘了,又找了一次,记下来,...
  8. andpods授权码订单号分享_Axure 9.0学生免费授权申请详细步骤
  9. Go语言 channel 管道 阻塞 死锁 经典问题
  10. java移位运算_Java 移位运算符
  11. 金山毒霸 2011 SP2 论坛内测开始
  12. 微信emoji表情及输入法自带表情存储与显示
  13. double类型判断是否相等
  14. Loadrunner:管理员权限启动报错“win10为了对电脑进行保护,已经阻止此应用”
  15. HDU 5713 K个联通块(状压DP)
  16. 江西省中小学生学籍管理-登录(1)
  17. 机械寿命预测,多步预测
  18. 电脑手机生产力提升操作
  19. zbb20181006 maven配置阿里云中央仓库
  20. 2010 我的求职经历(4)

热门文章

  1. 创业基础(第8章 新企业的创办与管理) 来自高校:全国大学生创新创业实践联盟 分类:创新创业 学习规则:按序学习
  2. 使用OpenCV加载TensorFlow2模型
  3. 链表问题2——在双链表中删除倒数第K个节点
  4. MaxCompute Studio使用心得系列7—作业对比
  5. 网站优化基础教程:发布外链常见的五种方式!
  6. SQL Server中的命名规范(个人使用)
  7. UBOOT启动内核过程
  8. phpMyAdmin FTP iptables Selinux
  9. 记一次数据中心云平台系统项目实施
  10. 在ASP.NET中备份和还原数据库