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 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 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条边一次的走法有多少种,路线倒过来一样的也只算一种。

思路:我们可以这样想,那m-2条边经过两次等价于有两条m-2条边都分别经过了一次,然后题目就转化成经过所有2m-2条边一次的走法有多少种,也就是欧拉路有几条。欧拉路的条件是要满足奇数度节点为0或2。然后我们来分析一下,若图是不联通的,答案肯定为0,若图是联通的,我们先假设有2m条边(也就是把每条边都增加一条),那么奇数度节点为0。因为我们要选择两条边只经过一次,也就是去掉两条边之后要符合欧拉路条件,因为题目说明有自环边,所以有以下4种情况,去掉相邻的两条普通边,奇数度节点为2,符合欧拉路条件;去掉不相邻的两条普通边,奇数度节点为4,不符合欧拉路条件;去掉一条自环边和一条普通边,奇数度节点为2,符合欧拉路条件;去掉两条自环边,奇数度节点为0,符合欧拉路条件。剩下的就是单纯的统计了,这题还有个坑就是所有的边里面不一定包含n个点,也就是他给你m条边,有可能点1没有在里面,但他还是联通的。下面给代码:

#include<iostream>  
#include<cmath>  
#include<queue>  
#include<cstdio>  
#include<queue>  
#include<algorithm>  
#include<cstring>  
#include<string>  
#include<utility>
#include<map>
#include<set>
#define maxn 1000005  
#define inf 0x3f3f3f3f  
using namespace std; 
typedef long long LL; 
const double eps=1e-8;
const LL mod=1e9+7;
int vis[maxn],deg[maxn],n,m,len,head[maxn],loop[maxn];
struct node{
    int v,next;    
}p[maxn<<1];
void addedge(int u,int v){
    p[len].v=v;
    p[len].next=head[u];
    head[u]=len++;
}
void dfs(int x){
    vis[x]=1;
    for(int i=head[x];~i;i=p[i].next){
	if(vis[p[i].v])
	    continue;
	dfs(p[i].v);	
    }    
}
int main(){
    memset(head,-1,sizeof(head));
    int num=0;
    LL sumloop=0;
    LL ans=0;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
	int u,v;
	scanf("%d%d",&u,&v);
	if(u==v){
	    sumloop++;
	    loop[u]++;
	    continue;    
	}
	addedge(u,v);
	addedge(v,u);
	deg[u]++;
	deg[v]++;
    }
    for(int i=1;i<=n;i++){
	if((deg[i]||loop[i])&&!vis[i]){
	    dfs(i);
	    num++;    
	}	
    }
    if(num>1){
	printf("0\n");
	return 0;	
    }
    for(int i=1;i<=n;i++)
	ans+=(LL)deg[i]*(deg[i]-1)>>1;
    ans+=sumloop*(sumloop-1)>>1;
    ans+=sumloop*(m-sumloop);
    printf("%lld\n",ans);	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值