D. Weird journey
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

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)条边每条边走两次,另外两条边每条边走一次,问有多少种不同的路径。

这题咋一看,像不像欧拉路,很像啊,欧拉路是划出一条路径经过所有的边一次且仅一次,而这个是选出(m-2)条边来走两次。那我们不妨将这(m-2)条边再建一条,比如,

u<-->v,那我们再建一条u<-->v。那么如果修改后的图能跑出一条欧拉路的话,那么就符合题意了,所以我们就枚举这些边。由于(m-2)可能比较大,那么我们就枚举这个2,即不增建的边。

我们判定欧拉路的依据是奇数度数节点只有两个或者没有。那么我们枚举每一条边,将此边不增,其他的每条边都扩增一条,那么此时除了这条边连接的两个端点(u!=v)

以外,其他的所有节点的度数都是偶数,这两个节点的度数都是奇数。等会,我们还差一条边,,,现在已经有两个节点度数是奇数了,那么我们要拆的下一条边就必须是与这两个节点相关的(可以自己想想为什么)那么这条边的贡献就是这两个节点所连得边的和-1了。还有就是如果你拆了某个自环的边,也是可以的。还有种情况就是你一开始选的是一个自环的边,那么你下一条要拆的随便那条边都行。

#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#include <set>
#include <queue>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef pair<int,int>pii;
typedef long long LL;
const int N=1e6+;
const int mod=1e9+;
int n,m,s,k,t,cnt;
bool vis[N];
vector<int>edg[N];
int loop=;
LL ans=;
int in[N];
struct man{
int u,v;
}a[N];
void dfs(int u){
vis[u]=;
for(int i=;i<edg[u].size();i++){
int v=edg[u][i];
if(!vis[v])dfs(v);
}
}
int main()
{
int x,y,w,l,r;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
a[i].u=x;a[i].v=y;
if(x!=y){
edg[x].push_back(y);
edg[y].push_back(x);
}
else loop++;
in[x]++;in[y]++;
}
for(int i=;i<=n;i++)
if(in[i]!=){
dfs(i);
break;
}
for(int i=;i<=n;i++){
if(!vis[i]&&in[i]!=){
puts("");
return ;
}
}
for(int i=;i<=m;i++){
x=a[i].u;
y=a[i].v;
if(x!=y){
ans+=(edg[x].size()-+edg[y].size()-+loop);
}
else {
ans+=(m-);
}
}
printf("%lld\n",ans/);
return ;
}

最新文章

  1. #20145205 《Java程序设计》第3周学习总结
  2. 循序渐进Python3(八) -- 0 -- 初识socket
  3. ijg库的使用的几点注意
  4. svn: warning: &#39;xxxxxx&#39; is already under version control
  5. HTTP 笔记与总结(1 )Telnet 分别发送 HTTP GET 和 HTTP POST 请求
  6. POJ 1456 (贪心+并查集) Supermarket
  7. jQuery基础学习4——jQuery容错性
  8. [六]JFreeChart实践五之与Struts2整合
  9. ODI中删除数据的处理
  10. Socket通信中AF_INET 和 AF_UNIX域的区别
  11. java课设-计算数学表达式的程序,201521123050,肖世松,个人
  12. angular2 学习笔记 ( Dynamic Component 动态组件)
  13. MySQL复制相关技术的简单总结
  14. MySQL 并行复制演进及 MySQL 8.0 中基于 WriteSet 的优化
  15. linux 小笔记
  16. P2272 [ZJOI2007]最大半连通子图
  17. vue数组检测更新问题
  18. CSS布局之——对齐方式
  19. OpenStack 网络服务 Neutron 多网卡(提供者网络)(十八)
  20. mysql 查询某一主键在那些表中中被设置为外键了

热门文章

  1. Codeforces Round #411 (Div. 2) A-F
  2. 快速搭建 DNS 服务器: skydns + etcd
  3. gridveiw的使用
  4. javascript工厂模式、单例模式
  5. python 正则表达式口诀
  6. ShellCode的几种调用方法
  7. yocto 离线编译
  8. dev_cpu_dead
  9. 查看mysql的版本和端口号
  10. Leetcode 之Flatten Binary Tree to Linked List(50)