CodeForces788B 欧拉路
2 seconds
256 megabytes
standard input
standard output
Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherland — Uzhlyandia.
It is widely known that Uzhlyandia has n cities connected with m bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and ending in the same city can exist. Igor wants to plan his journey beforehand. Boy thinks a path is good if the path goes over m - 2 roads twice, and over the other 2 exactly once. The good path can start and finish in any city of Uzhlyandia.
Now he wants to know how many different good paths are in Uzhlyandia. Two paths are considered different if the sets of roads the paths goes over exactly once differ. Help Igor — calculate the number of good paths.
The first line contains two integers n, m (1 ≤ n, m ≤ 106) — the number of cities and roads in Uzhlyandia, respectively.
Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n) that mean that there is road between cities u and v.
It is guaranteed that no road will be given in the input twice. That also means that for every city there is no more than one road that connects the city to itself.
Print out the only integer — the number of good paths in Uzhlyandia.
5 4
1 2
1 3
1 4
1 5
6
5 3
1 2
2 3
4 5
0
2 2
1 1
1 2
1
In first sample test case the good paths are:
- 2 → 1 → 3 → 1 → 4 → 1 → 5,
- 2 → 1 → 3 → 1 → 5 → 1 → 4,
- 2 → 1 → 4 → 1 → 5 → 1 → 3,
- 3 → 1 → 2 → 1 → 4 → 1 → 5,
- 3 → 1 → 2 → 1 → 5 → 1 → 4,
- 4 → 1 → 2 → 1 → 3 → 1 → 5.
There are good paths that are same with displayed above, because the sets of roads they pass over once are same:
- 2 → 1 → 4 → 1 → 3 → 1 → 5,
- 2 → 1 → 5 → 1 → 3 → 1 → 4,
- 2 → 1 → 5 → 1 → 4 → 1 → 3,
- 3 → 1 → 4 → 1 → 2 → 1 → 5,
- 3 → 1 → 5 → 1 → 2 → 1 → 4,
- 4 → 1 → 3 → 1 → 2 → 1 → 5,
- and all the paths in the other direction.
Thus, the answer is 6.
In the second test case, Igor simply can not walk by all the roads.
In the third case, Igor walks once over every road.
题意:
有n个点,m条边,问如果选择m-2条边走2次,2条边只走1次,一共有多少种走法(当且仅当只走一次的边的集合不相同时,走法才不相同)
题解:
可以这样想,其它边走两次,就是说,一次去,一次回来。
如果图不连通,那么绝对是0,每条边经过两次,遍历整个图,那是局对可行的,
因为这样就是边重复走一次,求欧拉回路,那是绝对可以的,去在回来不久行了,只要连通随便走。
这样两条边只经过一次,如果有自环,自环多余两个随便选两个都可以,
选一个自环其它随便选一条边也可以,
不然,就只能两条边同一端点才行,统计答案。
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstdio>
#define ll long long
#define N 1000007
using namespace std; int n,m,t;
ll du[N];
int zh[N];
bool vis[N];
int cnt,head[N],next[N*],rea[N*]; void add(int u,int v)
{
next[++cnt]=head[u];
head[u]=cnt;
rea[cnt]=v;
}
void dfs(int u)
{
vis[u]=true;
for (int i=head[u];i!=-;i=next[i])
{
int v=rea[i];
if (!vis[v]) dfs(v);
}
}
int main()
{
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
for (int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
add(x,y),add(y,x);
if (x==y)
{
t++;
zh[x]++;
continue;
}
du[x]++,du[y]++;
}
for (int i=;i<=n;i++)
if (du[i])
{
dfs(i);
break;
}
for (int i=;i<=n;i++)
if (!vis[i])
{
if (du[i]||zh[i])
{
printf("0\n");
return ;
}
}
ll ans=;
ans+=(ll)t*(t-)/;//自环随便选。
ans+=(ll)t*(m-t);//其它点随便选、
for (int i=;i<=n;i++)
if (du[i]>=) ans+=(ll)du[i]*(du[i]-)/;//表示一条边进,一条边出。
printf("%lld",ans);
}
CodeForces788B 欧拉路的更多相关文章
- 洛谷P1341 无序字母对[无向图欧拉路]
题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 输入格式: 第一行输入一 ...
- POJ1386Play on Words[有向图欧拉路]
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11846 Accepted: 4050 De ...
- hdu1161 欧拉路
欧拉路径是指能从一个点出发能够“一笔画”完整张图的路径:(每条边只经过一次而不是点) 在无向图中:如果每个点的度都为偶数 那么这个图是欧拉回路:如果最多有2个奇数点,那么出发点和到达点必定为该2点,那 ...
- UVA10054The Necklace (打印欧拉路)
题目链接 题意:一种由彩色珠子组成的项链.每个珠子的两半由不同的颜色组成.相邻的两个珠子在接触的地方颜色相同.现在有一些零碎的珠子,需要确定他们是否可以复原成完整的项链 分析:之前也没往欧拉路上面想, ...
- 洛谷 P1341 无序字母对 Label:欧拉路 一笔画
题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 输入格式: 第一行输入一 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- hihocoder 1181 欧拉路.二
传送门:欧拉路·二 #1181 : 欧拉路·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回中小Hi和小Ho控制着主角收集了分散在各个木桥上的道具,这些道具其 ...
- hiho48 : 欧拉路·一
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最后的宝藏.现在他们控制的 ...
- hdu5883 The Best Path(欧拉路)
题目链接:hdu5883 The Best Path 比赛第一遍做的时候没有考虑回路要枚举起点的情况导致WA了一发orz 节点 i 的贡献为((du[i] / 2) % 2)* a[i] 欧拉回路的起 ...
随机推荐
- Python基础第一天
诞生时间:1991年,创造者Guido van Rossum 优点: 1.简单 Python是一种代表简单注意思想的语言 2.易学 Python是及其容易上手,因为Python有极其简单的说明文档 ...
- IE8下ajax兼容问题
function crossDomainAjax(url, successCallback) { // IE8 & 9 only Cross domain JSON GET request i ...
- Windows远程桌面连接复制文件失败或非常慢
环境搭建过程中经常会遇到需要将文件从本机传到云服务器或者企业内部服务器上的场景,此时如果文件过大的话要传个半天或者直接告诉你复制失败,让人好生无奈 ~ ~. windows环境下,可以将本地磁盘映 ...
- [BZOJ4815][CQOI2017]小Q的表格 数论+分块
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4815 题目中所给条件中的$(a,a+b)$和$(a,b)$的关系很瞩目. 然后大家都知道$ ...
- 块级元素的text-align对行内元素和果冻元素(inline-block)的作用
块级元素社设置了text-align:center以后,对其直接行内元素/果冻元素.继承行内元素/果冻元素都会产生“居中效应”. <style> .test4{ text-align: c ...
- 拼图游戏源码-swift版项目源码
作者fanyinan,源码PuzzleProject,公司的项目中需要一个拼图游戏,之前有手动拼图和随机打乱的功能,近期又由于个(xian)人(zhe)爱(dan)好(teng)自己加入了自动拼图功能 ...
- PowerShell让系统可以执行.ps1文件,开机,关机,在线时间 , Function自定义函数
Function Get-ComputerUptimeHistory { $q=' <QueryList> <Query Id="0" Path="Sy ...
- glm 矩阵乘法得反过来写
- [eclipse]的快捷键的设置
今天新解压了一个eclipse,发现alt+/提示的快捷键不好用了,开始比较犯懒,但是发现开发效率低下,总结几个eclipse下快捷方式的解决办法. 第一个解决没有一点提示的情况. 1.首先通过菜单打 ...
- ES6特性的两点分析
块级作用域声明let.constES6中const 和let的功能,转换为ES5之后,我们会发现实质就是在块级作用改变一下变量名,使之与外层不同.ES6转换前: let a1 = 1; let a2 ...