HDU 5424 Rikka with Graph II
题目大意: 在 N 个点 N 条边组成的图中判断是否存在汉密尔顿路径。
思路:忽略重边与自回路,先判断是否连通,否则输出“NO”,DFS搜索是否存在汉密尔顿路径。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
#define INF 0x7fffffff
int n,ans,vis[1010],f[1010],cnt;
vector<int> adj[1010];
#define pii pair<int,int>
map<pii,int> MAP;
int find(int x){
return f[x] == x ? x : f[x] = find(f[x]);
}
void init(){
int i;
for(i=0;i<=n;i++){
adj[i].clear();
f[i] = i;
}
}
void judge(int u) {
vis[u] = 1;
cnt ++;
int d = adj[u].size();
for(int i = 0; i < d; i ++) {
int v = adj[u][i];
if(!vis[v]) {
judge(v);
}
}
}
bool DFS(int u, int cnt) {
vis[u] = 1;
if(cnt == n) return true;
int d = adj[u].size();
for(int i = 0; i < d; i ++) {
int v = adj[u][i];
if(!vis[v]) {
if(DFS(v, cnt + 1)) return true;
vis[v] = 0;
}
}
return false;
}
int main(){
int i,j,a,b,x,y,degree[1010];
while(scanf("%d",&n) == 1){
memset(degree,0,sizeof(degree));
MAP.clear();
init();
for(i=1;i<=n;i++){
scanf("%d%d",&a,&b);
if(MAP.find(make_pair(a,b)) == MAP.end() && MAP.find(make_pair(b,a)) == MAP.end() && a != b){
degree[a] ++;
degree[b] ++;
MAP[make_pair(a,b)] = 1;
MAP[make_pair(b,a)] = 1;
adj[a].push_back(b);
adj[b].push_back(a);
x = find(a);
y = find(b);
if(x != y)
f[x] = y;
}
}
int k,MIN = INF;
for(i=1;i<=n;i++)
if(degree[i] < MIN){
k = i;
MIN = degree[i];
}
int num2 = 0;
for(i=1;i<=n;i++){
if(degree[i] == 1)
num2 ++;
}
if(num2 > 2){
printf("NO\n");
continue ;
}
cnt = 0;
memset(vis,0,sizeof(vis));
judge(1);
if(cnt != n){
printf("NO\n");
continue;
}
memset(vis,0,sizeof(vis));
if(DFS(k,1))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
HDU 5424 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/Othe ...
- 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条边,最后一条边的 ...
- hdu 5424 Rikka with Graph II(dfs+哈密顿路径)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so h ...
- HDU 5831 Rikka with Parenthesis II(六花与括号II)
31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 5831 Rikka with Parenthesis II (栈+模拟)
Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
- hdu 5831 Rikka with Parenthesis II 线段树
Rikka with Parenthesis II 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
- HDU 5631 Rikka with Graph 暴力 并查集
Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...
- HDU 5422 Rikka with Graph
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 6090 Rikka with Graph
Rikka with Graph 思路: 官方题解: 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...
随机推荐
- UBI(unsorted block image )块管理
一.介绍 ubi是unsorted block images的缩写,是由IBM开发设计的,它与ubifs有不同的含义,ubifs是一种文件系统(nokia开发的):而ubi是一种块管理工具,工作在mt ...
- 必看谷歌HTML/CSS规范
背景 这篇文章定义了 HTML 和 CSS 的格式和代码规范,旨在提高代码质量和协作效率. 通用样式规范 协议 省略图片.样式.脚本以及其他媒体文件 URL 的协议部分( http:,https: ) ...
- [Redux] Redux: Extracting Container Components -- AddTodo
Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...
- 一个不错的PPT,扁平化设计,开放资源,要的进来
开了那么多的博客,没做啥资源贡献,今天共享一个不错的PPT模板.例如以下图所看到的,须要的话留下邮箱 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFp ...
- JavaScripts学习日记——BOM
IE 3.0 和 Netscape Navigator 3.0 提供了一种特性 - BOM(浏览器对象模型),可以对浏览器窗口进行访问和操作.使用 BOM,开发者可以移动窗口.改变状态栏中的文本以及执 ...
- 《第一行代码》学习笔记11-活动Activity(9)
1.android:theme,是用于给当前活动指定主题的,Android系统内置很多主题可以选择,@android:style/Theme.Dialog则是 让DialogActivity使用对话框 ...
- Android发送通知栏通知
/** * 发送通知 * * @param message */ @SuppressWarnings("deprecation") @SuppressLint("NewA ...
- 整理的sql sever一些数据库查询面试题
当然,我整理的只是一些常见的面试题,具体数据库就不给了,相信大家能看懂!!! --2列出EMPLOYEES表中各部门的部门号,最高工资,最低工资 select Max(salary) as '最高工资 ...
- iOS8怎么降级到iOS7,苹果iOS8怎么刷回iOS7
iOS8怎么降级到iOS7,苹果iOS8怎么刷回iOS7 http://jingyan.baidu.com/article/e75aca855c5c19142edac6e9.html 威锋APPLE工 ...
- IOS 创建App的最佳捷径
简网App工场 ----------------创建App的最佳捷径