AtCoder:Hamiltonish Path(思维 & dfs)
来源:互联网 发布:良辰好景知几何 91baby 编辑:程序博客网 时间:2024/05/22 14:10
B - Hamiltonish Path
Time limit : 2sec / Memory limit : 256MB
Score : 500 points
Problem Statement
You are given a connected undirected simple graph, which has N vertices and M edges. The vertices are numbered 1 through N, and the edges are numbered 1through M. Edge i connects vertices Ai and Bi. Your task is to find a path that satisfies the following conditions:
- The path traverses two or more vertices.
- The path does not traverse the same vertex more than once.
- A vertex directly connected to at least one of the endpoints of the path, is always contained in the path.
It can be proved that such a path always exists. Also, if there are more than one solution, any of them will be accepted.
Constraints
- 2≤N≤105
- 1≤M≤105
- 1≤Ai<Bi≤N
- The given graph is connected and simple (that is, for every pair of vertices, there is at most one edge that directly connects them).
Input
Input is given from Standard Input in the following format:
N MA1 B1A2 B2:AM BM
Output
Find one path that satisfies the conditions, and print it in the following format. In the first line, print the count of the vertices contained in the path. In the second line, print a space-separated list of the indices of the vertices, in order of appearance in the path.
Sample Input 1
5 61 31 42 31 53 52 4
Sample Output 1
42 3 1 4
There are two vertices directly connected to vertex 2: vertices 3 and 4. There are also two vertices directly connected to vertex 4: vertices 1 and 2. Hence, the path 2 → 3 → 1 → 4 satisfies the conditions.
Sample Input 2
7 81 22 33 44 55 66 73 52 6
Sample Output 2
71 2 3 4 5 6 7
思路:直接看作者题解吧:
First, consider an arbitrary path. Check if this path satisfies the conditions. If yes, we are done.Otherwise, we can find a vertex that is adjacent to one of the endpoints of the path. We can extend the path using this vertex and we get a longer path. We repeat this process, and this process finishes after at most N − 1 steps since the length of any path is at most N − 1.For example, we can implement this idea as follows:1. Create a deque with single vertex: {1}.
2. Let x be the first element of the deque, and y be the last element of the deque. Repeat the following:(a)For each vertex z that is adjacent to x, check if z is contained in the path. If not, append it to the beginning of the deque and go to step 2.(b)For each vertex z that is adjacent to y, check if z is contained in the path. If not, append it to the end of the deque and go to step 2.
3. End the process. We found a solution.
In step 2(a) and step 2(b), each vertex is processed at most once. Therefore, this solution works in O(M).
# include <bits/stdc++.h>using namespace std;const int N = 1e5+3;vector<int>v[N];bool vis[N];list<int>L;void dfs(int u, bool des){ for(int i=0; i<v[u].size(); ++i) { int e = v[u][i]; if(!vis[e]) { if(des) L.push_front(e); else L.push_back(e); vis[e] = true; dfs(e, des); return; } }}int main(){ int n, m, a, b; scanf("%d%d",&n,&m); while(m--) { scanf("%d%d",&a,&b); v[a].push_back(b); v[b].push_back(a); } L.push_back(1); L.push_back(v[1][0]); vis[1] = vis[v[1][0]] = true; dfs(1,true); dfs(v[1][0], false); printf("%d\n",L.size()); for(auto it=L.begin(); it!=L.end(); ++it) printf("%d ",*it); printf("\n"); return 0;}
- AtCoder:Hamiltonish Path(思维 & dfs)
- AtCoder:Fennec VS. Snuke(dfs & 思维)
- AtCoder:Connected?(思维)
- AtCoder:AtCoder Group Contest(思维)
- Atcoder:TrBBnsformBBtion(思维 & 字符串)
- AtCoder:Big Array(思维)
- AtCoder:pushpush(STL & 思维)
- AtCoder:11(数论 & 思维)
- AGC 013 B Hamiltonish Path 模拟(deque)
- AtCoder:Median Pyramid Hard(思维)
- AtCoder:Median Pyramid Easy(思维)
- AtCoder:Tautonym Puzzle(思维 & 构造)
- AtCoder:Colorful Balls(思维 & 数论)
- AtCoder:guruguru(思维 & 差分)
- AtCoder:Chef Hates Palindromes(思维 & 字符串)
- Atcoder #2362 Splatter Painting(dfs+优化)
- 简单思维 AtCoder
- Atcoder Transit Tree Path
- 线材基础知识02——AWG线规
- python爬虫练手之斗图啦
- 这个demo你会编吗
- c++中的消息框messagebox()详细介绍及使用方法
- curl 命令详解
- AtCoder:Hamiltonish Path(思维 & dfs)
- 主进程杀不死
- L2-009. 抢红包
- 项目进度(三)
- JS一些基础算法题解析
- Android 5.0新增控件的总结
- Mycat和Atlas分库分表安装、配置指南
- 在vs code中使用ftp-sync插件实现客户端与服务器端代码的同步
- 深入理解Java注解类型(@Annotation)