Summer exams 4: Star Trek (Euler Road)

 

 

topic:

analysis:

Subject to the effect: From any point, ending any point in the choice of two sides through only once, the other two have been through all situations edges.

Is not considered self-Ring: This question looks like Euler road, but the road is Euler each edge only once through, then we consider: doubling the number of sides, select two edges deleted, so that the rest of the Euler is a way.

After doubling the number of sides, each point is an even number of degrees

Euler path determination: only odd points is two points, the other points are the even (odd refers to degrees) demonstrated

Find two different sides of the deletion, further consideration from the ring, we should discuss classification:

1. Delete two sides: by drawing the launch, delete the two sides must be connected to the same point, because the only way two odd points appear.

2. Delete an edge and a loopback: Deletion from a ring, the degree of that point is still an even number, to delete just one side, two odd dot will produce, since it is clear that each additional ring and can be so the side match.

3. Delete the two self-ring: After deleted, all remaining points still is even point, it is an Euler tour, also satisfies the condition (two rings away from one, the other walked twice).

But there may occur without communicating, so it should be with disjoint-set special sentence a bit, does not satisfy the output 0.

Note: like this data: a communication block + one lone point, is satisfied. So disjoint-set point can not do, and should be doing, in other words, where the communication refers to the side of connectivity! ! !

So how to check and set by doing it?

Or the first input point, and then point together. When the judgment, there are only point judgment even if one side of the disjoint-set, not to a point no edge of the tube.

Complexity: O (N)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100005
int fa[N],a[N],b[N];
ll ans=0,self=0,du[N];
int find(int x)
{
    if(fa[x]==x) return x;
    return fa[x]=find(fa[x]);
}
int main()
{
    freopen("tour.in","r",stdin);
    freopen("tour.out","w",stdout);
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++){
        scanf("%d%d",&a[i],&b[i]);
        if(a[i]==b[i]) self++;
        else{
            int f1=find(a[i]),f2=find(b[i]);
            fa[f1]=f2;
            du[a[i]]++; du[b[i]]++;
        }
    }
    int f=find(a[1]);
    for(int i=2;i<=m;i++) if(find(a[i])!=f) { printf("0\n"); return 0; }//对边做并查集 
    for(int i=1;i<=n;i++)
    if(du[i]>1) ans+=du[i]*(du[i]-1)/2;//C(n,2)累加答案 
    if(self&&self!=1) ANS + * = Self (self-1) / 2 ; // Laid to avoid run-time error determination at 
    ANS = Self * + (M- Self); 
    the printf ( " % LLD \ n- " , ANS);
     return  0 ; 
} 
/ * 
. 5. 4 
. 1 2 
. 1. 3 
. 1. 4 
. 1 . 5 

10 10 
2. 4 
. 4. 4 
. 4. 6 
. 6. 6 
. 6. 7 
. 7. 5 
. 5. 8 
. 8. 4 
. 4. 9 
. 9 10 
* /

 

Guess you like

Origin www.cnblogs.com/mowanying/p/11408980.html