Codeforces Round #161 (Div. 2)-D. Cycle in Graph
来源:互联网 发布:windjview mac 编辑:程序博客网 时间:2024/05/18 00:52
原题链接
从一个点开始深搜,记录每个节点的深度,若i和j有点相连,且d[i] - d[j] >= k则输出j到i的路径
#include <bits/stdc++.h>#define maxn 100005#define MOD 1000000007using namespace std;typedef long long ll;vector<int> v[maxn];int d[maxn], pre[maxn];int n, m, k;bool dfs(int j, int f){for(int i = 0; i < v[j].size(); i++){int h = v[j][i];if(h == f)continue;if(d[h] == 0){d[h] = d[j] + 1;pre[h] = j;if(dfs(h, j))return true;}else{if(d[h] < d[j] && d[j] - d[h] >= k){int s = d[j] - d[h] + 1;printf("%d\n", s);printf("%d", j);s--;while(s--){printf(" %d", pre[j]);j = pre[j];}return true;}}}return false;}int main(){//freopen("in.txt", "r", stdin);int a, b;scanf("%d%d%d", &n, &m, &k);for(int i = 0; i < m; i++){scanf("%d%d", &a, &b);v[a].push_back(b);v[b].push_back(a);}for(int i = 1; i <= n; i++){if(d[i] == 0){d[i] = 1;if(dfs(i, -1)) return 0;}}}
0 0
- Codeforces Round #161 (Div. 2)-D. Cycle in Graph
- contest: Codeforces Round #161 (Div. 2), problem: (D) Cycle in Graph 图论
- Codeforces Round #161 (Div. 2) D. Cycle in Graph(无向图中找指定长度的简单环)
- Codeforces 263 D. Cycle in Graph 环
- Codeforces 263 D. Cycle in Graph
- Codeforces 263D Cycle in Graph【Dfs】
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle
- Codeforces Round #434 Div.1 D graph
- cf#161-D-Cycle in Graph- dfs
- Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
- D. Psychos in a Line Codeforces Round #189 (Div. 2)
- Codeforces Round #326 (Div. 2) D. Duff in Beach
- Codeforces Round #375 (Div. 2) D. Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 贪心
- Codeforces Round #237 (Div. 2)__Restore Graph
- String与StringBuffer,StringBuilder的区别
- Azure Token Lifetime
- android--Intent和IntentFilter详解
- Ehcache工具类 EhcacheUtil
- HTML之认识标签(一)
- Codeforces Round #161 (Div. 2)-D. Cycle in Graph
- HDU 1254 推箱子(BFS + DFS)
- 【SpringBoot】SpringBoot核心-基本配置
- appium(二)简单的demo
- ArcGIS空间分析要点学习
- Fragment Transactions & Activity State Loss
- 5分钟学会两种侧滑菜单栏
- Hadoop、Spark等5种大数据框架对比,你的项目该用哪种?
- linux下生成动态库和静态库的方法