Codeforces Round #407 (Div. 2) D Weird journey [dfs]

                                                                                           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 withm 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 integersu and v (1 ≤ u, v ≤ n) that mean that there is road between citiesu 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

题意:给你n个点m条边的无向图(会有自环的情况),求出从任意一个点出发长为2m-2的路径,满足其中有m-2条边经过2次,2条边只经过1次的路径的总数(定义当两条之经过一次的边不完全一样时,两条路径不同)。
题解:当存在边不相通时,答案为0。
答案不为0时,可以经过推断可以得知两种情况
①当两条只经过一次的边都不是自环的边时,这两条边必须相邻。
②当两条只经过一次的边,其中一条为自环的边时另一条可以为其他任意边。


AC代码:
#include<stdio.h>
#include<vector>
using namespace std;
typedef long long ll;
ll size[1000005];
ll tru[1000005];
vector<ll>vt[1000005];
ll mark[1000005];
ll step;
void dfs(ll u)
{
	for(ll i=0;i<vt[u].size();i++)
	{
		ll y=vt[u][i];
		if(mark[y]==0)
		{
			mark[y]=1;
			dfs(y);
		}
	}
}
int main()
{
	ll n,m;
	scanf("%lld%lld",&n,&m);
	ll sum=0;
	for(ll i=0;i<m;i++)
	{
		ll u,v;
		scanf("%lld%lld",&u,&v);
		tru[v]++;
		tru[u]++;
		if(u!=v)
		{
			size[v]++;
			size[u]++;
			vt[u].push_back(v);
			vt[v].push_back(u);
		}
		else sum++;
	}
	step=0;
	for(ll i=1;i<=n;i++)
	{
		if(mark[i]==0)
		{
			if(tru[i]!=0)step++;
			mark[i]=1;
			dfs(i);
		}
	} 
	if(step!=1)
	{
		printf("0\n");
		return 0; 
	}
	ll ans=0;
	for(ll i=1;i<=n;i++)
	{
		ll k=size[i]*(size[i]-1);
		ans+=k/2;
	}
	ans+=(m-sum)*sum;
	ans+=sum*(sum-1)/2;
	printf("%lld\n",ans);
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值