判图的连通性(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 ...
随机推荐
- xargs mv命令使用方法:ls *.mp3 |xargs -i mv {} /tmp
ls *.mp3 |xargs -i mv {} /tmp 或者 find . -name "*.mp3" -exec mv {} /tmp \;
- XML文档形式&JAVA抽象类和接口的区别&拦截器过滤器区别
XML文档定义有几种形式?它们之间有何本质区别?解析XML文档有哪几种方式? a: 两种形式 dtd schemab: 本质区别:schema本身是xml的,可以被XML解析器解析(这也是从DTD上发 ...
- oracle获取某一字段字符串长度
用length方法 select t.* from tp_area t where substr(t.area_id,0,2)='03' and length(t.area_id)>2
- Extjs实现进度条
做Extjs开发中,往往后台程序可能要执行一段时间才能得到返回结果,加入进度条可以提高客户体验度,以下为两种便捷的方式: 1.提交数据前用Ext.Msg.wait('提示','正在处理数据,请稍候') ...
- hihoCoder #1234 : Fractal(数学简单题)
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 This is the logo of PKUACM 2016. More specifically, the logo i ...
- #include<string.h>
#include<string.h> 1 strcpy #include <string.h> char *strcpy(char *str1, const char *str ...
- Dice Notation(模拟)
Dice Notation Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- 小函数,大智慧,php的isset和empty
Disset()函数 一般用来检测变量是否设置 bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: FALSE N ...
- SQL Server 日期函数:某天是星期几?
[鹏城万里] 发表于 www.sqlstudy.com 要得到某一天是星期几,需要用到 SQL Server 中的日期函数:datename(). 今天是星期几,例子 1: set language ...
- centos网速特别慢的最佳解决的方法 - 关闭ipv6
我使用了centOS,可是发现网速实在是卡得差点儿不能上网,连百度都打不开,可是win却飞快. 后来想到偶然记得有一次看过一段话,说到关闭ipv6,測试来一下,果然有效,关闭来ipv6打开网速飞快. ...