CodeForces 788B Weird journey

 Weird journey
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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 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.

Sample Input

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 条路,然后求 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;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值