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的差值 ...
随机推荐
- C++构造函数初始化顺序
[本文链接] http://www.cnblogs.com/hellogiser/p/constructor-order.html 1.构造函数.析构函数与拷贝构造函数介绍 构造函数 构造函数不能有返 ...
- MongoDB 3.0 用户创建
摘要: MongoDB 3.0 安全权限访问控制,在添加用户上面3.0版本和之前的版本有很大的区别,这里就说明下3.0的添加用户的方法. 环境.测试: 在安装MongoDB之后,先关闭auth认证,进 ...
- 内存管理_JAVA内存管理
Java虚拟机规范中试图定义一种Java内存模型(Java Memory Model,JMM)来屏蔽各个硬件平台和操作系统的内存访问差异,以实现让Java程序在各种平台下都能达到一致的内存访问效果.那 ...
- Lake Counting_深度搜索_递归
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30414 Accepted: 15195 D ...
- 穹举,迭代,while循环。
所有循环 必要条件 : 初始条件(注意初始条件的位置) 循环条件 循环体 状态改变: 1.穷举 将所有可能性全部全部走一遍,使用IF筛选出满足的情况 使用循环语句 for ...
- 【leetcode】Remove Nth Node From End of List(easy)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- mac系统下如何解压.car文件
纯手打: 1.去github下载demo然后运行 github地址:https://github.com/steventroughtonsmith/cartool 2.找到项目下cartool的位置 ...
- 使用rdesktop连接Windows远程桌面
rdesktop 使用简单,windows也不和装什么服务端,是要把远程桌面共享打开就行了 安装 yum -y install rdesktop 具体使用方法要先打开终端,然后输入以下命令: rdes ...
- Qt Creator 中关于调试器的设置
Qt Creator3.4.2 Based on Qt 5.5.0 (MSVC 2013, 32 bit) 在安装了VS2013的电脑上能够自动识别VS编译器,却不能识别调试器 需要下载一个wdk,安 ...
- sqlplus 设置
set heading offset line 40001.设置页面显示总行数show pagesize; //首先查看目前的pagesize,默认是14set pagesize 100; //将pa ...