[Euler Road] Codeforces Round #407 (Div. 1) 788B. Weird journey__ Graph theory-

Source: Internet
Author: User
the

A non-direction graph, no heavy edge, and a self loop are given.
Ask how many sides of the two-tuple <e1,e2> meet a path in the diagram, where E1,e2 e1,e2 are just passing through one time, the other m−2 m-2 bar just after two times.
n,m≤100000 n,m \le 100000

Give each edge a side, and then the problem is converted to: Cancel the two-piece patch, so that the number of odd points is 0 0 or 2 2 of the number of scenarios.
Obviously, before the edge is removed, the degrees of all points are even, and when one edge is deleted, two odd points appear. When you delete an edge, there will be a public endpoint on the side, or 4 4 odd points.
Then just enumerate the first side and XJB XJB statistics.
Pay attention to dealing with the self loop, and whether the diagram has no solution.

#include <cstdio> #include <cmath> #include <algorithm> using namespace std;
typedef long Long LL;
const int maxn=1000005;
int N,M,_M,CNT,D[MAXN],FA[MAXN];
BOOL VIS[MAXN]; struct edge{int x,y;}
ES[MAXN];
LL ans; int GETFA (int x) {return FA[X]==X?X:FA[X]=GETFA (fa[x]);} void merge (int x,int y) {X=GETFA (x); Y=getfa (y); if (x!=y) fa[x]= Y
    int main () {//freopen ("cf788b.in", "R", stdin);
    Freopen ("Cf788b.out", "w", stdout);
    scanf ("%d%d", &n,&m);
    for (int i=1;i<=n;i++) fa[i]=i;
        for (int i=1;i<=m;i++) {int x,y; scanf ("%d%d", &x,&y);
            if (x!=y) {es[++_m].x=x, es[_m].y=y; d[x]++; d[y]++;
            Vis[x]=vis[y]=true;
        Merge (X,y);
    else cnt++, vis[x]=true; int rt=0; 
    for (int i=1;i<=n;i++) if (vis[i]) rt=i;
    if (!RT) return printf ("0\n"), 0;
    for (int i=1;i<=n;i++) if (VIS[I]&AMP;&AMP;GETFA (RT)!=GETFA (i)) return printf ("0\n"), 0; for (int i=1;i<=_m;i++) ans+= (d[es[i].x]-1) + (d[es[i].y]-1);
    ans=ans/2+ (LL) _m*cnt+ (LL) cnt* (cnt-1)/2;
    printf ("%i64d\n", ans);
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.