HDU-1878 欧拉回路 欧拉回路
题目链接:https://cn.vjudge.net/problem/HDU-1878
题意
中文题,而且就是单纯的欧拉回路
思路
- 判断连通图
用并查集会很好,bfs亦可
一时脑抽用bfs过了这个题,数据还是太弱 - 出度==入度
代码
并查集查连通
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=1000;
struct Node{
int parent, rank;
Node(int parent=0, int rank=0):
parent(parent), rank(rank) {}
}node[maxn+5];
int n;
int find(int x){
return (node[x].parent==x)?x:(node[x].parent=find(node[x].parent));
}
void join(int a, int b){
a=find(a); b=find(b);
if (a==b) return;
if (node[a].rank==node[b].rank) node[a].rank++;
if (node[a].rank>node[b].rank) node[b].parent=a;
else node[a].parent=b;
}
bool connect(void){
for (int i=2; i<=n; i++)
if (find(1)!=find(i)) return false;
return true;
}
int main(void){
while (scanf("%d", &n)==1 && n){
int m, cnt=0, vis[maxn+5]={0};
bool set[maxn+5]={false};
for (int i=1; i<=n; i++) node[i]=Node(i, 0);
scanf("%d", &m);
for (int i=0, a, b; i<m; i++){
scanf("%d%d", &a, &b);
join(a, b);
vis[a]++; vis[b]++;
}
int flag=false;
for (int i=1; i<=n; i++)
if (vis[i]%2) {flag=true; break;}
if (flag==false && !connect()) flag=true;
printf("%d\n", (flag)?0:1);
}
return 0;
}
BFS查连通
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=1000;
struct Edge{
int from, to;
bool vis;
Edge(int from=0, int to=0, int vis=false):
from(from), to(to), vis(vis) {}
};
vector<Edge> edge;
vector<int> G[maxn+5];
int n;
inline void addEdge(int from, int to){
edge.push_back(Edge(from, to, false));
G[from].push_back(edge.size()-1);
G[to].push_back(edge.size()-1);
}
bool connective(void){
int cnt=1; bool vis[maxn+5]={false};
queue<int> que;
que.push(1); vis[1]=true;
while (que.size()){
int from=que.front(); que.pop();
if (cnt==n) return true;
for (int i=0; i<G[from].size(); i++){
Edge &e=edge[G[from][i]];
int to=(e.to==from)?e.from:e.to;
if (e.vis) continue;
vis[to]=true; e.vis=true; cnt++;
que.push(to);
}
}return false;
}
int main(void){
while (scanf("%d", &n)==1 && n){
int m, vis[maxn+5]={0};
memset(G, 0, sizeof(G));
scanf("%d", &m);
for (int i=0, a, b; i<m; i++){
scanf("%d%d", &a, &b);
addEdge(a, b);// G[a][b]++; G[b][a]++;
vis[a]++; vis[b]++;
}
int flag=false;
for (int i=1; i<=n; i++)
if (vis[i]%2) {flag=true; break;}
if (flag==false && connective()==0) flag=true;
printf("%d\n", (flag)?0:1);
}
return 0;
}
并查集
Time | Memory | Length | Lang | Submitted |
---|---|---|---|---|
93ms | 1524kB | 1198 | G++ | 2018-03-14 17:22:31 |
BFS
Time | Memory | Length | Lang | Submitted |
---|---|---|---|---|
124ms | 7016kB | 1445 | G++ | 2018-03-14 17:03:18 |
HDU-1878 欧拉回路 欧拉回路的更多相关文章
- HDU 1878 欧拉回路(判断欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878 题目大意:欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一 ...
- HDU 1878 欧拉回路
并查集水题. 一个图存在欧拉回路的判断条件: 无向图存在欧拉回路的充要条件 一个无向图存在欧拉回路,当且仅当该图所有顶点度数都是偶数且该图是连通图. 有向图存在欧拉回路的充要条件 一个有向图存在欧拉回 ...
- HDU 1878 欧拉回路 图论
解题报告:题目大意,给出一个无向图,判断图中是否存在欧拉回路. 判断一个无向图中是否有欧拉回路有一个充要条件,就是这个图中不存在奇度定点,然后还要判断的就是连通分支数是否为1,即这个图是不是连通的,这 ...
- hdu 1878 无向图的欧拉回路
原题链接 hdu1878 大致题意: 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个无向图,问是否存在欧拉回路? 思路: 无向图存在欧拉回路的条件:1.图是连 ...
- HDU 1878 欧拉回路(无向图的欧拉回路)
欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU - 1878 欧拉回路 (连通图+度的判断)
欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是否存在欧拉回路? Input 测试输入包含若干测试用例.每个测试用例的第1行给出两个正整数,分别是节点数 ...
- HDU 1878(1Y) (判断欧拉回路是否存在 奇点个数为0 + 一个联通分量 *【模板】)
欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 1878 欧拉回路(联通<并查集> + 偶数点)
欧拉回路Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- The Best Path HDU - 5883(欧拉回路 && 欧拉路径)
The Best Path Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- hdu 1878
http://acm.hdu.edu.cn/showproblem.php?pid=1878 题意:就是判断这个图是不是一个欧拉回路的一个题, 思路:我觉得这个题可以用并查集判环加上判断每个点的度就行 ...
随机推荐
- 用pigz来加速解压tar.gz
兼容tar.gz 多线程的解压工具, 用于解压大文件时使用. https://zlib.net/pigz/ 方法: 1. 安装pigz 2. 使用tar时,选择pigz tar --use-compr ...
- fatal error C1003: error count exceeds number; stopping compilation解决方法
[error]C1003: error count exceeds 100; stopping compilation ...winnt.h 在项目工程中添加#include<windows.h ...
- 【原创】不重启was server重新加载应用class文件
类装入和更新检测(Class loading and update detection settings)菜单路径:Applications < Application Types < W ...
- ActiveMQ学习笔记(1)----初识ActiveMQ
1. 什么是ActiveMQ? ActiveMQ是Apache推出的,一款开源的,完全支持JMS1.1和j2ee1.4规范的JMS Provider实现的消息中间件(Message Oriented ...
- 3ds Max实例教程-顽皮的小孩
本教程介绍使用3ds Max制作设计一个顽皮的小孩,这个作品的灵感来源于作者的亲身经历,也是以真实人物为原型做出来这么一个小人. 作者: Claudius Vesting 使用软件:3ds Max,P ...
- LaTex的几种数学符号字体以及相关说明
\mathrm is the normal upright Roman font \mathnormal is the normal math italic font: $\mathnormal{a} ...
- yum下载的rpm包离线安装
#修改yum设置,让rpm包缓存到本地 vi /etc/yum.conf #修改keepcache为1 keepcache=1 #清空yum缓存 yum clean all #安装你要离线安装的rpm ...
- mayan 游戏 search
纯搜索,,,模拟,,还不算太难,,就是细节略繁琐 首先因为题目要求保证字典序,所以显然把右边的块换到左边不如把左边的块换到右边优, 所以可以进行不小规模的剪枝,之后显然交换两块相同的色块没有意义,至此 ...
- openssh 升级到7.5p1
1. 参照: http://www.cnblogs.com/xiegj/p/5669800.html http://blog.csdn.net/u011080082/article/details/5 ...
- hihocoder 1124 : 好矩阵 dp
好矩阵 时间限制:3000ms 单点时限:1000ms 内存限制:256MB 描写叙述 给定n, m.一个n × m矩阵是好矩阵当且仅当它的每一个位置都是非负整数,且每行每列的和 ≤ 2.求好矩阵的个 ...