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.

Input

The first line contains two integers nm (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.

Output

Print out the only integer — the number of good paths in Uzhlyandia.

Examples
input
5 4
1 2
1 3
1 4
1 5
output
6
input
5 3
1 2
2 3
4 5
output
0
input
2 2
1 1
1 2
output
1
Note

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次。两条路径被看做不同的,当且仅当它们经过的边的集合不同。

  原问题可以转换为将每条边复制一下,然后再删去两条边使得新图存在欧拉路的方案数。

  欧拉路存在的两个条件是

  1)只存在一个连通块包含的边数大于0

  2)度数为奇数的点少于2个。

  暂时先不考虑自环的情况,然后可以得到一个结论就是:这两条边的必须存在公共顶点。

  然后可以得到一个做法就是枚举每个点,计算和它相连的边中,任选两条的方案数。

  现在考虑自环,删掉一个自环使得这个顶点的度数仍然为偶数,所以选取的一条边包含自环,那么另一条边可以任意选。

  为了更好地计数,暂时不把自环算入度数,最后统一计算。然后会出现选择的两条边都是自环被计算2次的情况,所以减一减就好了。

Code

 /**
* Codeforces
* Problem#789D
* Accepted
* Time: 421ms
* Memory: 37400k
*/
#include <bits/stdc++.h>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean; int n, m;
int *dag;
int* f;
int scc = ;
boolean *haveedge; int find(int x) {
return (f[x] == x) ? (x) : (f[x] = find(f[x]));
} inline void init() {
scanf("%d%d", &n, &m);
dag = new int[(n + )];
f = new int[(n + )];
haveedge = new boolean[(n + )];
memset(dag, , sizeof(int) * (n + ));
for(int i = ; i <= n; i++)
f[i] = i, haveedge[i] = false;
for(int i = , u, v; i <= m; i++) {
scanf("%d%d", &u, &v);
dag[u] += u != v, dag[v] += u != v;
haveedge[u] = haveedge[v] = true;
scc += u == v;
f[find(u)] = find(v);
}
} long long res = ;
int cnt = ;
inline void solve() {
for(int i = ; i <= n; i++) {
res += (dag[i] * 1LL * (dag[i] - )) >> ;
cnt += f[i] == i && haveedge[i];
}
printf(Auto"\n", (cnt == ) ? (res + (scc * 1LL * (m - )) - ((scc * 1LL * (scc - )) >> )) : ());
} int main() {
init();
solve();
return ;
}

Codeforces 789D Weird journey - 欧拉路 - 图论的更多相关文章

  1. CodeForces - 788B Weird journey 欧拉路

    题意:给定n个点,m条边,问能否找到多少条符合条件的路径.需要满足的条件:1.经过m-2条边两次,剩下两条边1次  2.任何两条路的终点和起点不能相同. 欧拉路的条件:存在两个或者0个奇度顶点. 思路 ...

  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 - 789D Weird journey

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

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

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

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

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

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

  7. codeforces 407 div1 B题(Weird journey)

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

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

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

  9. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

随机推荐

  1. jquery评分效果Rating精华版

    参考:https://blog.csdn.net/bluceyoung/article/details/8573629

  2. MACD 下0轴后,强力=7上0轴的实例:

    MACD 下0轴后,强力=7上0轴的实例: 虽然再上0轴,但是由于是强力上,必须有缓解期.缓解期后MACD开始收口,虽然没有下0轴,但是开始趋向死叉. 由于并没有很强的做多市场环境,MACD只是略高于 ...

  3. 《大话设计模式》c++实现 装饰者模式

    一.UML图   介绍 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创 ...

  4. CentOS上svn checkout时报错SSL handshake failed: SSL error: Key usage violation in certificate has been det

    局域网安装了个SVN在checkout的时候报错 SSL handshake failed: SSL error: Key usage violation in certificate has bee ...

  5. VS2013打包程序步骤

    VS自带的打包程序默认是没有安装的,如果有打包的需要,需要自己去下载一个安装程序  1.右击解决方案,选择添加项目,在打开的对话框中找到[已安装]-[模板]-[其他项目]-[安装和部署],如图示.第一 ...

  6. 3.用Thead子类及Runnable接口类实现车站购票的一个场景(static关键字)

    如上图所示,我们这里模拟一下去车站买票的情形:这里有3个柜台同时售票,总共是1000张票,这三个柜台同时买票,但是只能一个柜台卖同一张票,也就是说1号票卖了之后我们就只能买2号票,2号票卖了之后我们只 ...

  7. Python学习记录之(五)-----类进阶篇

    静态方法 类方法 属性方法 通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调 ...

  8. restful的特点

    1. 资源(Resources) REST的名称”表现层状态转化”中,省略了主语.”表现层”其实指的是”资源”(Resources)的”表现层”.                所谓”资源”,就是网络 ...

  9. python复习冒泡排序

    冒泡排序: 思路: 先找到最大值放到最右边: #encoding=utf-8 a=[1,9,2,8,3,6,4] print "a before change:",a for i ...

  10. sublime text3 快捷键和好用的插件

    常用快捷键: Ctrl + D 选中一个单词 Ctrl + L 选中一行 Ctrl + A 全选 Ctrl + M 选中括号内所有内容 (编写CSS或JS时非常实用) Ctrl + G 快速定位到某一 ...