hdu5424 Rikka with Graph II

给一个n个节点n条边的无向图G,试判断图中是否存在哈密顿路径。

若G中存在哈密顿路径l,则路径端点度数不小于1,其余点度数不小于2。

则G存在哈密顿路径的必要条件:

1)G连通;

2)G中度数为1的点不超过两个。

考虑到简单连通图中边的数目m不超过n,

1)若 m = n - 1,则可从任一度数为1的点搜索即可;

2)若 m = n,多余的一条边连接哈密顿路径上的两点,从任一度数为1的点搜索即可。

3)若不存在度数为1的点,从任一点开始搜索。

复杂度O(n)。

http://acm.hdu.edu.cn/showproblem.php?pid=5424

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4
 5 using namespace std;
 6
 7 const int maxn = 1e3 + 10;
 8
 9 struct Edge{
10     int to, next;
11 }edge[2 * maxn];
12
13 int n, d1, d0, N, S;
14 bool vis[maxn], ans;
15 int d[maxn], head[maxn];
16
17 void addEdge(int u, int v){
18     edge[N].next = head[u];
19     edge[N].to = v;
20     head[u] = N++;
21 }
22
23 bool inMap(int u, int v){
24     for(int i = head[u]; i + 1; i = edge[i].next){
25         int v1 = edge[i].to;
26         if(v1 == v) return 1;
27     }
28     return 0;
29 }
30
31 bool dfs(int u, int cnt){
32     if(cnt == n) return ans = 1;
33     if(ans) return 1;
34     for(int i = head[u]; i + 1; i = edge[i].next){
35         int v = edge[i].to;
36         if(vis[v]) continue;
37         vis[v] = 1;
38         dfs(v, cnt + 1);
39         vis[v] = 0;
40     }
41 }
42
43 int main(){
44     while(~scanf("%d", &n)){
45         memset(d, 0, sizeof d);
46         memset(head, -1, sizeof head);
47         N = 0;
48         for(int i = 0, u, v; i < n; i++){
49             scanf("%d%d", &u, &v);
50             if(u != v && !inMap(u, v)){
51                 addEdge(u, v);
52                 addEdge(v, u);
53                 ++d[u], ++d[v];
54             }
55         }
56         d0 = d1 = 0;
57         S = 1;
58         for(int i = 1; i <= n; i++){
59             if(!d[i]) ++d0;
60             else if(d[i] == 1){
61                 ++d1;
62                 S = i;
63             }
64         }
65         if(d0 + d1 > 2){
66             puts("NO");
67             continue;
68         }
69         ans = 0;
70         memset(vis, 0, sizeof vis);
71         vis[S] = 1;
72         dfs(S, 1);
73         puts(ans ? "YES" : "NO");
74     }
75     return 0;
76 }

时间: 2024-07-31 14:30:33

hdu5424 Rikka with Graph II的相关文章

hdu 5424 Rikka with Graph II 哈密顿通路

Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 367    Accepted Submission(s): 90 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situatio

HDU 5424——Rikka with Graph II——————【哈密顿路径】

Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1051    Accepted Submission(s): 266 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situati

HDU5424——DFS——Rikka with Graph II

http://acm.hdu.edu.cn/showproblem.php?pid=5424 /* 对于一个连通图,有N个顶点,如果有N条边,那么肯定有环 因为N个点如果不成环最多N-1条边...然后找最小度的点DFS就行... */ /************************************************ * Author :Powatr * Created Time :2015-8-31 17:29:13 * File Name :HDU5424.cpp *****

哈密顿图 BestCoder Round #53 (div.2) 1003 Rikka with Graph II

题目传送门 题意:判断是否为哈密顿图 分析:首先一种情况是不合法的:也就是度数为1的点超过2个:合法的有:,那么从度数为1的点开始深搜,如果存在一种走法能够走完n个点那么存在哈密顿路 收获:学习资料 代码: /************************************************ * Author :Running_Time * Created Time :2015-8-29 20:37:34 * File Name :C.cpp *******************

hdu 5424 Rikka with Graph II (BestCoder Round #53 (div.2))(哈密顿通路判断)

http://acm.hdu.edu.cn/showproblem.php?pid=5424 哈密顿通路:联通的图,访问每个顶点的路径且只访问一次 n个点n条边 n个顶点有n - 1条边,最后一条边的连接情况: (1)自环(这里不需要考虑): (2)最后一条边将首和尾连接,这样每个点的度都为2: (3)最后一条边将首和除尾之外的点连接或将尾和出尾之外的点连接,这样相应的首或尾的度最小,度为1: (4)最后一条边将首和尾除外的两个点连接,这样就有两个点的度最小,度都为1 如果所给的图是联通的话,那

HDU 5422:Rikka with Graph

Rikka with Graph Accepts: 353 Submissions: 1174 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 勇太有一张nn个点mm条边的无向图,每一条边的长度都是1.现在他想再在这张图上连上一条连接两个不同顶点边,使得1号点到nn号点的最短路尽可能的短

HDU 6090 Rikka with Graph —— 2017 Multi-University Training 5

Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is

HDU 5831 Rikka with Parenthesis II(六花与括号II)

HDU 5831 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description 题目描述 As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math ta

hdu 5425 Rikka with Tree II(暴力)

题目链接:hdu 5425 Rikka with Tree II 直接枚举就好了,当概率极小时贡献值可以忽略. #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxn = 1e5 + 5; int N,