题目大意: 在 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的更多相关文章

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

    Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  2. 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条边,最后一条边的 ...

  3. 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 ...

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

    31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  5. HDU 5831 Rikka with Parenthesis II (栈+模拟)

    Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  6. hdu 5831 Rikka with Parenthesis II 线段树

    Rikka with Parenthesis II 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  7. HDU 5631 Rikka with Graph 暴力 并查集

    Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...

  8. HDU 5422 Rikka with Graph

    Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. HDU 6090 Rikka with Graph

    Rikka with Graph 思路: 官方题解: 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...

随机推荐

  1. xcode插件——新建cocos2dx工程

    个人制作的一个创建cocos2dx工程的xcode小插件 按照readme安装一下即可. 创建工程后,将自动弹出finder到工程目录. 弹出窗口:

  2. c++命名空间using

    #include<iostream> namespace run1 { ; } namespace run2 { ; void show() { std::cout << x ...

  3. 分页标签:pager-taglib使用指南

    一简介, Pager-taglib,支持多种风格的分页显示.实际上她是一个Jsp标签库,为在JSP上显示分页信息而设计的一套标签,通过这些标签的不同的组合,会形成多种不一样的分页页面,风格各异,她自带 ...

  4. (转)Building MariaDB on Mac OS X using Homebrew

    https://kb.askmonty.org/en/building-mariadb-on-mac-os-x-using-homebrew/ Work has been done to provid ...

  5. Salt安装

    salt-master安装 [salt-master]# yum install salt-master 或者 curl -L http://bootstrap.saltstack.org | sud ...

  6. js_day14

  7. 使用kindeditor 注意

    ValidateRequest="false"引用编辑器要在最上端加入上面的话

  8. silverlight visifire控件图表制作——silverlight 后台方法打印

    一.后台方法 1.添加引用:using System.Windows.Printing; 2.全局变量://定义图片和文本打印变量  PrintDocument printImage; 3.构造方法体 ...

  9. OC基础 单例

    #undef  AS_SINGLETON   #define AS_SINGLETON( __class ) \       + (__class *)sharedInstance;      #un ...

  10. STL set容器添加结构体并排序

    #include <iostream> #include <string> #include <cstring> //strcpy #include <cst ...