Codeforces-788B Weird journey(计数/欧拉回路)

B. 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次,2条边只走1次,一共有多少种走法(当且仅当只走一次的边的集合不相同时,走法才不相同)
题解:首先只考虑所有路都走2遍的情况。在这种情况下,等效于把所有的边复制一次,那么只要整个图是一个联通块,这个图肯定是个欧拉回路,
因为所有的点的度都是偶数。
那么接下来就要考虑“删除”2条边,仍然能构成欧拉路的情况
①删去的2条边都是自环:删去后所有的点的度仍然是偶数,成立。
②删去的2条边中一条是自环:删去后只有2个点的度是奇数,成立。
③删去的2条边没有自环,且没有公共点:删去后有4个点的度是奇数,不成立。
④删去的2条边没有自环,且有公共点:删去后只有2个点的度是奇数,成立。
考虑完以上情况只有,只要枚举每条边,对于非自环的边,对答案的贡献是与他有公共点的边的数量+自环数;对于自环,对答案的贡献是m-1。最后将ans/2去掉重复情况即可。


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MX = 1e6 + 5;
int u[MX],v[MX],mark[MX],IN[MX],p[MX],sz[MX];
int find(int x){return p[x]==x?x:(p[x]=find(p[x]));}
int main(){
    //freopen("in.txt","r",stdin);
    int n,m,cnt=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) p[i]=i,sz[i]=1;
    for(int i=0;i<m;i++){
        scanf("%d%d",&u[i],&v[i]);
        if(u[i]==v[i]) {
            cnt++;
            mark[u[i]]=1;
            continue;
        }
        int p1=find(u[i]),p2=find(v[i]);
        if(p1!=p2){
            p[p1]=p2;
            sz[p2]+=sz[p1];
        }
        IN[u[i]]++;IN[v[i]]++;
    }
    bool flag=0;
    int nn=n;
    for(int i=1;i<=n;i++) if(sz[p[i]]==1&&mark[i]==0) nn--;
    for(int i=1;i<=n;i++) if(sz[p[i]]==nn) flag=1;
    if(flag){
        LL ans=0;
        for(int i=0;i<m;i++){
            if(u[i]==v[i]){
                ans+=(LL)(m-1);
            }
            else{
                ans+=(IN[u[i]]-1)+(IN[v[i]]-1)+cnt;
            }
        }
        printf("%I64d\n",ans/2);
    }
    else printf("0\n");
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值