Codeforces 789 D. Weird Journey (Euler Road count)

Source: Internet
Author: User



Test instructions: give you a n point m edge of the graph, ask you to pass through which m-2 side two times, the remaining 2 side of the scheme number of one time there are several, if the remaining two side of the set as the same

The problem: Convert the test instructions into, we select two sides, the other edges are changed to two, so it becomes a stroke question (Euler path exists), the necessary and sufficient condition of the existence of our Euler path is the number of singularities is 0 or 2,. Then we need to discuss the classification, we divide the side into normal and self-loop (u=v) two categories.

1. Select two non-adjacent ordinary edges, there are 4 singularities in the figure, do not meet the conditions of the Euro-La path

2. Select two adjacent ordinary edges, there are 2 singularities in the figure, meet the conditions of the Euler path

3. Select an ordinary edge of a self-loop, there are 2 singularities in the figure, to meet the conditions of the Euler path

4. Select two self-ring, there are 0 singularities in the figure, to meet the conditions of the Euler path

Before this, if the set of M-bar overlays is not connected, the answer is 0.

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 have n cities connected with M bidirectional roads. Also, there is no and roads in the country that connect the same pair of cities, but roads starting and ending in the SAM E 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 is in Uzhlyandia. Paths is 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-integers n, m (1≤n, m≤106)-the number of cities and roads in Uzhlyandia, respectively.

Each of the next m lines contains a 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 would be given in the input twice. That also means, every city there are no more than one road this connects the city to itself. Output

Print out the-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 is good paths that is same with displayed above, because the sets of roads they pass over once is 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 of 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.

#include <bits/stdc++.h> using namespace std;
typedef long Long LL;
const int N = 1e6 + 100;
typedef long Long LL;
vector<int>vec[n];
int a[n],fa[n],vis[n];
int deg[n];
    int find (int x) {int r=x;
    while (FA[R]!=R) r=fa[r];
    int i=x,j;
        while (i!=r) {j=fa[i];
        Fa[i]=r;
    I=j;
} return R;
    } int main () {Ios::sync_with_stdio (false);
    int n,m,i,j,u,v;
    int fx,fy;
    ll Loop,ans,edge;
    cin>>n>>m;
    loop=ans=edge=0;
    for (i=1;i<=n;i++) fa[i]=i;
        while (m--) {cin>>u>>v;
        Vis[u]=vis[v]=1;
            if (u==v) {loop++;
        Continue
        } edge++;
        Vec[u].push_back (v);
        Vec[v].push_back (U);
        deg[u]++;
        deg[v]++;
        Fx=find (U);
        Fy=find (v);
    if (fx!=fy) fa[fx]=fy;
    } for (j=1;j<=n;j++) if (vis[j]) break;
    for (i=1;i<=n;i++) if (Vis[i] && find (i)!=find (j)) break; if (i<=n) {printf ("0\n");
        } else {for (i=1;i<=n;i++) {ans+= (LL) deg[i]* (deg[i]-1)/2;
        } ans+= (ll) Loop*edge;
        ans+= (LL) loop* (loop-1)/2;
    cout<<ans<<endl;
} return 0;
 }





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.