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] 欧拉回路的起 ...
随机推荐
- unix shell 解析 1
---- shell 1 testdb3:/home/oracle [pprod] >more /home/oracle/utility/macro/tns_log_back_12c.sh #! ...
- jsp的简介
https://www.w3cschool.cn/jsp/jsp-intro.html
- skeljs框架关键点使用
global 全局 style.css containers: 1400px;容器宽度 xlarge 超大屏(media: max-width:1680px) style-xlarge.cs ...
- EditText自动弹出软键盘
editText.requestFocus() editText.isFocusable = true editText.isFocusableInTouchMode = true val timer ...
- git ---匿名分支和checkout命令
git checkout -b //创建一个匿名分支
- linux下php开启pdo扩展
前提:网页报错 为解决问题:Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' 解决方案 : 下载安装PHP_MYSQL扩展 wget http:// ...
- leetcode_1011. Capacity To Ship Packages Within D Days_binary search二分
https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/ 传送带每天有最大传送量V,对于n个货物[w1,w2,w3. ...
- 扩展 IHttpModule
上篇提到请求进入到System.Web后,创建完HttpApplication对象后会执行一堆的管道事件,然后可以通过HttpModule来对其进行扩展,那么这篇文章就来介绍下如何定义我们自己的mod ...
- Solaris 默认Shell 修改
ssh登陆远程的solaris 10,backspace出现乱码. ssh登陆远程的solaris 10默认Shell不是bash 把solaris10的shell环境改为bash就行 dev13% ...
- 第3节 hive高级用法:15、hive的数据存储格式介绍
hive当中的数据存储格式: 行式存储:textFile sequenceFile 都是行式存储 列式存储:orc parquet 可以使我们的数据压缩的更小,压缩的更快 数据查询的时候尽量不要用se ...