题意:给定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 欧拉路的更多相关文章

  1. Codeforces 789D Weird journey - 欧拉路 - 图论

    Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his mot ...

  2. 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 ...

  3. CodeForces 788B - Weird journey [ 分类讨论 ] [ 欧拉通路 ]

    题意: 给出无向图. good way : 仅有两条边只经过一次,余下边全经过两次的路 问你共有多少条不同的good way. 两条good way不同仅当它们所经过的边的集合中至少有一条不同 (很关 ...

  4. CodeForces - 789D Weird journey

    D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. 【cf789D】Weird journey(欧拉路、计数)

    cf788B/789D. Weird journey 题意 n个点m条边无重边有自环无向图,问有多少种路径可以经过m-2条边两次,其它两条边1次.边集不同的路径就是不同的. 题解 将所有非自环的边变成 ...

  6. Weird journey CodeForces - 788B (路径计数)

    大意:$n$结点$m$条边无向图, 满足 $(1)$经过$m-2$条边$2$次 $(2)$经过其余$2$条边$1$次 的路径为好路径, 求所有好路径数 相当于边加倍后再删除两条边, 求欧拉路条数 首先 ...

  7. 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 ...

  8. codeforces 407 div1 B题(Weird journey)

    codeforces 407 div1 B题(Weird journey) 传送门 题意: 给出一张图,n个点m条路径,一条好的路径定义为只有2条路径经过1次,m-2条路径经过2次,图中存在自环.问满 ...

  9. [cf1038E][欧拉路]

    http://codeforces.com/contest/1038/problem/E E. Maximum Matching time limit per test 2 seconds memor ...

随机推荐

  1. sping中防止定时任务多处运行解决方法

    @Servicepublic class TimerJobService implements LzhTimerJobDao{ Logger logger = LoggerFactory.getLog ...

  2. Shader 入门笔记(三) ShaderLab 初识

    Unity中,Unity Shader 都是ShaderLab 来编写的.ShaderLab 是Unity提供的编写Unity Shader 的一种说明性语言. 1)Properties :定义了着色 ...

  3. 【转】sed 的参数

    一.15个参数 1.r 从文件读入 [root@watchout2 ~]# cat file12345 [root@watchout2 ~]# cat newfile abcde [root@watc ...

  4. 【转】AWK常用

    awk是个优秀文本处理工具,可以说是一门程序设计语言.下面是awk内置变量. 一.内置变量表 属性 说明 $0 当前记录(作为单个变量) $1~$n 当前记录的第n个字段,字段间由FS分隔 FS 输入 ...

  5. Gitlab权限管理-issue管理[六]

    标签(linux): git 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 设置好密码后登录进入管理目录 创建组 设置组名和权限 创建用户 已有四个用户了 给p ...

  6. PAT basic level 1001-1019 解题笔记

    1002 写出这个数 采用字符串输入数据,再对每位减去字符‘0’,得到该位相应的整数 int len=s.length();//字符串的长度 ; ;i<len;i++)//每位减去‘0’,逐位相 ...

  7. 使用clipboard.js实现复制内容至剪贴板

    下载插件 clipboard.js是不依赖flash,实现复制内容至剪贴板的js插件.下载clipboard.js的压缩包,根据需要选择dist目录下的压缩或未压缩版. github地址:https: ...

  8. 玩转webpack(二):webpack的核心对象

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者介绍:陈柏信,腾讯前端开发,目前主要负责手Q游戏中心业务开发,以及项目相关的技术升级.架构优化等工作. 前言 webpack 是一个强大的模 ...

  9. 浅谈计算机中的IO模型

    IO模型一共有5种: blocking IO #阻塞IO nonblocking IO #非阻塞IO IO myltiplexing #IO多路复用 signal driven IO #信号驱动IO ...

  10. android adb shell input各种妙用

    项目中使用一个开发版,预留两个usb接口.类似华硕TinkerBoard. 一个用户连接摄像头,一个用于adb调试.结果就没了鼠标的接口.多次切换鼠标和摄像头插头,非常不方便,带摄像头的app没法调试 ...