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的差值 ...
随机推荐
- metro压缩和解压文件
在1.zip中增加一张新图片StorageFile jpg = await KnownFolders.PicturesLibrary.GetFileAsync("1.jpg"); ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- Div内部的内容超出部分显示省略号(仅仅只有一行内容)
效果如下:
- SQLServer基本操作
SQL 全名是结构化查询语言(Structured Query Language),是关系数据库管理系统的标准语言 1.分离数据库:将当前数据库文件和数据库引擎的关系断开,没有任何关系了,这样就可以随 ...
- mybatis 学习!
参考链接 http://www.mybatis.org/spring/zh/mappers.html http://www.cnblogs.com/fangjian0423/p/spring-myba ...
- python基础语法(二)
本文主要包括以下内容 函数 切片 迭代 列表生成式 生成器 迭代器 函数 定义函数 定义函数 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块 ...
- 学习JNDI
既然datasource对象有tomcat提供,那么我们如何在应用程序中获得它呢,tomcat把这个对象放在JNDI服务中, 并用一个名字把它关联起来,我们在应用程序中,只需通过JNDI搜索这个名字, ...
- candence 笔记总结
1.解决candece 启动后提示找不到licence文件的错误: candece的安装就不说了,按照破解步骤一步一步来就行了,但是装完后发现每次启动都会报错 "OrCAD Capture ...
- python字符串中插入变量