zoukankan      html  css  js  c++  java
  • 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;
    }
    


  • 相关阅读:
    并行 并发 概念
    为什么C++编译器不能支持对模板的分离式编译_刘未鹏
    并发编程的15 条建议
    VS2005 C++ str.Format 编译错误
    PPT 去除排练计时
    VS2005中 depends.exe 在哪里
    《A wavelet tour of signal processing》前言摘录
    Windows Forms 2.0 Programming 读书笔记Hello, Windows Forms
    VS2005 宽字符 unicode字符集和多字节字符集
    Windows Forms 2.0 Programming 读书笔记Hello, Windows Forms(2)
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538657.html
Copyright © 2011-2022 走看看