判图的连通性(dfs,并查集)
一.无向图
欧拉回路:每个顶点度数都是偶数
欧拉路:所有点度数为偶数,或者只有2个点度数为奇数
当然判连通性
hdu 1878 欧拉回路 两种判连通的方法
dfs
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define N 1010
int degree[N],n,m;
bool visit[N];
vector<int>edge[N];
void dfs(int point){
int i,j,p;
visit[point]=1;
for(i=0;i<edge[point].size();i++){
p=edge[point][i];
if(!visit[p])
dfs(p);
}
}
int main(int argc, char** argv) {
int i,j,a,b;
while(scanf("%d",&n)!=EOF&&n){
scanf("%d",&m);
for(i=1;i<=n;i++){
degree[i]=0;
edge[i].clear();
}
for(i=0;i<m;i++){
scanf("%d%d",&a,&b);
if(a!=b){
edge[a].push_back(b);
edge[b].push_back(a);
degree[a]++;
degree[b]++;
}
}
bool flag=0;
for(i=1;i<=n;i++){
if(degree[i]&1){
printf("0\n");
flag=1;
break;
}
}
if(flag)
continue;
memset(visit,0,sizeof(visit));
dfs(1);
for(i=1;i<=n;i++){
if(!visit[i]){
flag=1;
break;
}
}
if(flag)
printf("0\n");
else
printf("1\n"); }
return 0;
}
并查集:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define N 1010
int degree[N],n,m;
vector<int>edge[N];
int father[N];
int find(int x){
while(x!=father[x])
x=father[x];
return x;
}
void unite(int a,int b){
father[b]=find(a);
}
int main(int argc, char** argv) {
int i,j,a,b;
while(scanf("%d",&n)!=EOF&&n){
scanf("%d",&m);
for(i=1;i<=n;i++){
degree[i]=0;
edge[i].clear();
father[i]=i;
}
for(i=0;i<m;i++){
scanf("%d%d",&a,&b);
if(a!=b){
edge[a].push_back(b);
edge[b].push_back(a);
degree[a]++;
degree[b]++;
if(find(a)!=find(b))
unite(a,b);
}
}
bool flag=0;
for(i=1;i<=n;i++){
if(degree[i]&1){
printf("0\n");
flag=1;
break;
}
}
if(flag)
continue;
int x=father[1];
for(i=2;i<=n;i++){
if(find(i)!=x){
flag=1;
break;
}
}
if(flag)
printf("0\n");
else
printf("1\n");
}
}
判图的连通性(dfs,并查集)的更多相关文章
- ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol  bfs/dfs/并查集
		
Untrusted Patrol Time Limit: 3 Seconds Memory Limit: 65536 KB ...
 - BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)
		
BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...
 - Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)
		
<题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...
 - 1021. Deepest Root (25)——DFS+并查集
		
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
 - 1013 Battle Over Cities (25分) DFS  |  并查集
		
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
 - 分珠(dfs+并查集)
		
1140 分珠 时间限制:500MS 内存限制:65536K提交次数:24 通过次数:18 题型: 编程题 语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不 ...
 - CodeForces - 455C Civilization (dfs+并查集)
		
http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...
 - PAT甲题题解-1021. Deepest Root (25)-dfs+并查集
		
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
 - hdu 1198 Farm Irrigation(深搜dfs || 并查集)
		
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
 
随机推荐
- g++ error:   expected ‘)’ before ‘*’ token
			
原本*号前面的类型是我用typedef自定义的类型的,MyType* const p: 发生这样的错误是,编译器根本不知道MyType是什么东西,这是我在C++多重继承中遇到的.MyType是我在基类 ...
 - Android专项面试训练题(一)
			
1.下面不可以退出Activity的是?(D) A.finish() B.抛异常强制退出 C.System.exit(0) D.onStop() 解析: A, finish() 方法就是退出activ ...
 - javascript 判断系统设备
			
<script> function detectOS() { var sUserAgent = navigator.userAgent; var isWin = (navigator.pl ...
 - FlashBack-SCN-TIMESTAMP
			
一.基于时间(as of timestamp)的flashback1.创建表create table flash_tab(id,vl) as select rownum,oname from ( se ...
 - putty保持连接不自动段开
			
经常在网上看到有人说自己利用putty工具登录服务器总是连接不上,这样的情况自己在刚接触putty时也遇到过.在 Connection 里面有个 Seconds between keepaliaves ...
 - mysql 5.7忘记密码处理
			
vi /etc/my.cnf在[mysqld]下面增加一行skip-grant-tables 重启 /etc/init.d/mysqld restart /usr/local/mysql/bin/m ...
 - a:hover span 隐藏/显示 问题
			
:hover是我们在CSS设计中最常运用的伪类之一,许多绚丽效果的实现离不开伪类:hover,比如我们常见的纯CSS菜单.相册效果等等. 或许用了这么久的伪类:hover,还有部分朋友还不完全了解ho ...
 - Git GUI简易使用教程
			
Git GUI简易使用教程:先上图~~~~~ 一. 先从添加文件开始讲(先上图): 1.选中要添加的文件,点击菜单栏的“提交”,选中“缓存为提交”,
 - 【初级坑跳跳跳】第一个应用布局学习的代码运行时出错(manifest里未将activity先注册,控件错误)
			
首先,根据书中想要实现的结果,看了下书中代码,大致知道布局是怎么样的,然后根据图片自己写xml, 1.运行时第一个坑是 忘记在AndroidManifest.xml里先注册activity,导致运行时 ...
 - centos添加sudoers
			
CentOS添加sudoers 1.chmod 740 /etc/sudoers 2. ## Allow root to run any commands anywhere root A ...