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"\n", (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 }
posted @ 2017-08-26 21:24  阿波罗2003  阅读(182)  评论(0编辑  收藏  举报