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 ...
随机推荐
- 股票市场问题(The Stock Market Problem)
Question: Let us suppose we have an array whose ith element gives the price of a share on the day i. ...
- IOS深入学习(12)之Archiving
1 前言 本文介绍的是一个归档解档方法,也是编码和解码时候所做的事情,和如何进行,编码和归档其实就是将对象关系转化为字节流并且归档为特殊的文件,解码和解档是逆过程. 英文原文:http://blog. ...
- html a标签打开邮件
<a href="mailto:frotech@foxmail.com" target="_blank">frotech@foxmail.com&l ...
- OCP-1Z0-051-题目解析-第28题
28. Which two statements are true regarding constraints? (Choose two.) A. A foreign key cannot cont ...
- DOS命令大全--具体解释
在Linux和Windows下都能够用nslookup命令来查询域名的解析结果 DOS命令大全一)MD--建立子文件夹 1.功能:创建新的子文件夹 2.类型:内部命令 3.格式:MD[盘符:][路径名 ...
- android AsyncTask 详细例子(2)
超时处理 001 import java.util.Timer; 002 import java.util.TimerTask; 003 004 import android.app.Activi ...
- spark 高级算子
mapPartitionsWithIndex val func = (index: Int, iter: Iterator[(Int)]) => { iter.toList.map(x ...
- React初步
今天整理一下自己关于react的学习笔记. 什么是React? 学习某一个框架首先得知道这个框架是干什么的,它的特点是什么,有哪些优点和缺点. React有4个特点 组件化 虚拟DOM 单项数据流 j ...
- js改变div宽度
document.getElementById('Content_Right_id').style.width = document.documentElement.clientWidth - 250 ...
- vi 快捷键【转】【weber整理必出精品】
光标的移动 命令 光标移动 h或^h 向左移一个字符 j或^j或^n 向下移一行 k或^p 向上移一行 l或空格 向右移一个字符 G 移到文件的最后一行 nG 移到文件的第n行 w 移到下一个字的开头 ...