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 ...
随机推荐
- 重拾Python(2):如何安装第三方库(Windows)
使用python进行数据分析或者数据处理时,往往需要使用一些库,而使用库之前必须安装它.Anaconda内置了很多常用的第三方库,可以满足绝大部分需求,比如numpy.pandas.matplotli ...
- MySQL相关文档索引
MySQL的新功能5.7 https://dev.mysql.com/doc/refman/5.7/en/mysql-nutshell.html MySQL5.7安装 http://note.youd ...
- @Controller注解
Spring从2.5版本后开始引入注解,用户可以使用@Controller,@RequestMapping,@RequestParam,@ModelAttribute等类似这样的注解. @Contro ...
- 关于Kafka 的 consumer 消费者处理的一些见解
前言 在上一篇 Kafka使用Java实现数据的生产和消费demo 中介绍如何简单的使用kafka进行数据传输.本篇则重点介绍kafka中的 consumer 消费者的讲解. 应用场景 在上一篇kaf ...
- HTML核心标签之表格标签(一)
表格的基本语法: <body> <table> <tr><td></td><td></td></tr> ...
- (转)Elasticsearch 5 Ik+pinyin分词配置详解
今天以这篇文章结束同城旅游网的面试,正好面试官也问到站内检索,可以尝试一下这篇文章介绍的方法.Elasticsearch 5 Ik+pinyin分词配置详解
- HBase Filter及对应Shell--转
http://www.cnblogs.com/skyl/p/4807793.html 比较运算符 CompareFilter.CompareOp比较运算符用于定义比较关系,可以有以下几类值供选择: E ...
- BZOJ 1845: [Cqoi2005] 三角形面积并 [计算几何 扫描线]
1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 1151 Solved: 313[Submit][Stat ...
- 2018/1/27 Zookeeper实现分布式锁
public class DistributedClient { // 超时时间 private static final int SESSION_TIMEOUT = 5000; // zookeep ...
- linux常用命令(不断更新)
睡眠命令(第一步可省去): 1.查看你的系统支持什么模式:cat /sys/power/state(我的系统为:freeze mem disk) 2.切换到管理员模式下,执行命令:echo " ...