【CF263D】Cycle in Graph
题目大意:给定一个 N 个点,M 条边的无向图,保证图中每个节点的度数大于等于 K,求图中一条长度至少大于 K 的简单路径,输出长度和路径包含的点。
题解:依旧采用记录父节点的方式进行找环,不过需要记录一下节点的深度,即:只有当环的大小满足需要的大小,再进行操作,否则忽略那些环即可。
代码如下
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll sqr(ll x){return x*x;}
inline ll read(){
ll x=0,f=1;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
return f*x;
}
vector<int> G[maxn],cyc;
int n,m,k,ans;
int dep[maxn],fa[maxn];
void read_and_parse(){
n=read(),m=read(),k=read();
for(int i=1;i<=m;i++){
int a=read(),b=read();
G[a].pb(b),G[b].pb(a);
}
}
bool dfs(int u){
for(int i=0;i<G[u].size();i++){
int v=G[u][i];if(v==fa[u])continue;
if(!fa[v]){
fa[v]=u,dep[v]=dep[u]+1;
if(dfs(v))return 1;
}else if(dep[u]-dep[v]>=k){
ans=dep[u]-dep[v]+1;
for(int j=u;j!=v;j=fa[j])cyc.pb(j);
return cyc.pb(v),1;
}
}
return 0;
}
void solve(){
fa[1]=1,dfs(1);
printf("%d\n",ans);
for(int i=0;i<cyc.size();i++)printf("%d ",cyc[i]);
}
int main(){
read_and_parse();
solve();
return 0;
}
【CF263D】Cycle in Graph的更多相关文章
- 【题解】cycle
[题解]cycle 题目描述 给定一个无向图,求一个环,使得环内边权\(\div\)环内点数最大. 数据范围 \(n \le 5000\) \(m\le 10000\) \(Solution\) 考虑 ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 论文阅读笔记(十八)【ITIP2019】:Dynamic Graph Co-Matching for Unsupervised Video-Based Person Re-Identification
论文阅读笔记(十七)ICCV2017的扩刊(会议论文[传送门]) 改进部分: (1)惩罚函数:原本由两部分组成的惩罚函数,改为只包含 Sequence Cost 函数: (2)对重新权重改进: ① P ...
- 【POJ】【2125】Destroying the Graph
网络流/二分图最小点权覆盖 果然还是应该先看下胡伯涛的论文…… orz proverbs 题意: N个点M条边的有向图,给出如下两种操作.删除点i的所有出边,代价是Ai.删除点j的所有入边,代价是Bj ...
- 【转】使用Boost Graph library(二)
原文转自:http://shanzhizi.blog.51cto.com/5066308/942972 让我们从一个新的图的开始,定义一些属性,然后加入一些带属性的顶点和边.我们将给出所有的代码,这样 ...
- 【转】使用Boost Graph library(一)
转自:http://shanzhizi.blog.51cto.com/5066308/942970 本文是一篇译文,来自:http://blog.csdn.net/jjqtony/article/de ...
- 【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5775 Accepted: 2678 ...
- 【LeetCode】133. Clone Graph (3 solutions)
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
- 【CodeChef】Chef and Graph Queries
Portal --> CC Chef and Graph Queries Solution 快乐数据结构题(然而好像有十分优秀的莫队+可撤销并查集搞法qwq) 首先考虑一种方式来方便一点地..计 ...
随机推荐
- javap -v没有显示LocalVaribleTable
时隔多日,终于找到为什么javap -v .class文件没有LocalVariableTable出现 因为默认的javac编译没有生成相关的调试信息,这里我们可以通过javac -help查看指令帮 ...
- Python __slots__ 作用
参考:https://blog.csdn.net/u010733398/article/details/52803643 https://blog.csdn.net/sxingming/artic ...
- python学习笔记(9)--函数
函数定义: def <函数名>(<参数(0个或多个)>): 函数体 return <返回值> 参数有非可选参数,和可选参数,可选参数放在参数列表的最后,可以为可选参 ...
- 前端框架framework和库library的一点区别和记录
本篇纯文字,无关代码,只是一点概念的记录 关于所谓前端 首先学的是HTML5.CSS3.JavaScript这三个 之后接触了一下UI框架,如layui和bootstrap 目前是打算去学VUE和an ...
- python 网络编程 IO多路复用之epoll
python网络编程——IO多路复用之epoll 1.内核EPOLL模型讲解 此部分参考http://blog.csdn.net/mango_song/article/details/4264 ...
- linux 地址解析协议 arp
随便转载,保留出处:http://www.cnblogs.com/aaron-agu/ arp –na #查看 arp –s 123.253.68.209 00:19:56:6F:87:D4 #添加
- SQL Server与SQL Server Express的区别
SQL Server Express 2005(以下简称 SQLExpress) 是由微软公司开发的 SQL Server 2005(以下简称 SQL2005)的缩减版,这个版本是免费的,它继承了 S ...
- undefined reference to `cv::VideoCapture
出现opencv链接的问题原因: 1. 路径设置不正确,caffe会优先搜索Makefile.config里面的环境设置 2. anaconda2装的opencv和配置的opencv路径不一致 比如, ...
- Data Science With R In Visual Studio
R Projects Similar to Python, when we installed the data science tools we get an “R” section in our ...
- 【算法】串的模式匹配算法(KMP)
串的模式匹配算法 问题: 求子串位置的定位函数如何写? int index(SString S,SString T,int pos); 给定串S,子串T,问T在 ...