CF788B Weird journey 欧拉路径+计数

欧拉路径:如果一个图存在一笔画,那么度数为奇数的点的个数不大于2. 

code: 

#include 
#define N 1000005  
#define ll long long 
#define setIO(s) freopen(s".in","r",stdin) 
using namespace std;      
int p[N],deg[N],from[N],to[N],vis[N],n,m;  
int find(int x) 
{
    return p[x]==x?x:p[x]=find(p[x]); 
}
int main() 
{   
    ll ans=0; 
    int i,j,sum=0,tp=0; 
    // setIO("input");   
    scanf("%d%d",&n,&m); 
    for(i=1;i<=n;++i) p[i]=i; 
    for(i=1;i<=m;++i) 
    {
        int u,v; 
        scanf("%d%d",&u,&v); 
        from[i]=u, to[i]=v;
        vis[u]=vis[v]=1;                  
        if(u==v) 
        {           
            ++sum; 
        } 
        else 
        { 
            ++deg[u], ++deg[v];    
            u=find(u), v=find(v); 
            if(u!=v) p[u]=v, tp=v; 
        }
    }    
    for(i=1;i<=n;++i) 
        if(vis[i]&&find(i)!=tp) 
        { 
            printf("0\n"); 
            return 0;     
        }   
    for(i=1;i<=m;++i) 
    {
        if(from[i]==to[i]) ans+=1ll*(m-1); 
        else 
        {   
            ans+=(ll)deg[from[i]]+deg[to[i]]-2+sum;   
        }
    }
    printf("%lld\n",ans/2); 
    return 0; 
}

  

你可能感兴趣的:(CF788B Weird journey 欧拉路径+计数)