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 ...
随机推荐
- centerOS安装rkhunter
rkhunter是专业检测系统是否感染rootkit的一个工具: rkhunter-1.4.2.tar.gz 解压后直接安装: #./installer.sh --layout defualt --i ...
- WPF多线程问题
最近碰到这种多线程问题都是在WPF项目中. 1. 问题是这样.有个一主界面线程,然后background线程启动,这个background线程试图去修改主界面里面的数据. 造成死锁. 调用过程,主界面 ...
- for的用法
第一次看到这么用,哈哈,就记下 for (var control = ["程", "陈", "是"]; control[0]; contro ...
- Spark Yarn-cluster与Yarn-client
摘要 在Spark中,有Yarn-Client和Yarn-Cluster两种模式可以运行在Yarn上,通常Yarn-cluster适用于生产环境,而Yarn-Cluster更适用于交互,调试模式,以下 ...
- [Angular 2] Event in deep
This lesson talks about the benefits of using the parens-based (click) syntax so that Angular 2 can ...
- QT正则表达式---针对IP地址
判断合法IP的QT正则表达式: bool IsIPaddress(QString ip) { QRegExp rx2("(//d+)(//.)(//d+)(//.)(//d+)(//.)(/ ...
- 解决System.Data.SQLite兼容32位和64位问题
将当前说明文档的目录下的x64.x86目录和System.Data.SQLite.dll文件复制到您的应用程序根目录中(注意更新引用,引用System.Data.SQLite.dll即可,两目录中的不 ...
- PixelFormat 枚举
成员名称 说明 Alpha 像素数据包含没有进行过自左乘的 alpha 值. Canonical 默认像素格式,每像素 32 位. 此格式指定 24 位颜色深度和一个 8 位 alpha 通道. Do ...
- WndPric的使用方法
protected override void WndProc(ref Message m) { const int WM_SYSCOMMAND = 0x0112; const int SC_CLOS ...
- Key lock 的秘密
研究死锁,或者观察sp_lock,有时候最恼人的莫过于你看到下面研究成果的key lock,但是却不知道究竟是哪个page 哪个row被lock住了: Exec sp_lock: 就说上面的key ...