题目大意:给定一个 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的更多相关文章

  1. 【题解】cycle

    [题解]cycle 题目描述 给定一个无向图,求一个环,使得环内边权\(\div\)环内点数最大. 数据范围 \(n \le 5000\) \(m\le 10000\) \(Solution\) 考虑 ...

  2. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  3. 论文阅读笔记(十八)【ITIP2019】:Dynamic Graph Co-Matching for Unsupervised Video-Based Person Re-Identification

    论文阅读笔记(十七)ICCV2017的扩刊(会议论文[传送门]) 改进部分: (1)惩罚函数:原本由两部分组成的惩罚函数,改为只包含 Sequence Cost 函数: (2)对重新权重改进: ① P ...

  4. 【POJ】【2125】Destroying the Graph

    网络流/二分图最小点权覆盖 果然还是应该先看下胡伯涛的论文…… orz proverbs 题意: N个点M条边的有向图,给出如下两种操作.删除点i的所有出边,代价是Ai.删除点j的所有入边,代价是Bj ...

  5. 【转】使用Boost Graph library(二)

    原文转自:http://shanzhizi.blog.51cto.com/5066308/942972 让我们从一个新的图的开始,定义一些属性,然后加入一些带属性的顶点和边.我们将给出所有的代码,这样 ...

  6. 【转】使用Boost Graph library(一)

    转自:http://shanzhizi.blog.51cto.com/5066308/942970 本文是一篇译文,来自:http://blog.csdn.net/jjqtony/article/de ...

  7. 【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5775   Accepted: 2678   ...

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

  9. 【CodeChef】Chef and Graph Queries

    Portal --> CC Chef and Graph Queries Solution 快乐数据结构题(然而好像有十分优秀的莫队+可撤销并查集搞法qwq) 首先考虑一种方式来方便一点地..计 ...

随机推荐

  1. 解决.Net Mvc跨域请求问题

    针对ASP.NET MVC和ASP.NET Web API两种项目类型 1.针对ASP.NET MVC,只需要在web.config中添加如下的内容即可 <system.webServer> ...

  2. hadoop的缺点

    Hadoop的限制 Hadoop只能执行批量处理,并且只以顺序方式访问数据.这意味着必须搜索整个数据集,即使是最简单的搜索工作.

  3. 使用PHP进行SOCKET编程

    一.SOCKET原理图 二.SOCKET常用函数 1.创建socket函数: resource socket_create ( int $domain , int $type , int $proto ...

  4. Bootstrap之信息记录

    Bootstrap中文网: http://www.bootcss.com/ 上面有一些资料和范例 实例精选: https://v3.bootcss.com/getting-started/#examp ...

  5. vue axios 封装(一)

    封装一: 'use strict' import axios from 'axios' import qs from 'qs' import NProgress from 'nprogress' im ...

  6. web font

    gfx.downloadable_fonts.enabled

  7. 跳转语句之continue

    js里面有两个跳转语句,一个是continue,一个是break.由于这两个跳转语句都是用于循环当中,因此他们也就只能用于while.for.do…while语句中,当然了,break多加一个swit ...

  8. c++ string类型的定义及方法

    1.c++ 有两种风格的字符串形式  c风格字符串  定义及初始化  char a[]={'h','e','l','l','o','\0'}  或者  char a[]="hello&quo ...

  9. python绘制图形

      python能快速解决日常工作中的小任务,比如数据展示. python做数据展示,主要用到matplotlib库,使用简单的代码,就可以很方便的绘制折线图.柱状图等.使用Java等,可能还需要配合 ...

  10. spring boot项目基本结构

    /==================================Controller @Controller public class SimpleController { @Autowired ...