zoukankan      html  css  js  c++  java
  • Codeforces 789D Weird journey

    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次。两条路径被看做不同的,当且仅当它们经过的边的集合不同。

      原问题可以转换为将每条边复制一下,然后再删去两条边使得新图存在欧拉路的方案数。

      欧拉路存在的两个条件是

      1)只存在一个连通块包含的边数大于0

      2)度数为奇数的点少于2个。

      暂时先不考虑自环的情况,然后可以得到一个结论就是:这两条边的必须存在公共顶点。

      然后可以得到一个做法就是枚举每个点,计算和它相连的边中,任选两条的方案数。

      现在考虑自环,删掉一个自环使得这个顶点的度数仍然为偶数,所以选取的一条边包含自环,那么另一条边可以任意选。

      为了更好地计数,暂时不把自环算入度数,最后统一计算。然后会出现选择的两条边都是自环被计算2次的情况,所以减一减就好了。

    Code

     1 /**
     2  * Codeforces
     3  * Problem#789D
     4  * Accepted
     5  * Time: 421ms
     6  * Memory: 37400k
     7  */ 
     8 #include <bits/stdc++.h>
     9 #ifndef WIN32
    10 #define Auto "%lld"
    11 #else
    12 #define Auto "%I64d"
    13 #endif
    14 using namespace std;
    15 typedef bool boolean;
    16 
    17 int n, m;
    18 int *dag;
    19 int* f;
    20 int scc = 0;
    21 boolean *haveedge;
    22 
    23 int find(int x) {
    24     return (f[x] == x) ? (x) : (f[x] = find(f[x]));
    25 }
    26 
    27 inline void init() {
    28     scanf("%d%d", &n, &m);
    29     dag = new int[(n + 1)];
    30     f = new int[(n + 1)];
    31     haveedge = new boolean[(n + 1)];
    32     memset(dag, 0, sizeof(int) * (n + 1));
    33     for(int i = 1; i <= n; i++)
    34         f[i] = i, haveedge[i] = false;
    35     for(int i = 1, u, v; i <= m; i++) {
    36         scanf("%d%d", &u, &v);
    37         dag[u] += u != v, dag[v] += u != v;
    38         haveedge[u] = haveedge[v] = true;
    39         scc += u == v;
    40         f[find(u)] = find(v);
    41     }
    42 }
    43 
    44 long long res = 0;
    45 int cnt = 0;
    46 inline void solve() {
    47     for(int i = 1; i <= n; i++) {
    48         res += (dag[i] * 1LL * (dag[i] - 1)) >> 1;
    49         cnt += f[i] == i && haveedge[i];
    50     }
    51     printf(Auto"
    ", (cnt == 1) ? (res + (scc * 1LL * (m - 1)) - ((scc * 1LL * (scc - 1)) >> 1)) : (0));
    52 }
    53 
    54 int main() {
    55     init();
    56     solve();
    57     return 0;
    58 }
  • 相关阅读:
    斯皮尔曼+假设检验
    53. Maximum Subarray
    工具网站
    Win10 不稳定经常死机 -蓝屏-电脑-电脑 系统启动后 键盘等一会才有反应
    MFC 代码无错却无法运行
    为什么喝酒不能吃头孢
    基于靶机的SQL注入的探测
    SQlMap注入的基本原理
    burp suite 简单靶机验证码绕过【前台|后台验证】自娱自乐系列
    burp suite 基于自娱自乐的靶机简单密码爆破
  • 原文地址:https://www.cnblogs.com/yyf0309/p/7436415.html
Copyright © 2011-2022 走看看