【CodeForces - 789D】Weird journey(思维,图的性质,tricks,有坑)

题干:

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.

题目大意:

小男孩Igor想成为一名旅行者。起初,他决定访问他祖国 — Uzhlyandia的所有城市。

众所周知,Uzhlyandia有 n 座城市,用 m 条无向边连接起来。 此外,没有两条道路连接同一对城市,但道路开始和结束在同一个城市可以存在。Igor想事先计划好他的旅行。男孩认为好的路径经过 m - 2 条边恰好两次,经过 2 条边恰好一次, 这条路径的起点和终点可以在 n 个城市中任选。

现在他想知道有多少条不同的路径满足要求。注意,经过点顺序不同,每条边经过次数相同的路径为同一条路径。

Input

第一行包含两个整数 nm (1 ≤ n, m ≤ 106) — Uzhlyandia的城市和道路的数量。

之后 m 行包含两个整数 u 和 v (1 ≤ u, v ≤ n) 表示城市 u 和 v之间有一条路径。

保证没有重边,但可能有自环。

解题报告:

贴一个题解:m-2条边走两次,2条边走一次,那么我们可以把每一条边都复制一遍,然后再删掉两条边,求欧拉通路的方案数。

另一种思考方式:假设走一次的这两条边称为关建边,那么需要发现你选的这两条关建边,一定连在一个顶点上。所以枚举每个点C(n,2)统计一下,然后对于自环单独容斥处理一下即可。

当然,对于不连通的图,直接输出0就可以了。但是这里有坑,就是他虽然不连通,但是如果独立出去的分量都是独立的点的话,是没有关系的,因为他们并没有边相连。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e6 + 5;
vector<int> vv[MAX];
int n,m,f[MAX],vis[MAX],cnt;
void dfs(int x,int fa) {
	vis[x] = 1;
	for(auto v : vv[x]) {
		if(vis[v] || v == fa) continue;
		dfs(v,x);
	}
}
int main()
{
	cin>>n>>m;
	for(int u,v,i = 1; i<=m; i++) {
		scanf("%d%d",&u,&v);
		if(u == v) vv[u].pb(v);
		else vv[u].pb(v),vv[v].pb(u);
	}
	for(int i = 1; i<=n; i++) sort(vv[i].begin(),vv[i].end()),f[i]=i;
	ll ans = 0; 
	for(int i = 1; i<=n; i++) {
		if(vv[i].size() > 0) {dfs(i,0);break;}
	}
	for(int i = 1; i<=n; i++) {
		if(vis[i] == 0 && vv[i].size() > 0) return 0,puts("0");
	}
	for(int i = 1; i<=n; i++) {
		int ok = binary_search(vv[i].begin(),vv[i].end(),i);
		cnt += ok;
		ll tmp = vv[i].size() - ok;
		ans += tmp*(tmp-1)/2;
	}
	printf("%lld\n",ans +1LL*cnt*(m-1) - 1LL*cnt*(cnt-1)/2);
	return 0 ;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值