CodeForces - 788B Weird journey 欧拉路
题意:给定n个点,m条边,问能否找到多少条符合条件的路径。需要满足的条件:1.经过m-2条边两次,剩下两条边1次 2.任何两条路的终点和起点不能相同。
欧拉路的条件:存在两个或者0个奇度顶点。
思路:首先把给每条边都再增加一条边,所有点的度数都是偶数。每条边分为普通边和自环边。
1.删去两条没有公共顶点的普通边,会有四个点的度数变成奇数,不符合欧拉路。
2.删去两条有公共顶点的普通边,会有两个点的度数成为奇数,符合
2.删去一个自环边和一个普通边,会有两个点的度数成为奇数,符合
4.删去两条自环边,所有顶点的度数全是偶数,符合
在此之前必须保证所有的边是连通的,否则根本不可能走完所有的边。判断连通性可用dfs或者并查集。
AC代码
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 1e6 + 5;
vector<int>G[maxn];
int n, m;
int vis[maxn], mark[maxn];
void dfs(int u) {
vis[u] = 1;
for(int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if(!vis[v]) dfs(v);
}
}
int main() {
while(scanf("%d%d", &n, &m) == 2) {
for(int i = 1; i <= n; ++i) G[i].clear();
memset(mark, 0, sizeof(mark));
int x, y, loop = 0;
for(int i = 0; i < m; ++i) {
scanf("%d%d", &x, &y);
if(x != y) {
G[x].push_back(y);
G[y].push_back(x);
}
else {
++loop;
mark[x] = 1;
G[x].push_back(y); //自环
}
}
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; ++i) {
if(G[i].size()) {
dfs(i);
break;
}
}
//是否所有边已经连通
for(int i = 1; i <= n; ++i) {
if(G[i].size() && !vis[i]) { //不连通
printf("0\n");
return 0;
}
}
LL ans = 0;
//相邻普通边
for(int i = 1; i <= n; ++i) {
int f = G[i].size();
if(f) {
if(mark[i]) --f; //减去自环边
ans += (LL)f * (f-1) / 2;
}
}
ans += (LL)loop * (m-loop); //自环和普通边
ans += (LL)loop * (loop-1) / 2; //两条自环边
printf("%lld\n", ans);
}
return 0;
}
如有不当之处欢迎指出!
CodeForces - 788B Weird journey 欧拉路的更多相关文章
- Codeforces 789D Weird journey - 欧拉路 - 图论
Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his mot ...
- Codeforces Round #407 (Div. 2) D. Weird journey(欧拉路)
D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- CodeForces 788B - Weird journey [ 分类讨论 ] [ 欧拉通路 ]
题意: 给出无向图. good way : 仅有两条边只经过一次,余下边全经过两次的路 问你共有多少条不同的good way. 两条good way不同仅当它们所经过的边的集合中至少有一条不同 (很关 ...
- CodeForces - 789D Weird journey
D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 【cf789D】Weird journey(欧拉路、计数)
cf788B/789D. Weird journey 题意 n个点m条边无重边有自环无向图,问有多少种路径可以经过m-2条边两次,其它两条边1次.边集不同的路径就是不同的. 题解 将所有非自环的边变成 ...
- Weird journey CodeForces - 788B (路径计数)
大意:$n$结点$m$条边无向图, 满足 $(1)$经过$m-2$条边$2$次 $(2)$经过其余$2$条边$1$次 的路径为好路径, 求所有好路径数 相当于边加倍后再删除两条边, 求欧拉路条数 首先 ...
- Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图
题目链接:http://codeforces.com/problemset/problem/788/B B. Weird journey time limit per test 2 seconds m ...
- codeforces 407 div1 B题(Weird journey)
codeforces 407 div1 B题(Weird journey) 传送门 题意: 给出一张图,n个点m条路径,一条好的路径定义为只有2条路径经过1次,m-2条路径经过2次,图中存在自环.问满 ...
- [cf1038E][欧拉路]
http://codeforces.com/contest/1038/problem/E E. Maximum Matching time limit per test 2 seconds memor ...
随机推荐
- linux_http协议
什么是http协议? 超文本传输协议 最流行www服务,是http协议的实现 ssh,nfs,rsync客户端(Client)和服务端(Server),C/S架构,局域网内部用,胖客户端 http协议 ...
- jquery取前、后、父、子元素
前.prev(); 后.next(); 父.parent(); 子.children(); 注意:前的前是.prev().prev(),例如前元素无i,但前的前的i元素有i,不能写成.prev('i' ...
- 基于Elasticsearch搜索平台设计
背景 随着公司业务的高速发展以及数据爆炸式的增长,当前公司各产线都有关于搜索方面的需求,但是以前的搜索服务系统由于架构与业务上的设计,不能很好的满足各个业务线的期望,主要体现下面三个问题: 不能支持对 ...
- jQuery Validate验证框架与 jQuery ajaxSubmit的联合使用
jQuery Validate验证框架自定义验证 第一步导入导入js库 <script src="<%=basePath%>static/js/jquery.js" ...
- [DeeplearningAI笔记]02_3.1-3.2超参数搜索技巧与对数标尺
Hyperparameter search 超参数搜索 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.1 调试处理 需要调节的参数 级别一:\(\alpha\)学习率是最重要的需要调节的 ...
- 通过url获取相应的location信息
var properties = ['href', 'origin', 'host', 'hostname', 'port', 'pathname', 'search', 'hash']; var g ...
- iOS-键盘监听YYKeyboardManager
如果键盘弹出覆盖了原有的试图,这种效果并不好,所以我们就要在键盘弹出的时候,监听键盘的位置来改变我们一些试图的位置,例如tableView列表等:在这里推荐一个大牛ibireme写的YYKeyboar ...
- BZOJ 2287. [HZOI 2015]疯狂的机器人 [FFT 组合计数]
2287. [HZOI 2015]疯狂的机器人 题意:从原点出发,走n次,每次上下左右不动,只能在第一象限,最后回到原点方案数 这不煞笔提,组合数写出来发现卷积NTT,然后没考虑第一象限gg 其实就是 ...
- BZOJ 2822: [AHOI2012]树屋阶梯 [Catalan数 高精度]
2822: [AHOI2012]树屋阶梯 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 779 Solved: 453[Submit][Status] ...
- vue端口号被占用如何解决
vue端口占用问题 1. vue init webpack-simple#1.0 vue-webpack-simple-demo 2. cd vue-webpack-simple-demo 3. cn ...