判图的连通性(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 ...
随机推荐
- 自定义searchview的编辑框,搜索按钮,删除按钮,光标等
//指定某个私有属性 Field mSearchHintIconField = argClass.getDeclaredField("mSearchHintIcon"); mSea ...
- LeeCode-Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- Impala 3、Impala、Hbase整合
Impala可以通过Hive外部表方式和HBase进行整合,步骤如下: • 步骤1:创建hbase 表,向表中添加数据 create 'test_info', 'info' put 'test_inf ...
- 为什么Myeclipse 提示Project 'bankmanager' is missing required library,myeclipse项目上红叉 但内部红叉
应该是正在使用的项目是从网上下的或者别人那里直接拷贝导致的,解决办法: 下一个mysql-connector-java-5.1.22-bin.jar或者是最新版,最好放项目目录里 右键点项目,Buil ...
- Samsung K9F1G08U0D SLC NAND FLASH简介(待整理)
Samsung K9F1G08U0D,数据存储容量为128M,采用块页式存储管理.8个I/O引脚充当数据.地址.命令的复用端口.详细:http://www.linux-mtd.infradead.o ...
- Unity 3D 调用摄像头捕获照片 录像
1,要想调用摄像头首先要打开摄像头驱动,如果用户允许则可以使用. 2,定义WebCamTexture的变量用于捕获单张照片. 3,连续捕获须启用线程. 实现代码: using UnityEngine; ...
- 使用Unicorn-engine 续1
续上次,在ubuntu server 14.04交叉编译好后,下一步就是在windows上使用了. 在windows上,我主要是用python进行分析程序,因此我最初安装的是官网上的 unicorn- ...
- javax inect
Spring 3 and JSR-330 @Inject and @Named example By mkyong | September 16, 2012 | Viewed : 86,399 tim ...
- WPS页面设置
以前使用WPS的时候遇到一些问题: 比如我输入一个英文的时候它总是自动的给我首字母大写,但是某些情况下我不想这样: 从VS中复制代码的时候不希望他吧那些颜色复制下来: 还有我输入1回车后它自动给我输入 ...
- URL 对特殊字符的处理
看到很多人说可以通过转移字符来进行转义,避免URL在请求的时候出错. 现在有了更好的方法了.不然还不得把半个ASCII码表给进行转义了! import java.io.UnsupportedEncod ...