Codeforces Round #161 (Div. 2) D. Cycle in Graph(无向图中找指定长度的简单环)
题目链接:http://codeforces.com/problemset/problem/263/D
思路:一遍dfs即可,dp[u]表示当前遍历到节点u的长度,对于节点u的邻接点v,如果v没有被访问过,则继续访问,否则计算dp[u] - dp[v] + 1是否大于等于K + 1,如果是,就说明找到了这样一个符合要求的环,然后将回退,回退的时候将环上的节点标记即可,否则,就继续找。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <deque>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (100000 + 10);
int N, M, K, st, found, flag[MAX_N], vis[MAX_N], dp[MAX_N];
vector<int > g[MAX_N];
vector<int > ans; void dfs(int u, int father, int len)
{
vis[u] = 1;
dp[u] = len;
REP(i, 0, (int)g[u].size()) {
int v = g[u][i];
if (v == father) continue;
if (vis[v] && dp[u] - dp[v] + 1 >= K + 1) {
st = v;
flag[u] = 1;
ans.push_back(u);
return;
}
if (!vis[v]) dfs(v, u, len + 1);
if (st) {
if (found) return;
if (u == st) found = 1;
ans.push_back(u);
flag[u] = 1;
return;
}
}
} int main()
{
while (cin >> N >> M >> K) {
ans.clear();
FOR(i, 1, N) flag[i] = vis[i] = 0, g[i].clear();
while (M--) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
st = found = 0;
dfs(1, -1, 0);
cout << (int)ans.size() << endl;
REP(i, 0, (int)ans.size()) {
if (i == (int)ans.size() - 1) cout << ans[i] << endl;
else cout << ans[i] << ' ';
}
}
return 0;
}
Codeforces Round #161 (Div. 2) D. Cycle in Graph(无向图中找指定长度的简单环)的更多相关文章
- 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph
题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...
- Codeforces Round #161 (Div. 2)
A. Beautiful Matrix 即相当于求1到中心位置\((2,2)\)的曼哈顿距离. B. Squares 排序,取倒数第\(k\)个即可. C. Circle of Numbers 固定\ ...
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35 36组数据 ...
- Codeforces Round #133 (Div. 2), A.【据图推公式】 B.【思维+简单dfs】
Problem - 216A - Codeforces Problem - B - Codeforces A Tiling with Hexagons 题意: 给出a b c ,求里面有多少个六边形 ...
- Codeforces Round #378 (Div. 2) A B C D 施工中
A. Grasshopper And the String time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces Round #236 (Div. 2) C. Searching for Graph(水构造)
题目大意 我们说一个无向图是 p-interesting 当且仅当这个无向图满足如下条件: 1. 该图恰有 2 * n + p 条边 2. 该图没有自环和重边 3. 该图的任意一个包含 k 个节点的子 ...
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp ...
- Codeforces Round #242 (Div. 2) C. Magic Formulas (位异或性质 找规律)
题目 比赛的时候找出规律了,但是找的有点慢了,写代码的时候出了问题,也没交对,还掉分了.... 还是先总结一下位移或的性质吧: 1. 交换律 a ^ b = b ^ a 2. 结合律 (a^b) ^ ...
- Codeforces Round #372 (Div. 1) B. Complete The Graph (枚举+最短路)
题目就是给你一个图,图中部分边没有赋权值,要求你把无权的边赋值,使得s->t的最短路为l. 卡了几周的题了,最后还是经群主大大指点……做出来的…… 思路就是跑最短路,然后改权值为最短路和L的差值 ...
随机推荐
- zju3545
AC自动机+状态压缩DP 注意:相同的串可能出现多次,如果匹配成功则将各次权值加和. #include <cstdio> #include <queue> #include & ...
- 3.nodejs权威指南--文件
1. 文件 1.1 读写整个文件 1.1.1 读 var fs = require('fs'); fs.readFile('./test.txt',function(err,data){ if(err ...
- ffmpeg-20160508-git-bin-v2
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- h5页面的公共css
/*reset*/body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,tex ...
- Django~Views
In Django, web pages and other content are delivered by views. To get from a URL to a view, Django u ...
- Linux/Unix命令
MAC 中自定义环境变量 打开:nano .bash_profile 查看:cat text 保存退出:Ctrl+C,Y #在.bash_profile 中添加tree alias tree=&quo ...
- 针对Xcode的警告忽略消除处理
一.问题描述 html代码如下 <html> <head> <meta charset="utf-8"/> <title>我的网页& ...
- 51nod 1117 聪明的木匠 (哈夫曼树)
题目:传送门. 题意:中文题. 题解:就是构造一颗哈夫曼树,数据结构里的知识. #include <iostream> #include <cstdio> #include & ...
- 【XLL API 函数】xlSheetNm
从外部引用包含的工作表ID返回工作表或宏表名称,或是当前表名称. 原型 Excel12(xlSheetNm, LPXLOPER12 pxRes, 1, LPXLOPER12 pxExtref); 参数 ...
- 苹果的软件/系统盘 网站 http://www.panduoduo.net/u/bd-369186934/2
http://www.panduoduo.net/u/bd-369186934/2