Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图

题目链接:http://codeforces.com/problemset/problem/788/B


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.




题解:

1.必要条件:有边的点构成的图必须是连通的。

2.连通图的性质:从随便一个点出发,当遍历完所有点又回到原点后,每一条边刚好都被经过2次。

3.当不考虑自环时:1次边必须有一个公共点(可以理解为起始边、结束边,但题目并不要求次序)。

   当考虑自环时:1次边还可以是:自环边+自环边   or   自环边+除自环边之外的其他边(此种情况只能自环边作为结束边)。




代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int maxn = 1e6+7;

int n,m,vis[maxn], d[maxn],used[maxn];
vector<int>g[maxn];

void dfs(int u)
{
    vis[u] = 1;
    for(int i = 0; i<g[u].size(); i++)
        if(!vis[g[u][i]])
           dfs(g[u][i]);
}

int main()
{
    int cnt = 0;
    int rt; //用于记录有边的点
    scanf("%d%d",&n,&m);
    int x = 0;
    for(int i = 1; i<=m; i++)
    {
        int u, v;
        scanf("%d%d",&u,&v);
        used[u] = used[v] = 1;
        if(u==v)
        {
            cnt++;
            continue;
        }
        g[u].push_back(v);
        g[v].push_back(u);
        rt = u;
    }

    dfs(rt);
    int B = 1;
    for(int i = 1; i<=n; i++)   //判断是否连通
        if(used[i] && !vis[i])
            B = 0;

    LL ans = 0;
    for(int i = 1; i<=n; i++)   //先不考虑自环的情况
        if(vis[i])
            ans += 1LL*g[i].size()*(g[i].size()-1)/2;


    ans += 1LL*cnt*(cnt-1)/2 + 1LL*cnt*(m-cnt); //加上自环的情况
    cout<< 1LL*B*ans <<endl;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值