Description
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.
Sample Input
5 4 1 2 1 3 1 4 1 5
6
5 3 1 2 2 3 4 5
0
2 2 1 1 1 2
1
题目大意:
给出n个点和m 条路,然后求 m - 2 条边经过两次,2 条边经过一次的路有多少。
思路:
对于每条路 我们都建两条,那么每个点的度数一定为偶数,然后删除两条边后仍然能够构成欧拉回路,枚举删除哪两条边。
对于自环 删除之后,当前点度数为偶数,随便删除一条边,两个点度数为奇数。
低于非自环,删除临边,两个点度数为偶数。其余都不行。
所以累加起来就是答案。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int MAX = 1000005;
int n,m,k;
int parent[2*MAX+10];
int aa[MAX];
int u[MAX];
int v[MAX];
int vis[MAX];
int getfa(int a)
{
if( parent[a]!= a)
parent[a] = getfa(parent[a]);
return parent[a];
}
void addd(int a,int b)
{
int p1 = getfa(a);
int p2 = getfa(b);
if( p1 == p2 )
return;
parent[p2] = p1;
}
int main()
{
int num=0;
scanf("%d%d",&n,&m);
for(int i = 0;i <=n; ++i)
{
parent[i] = i;
}
for(int i = 0;i < m; ++i)
{
scanf("%d%d",&u[i],&v[i]);
vis[u[i]]=1;
vis[v[i]]=1;
if(u[i]==v[i])
{
num++;
continue;
}
addd(u[i],v[i]);
aa[u[i]]++;
aa[v[i]]++;
}
int fff=-1;
int ans=1;
for(int i=1;i<=n;i++)
{
if(vis[i])
{
if(fff==-1)
{
fff=getfa(i);
}
else
{
if(getfa(i)!=fff)
{
ans=0;
break;
}
}
}
}
long long aas=0;
if(ans)
{
for(int i=0;i<m;i++)
{
if(u[i]==v[i])
{
aas+=m-1;
}
else
{
aas+=aa[u[i]]-1+aa[v[i]]-1+num;
}
}
aas=aas/2;
cout<<aas<<endl;
}
else
{
puts("0");
}
return 0;
}