zoukankan      html  css  js  c++  java
  • hdu1878判断欧拉回路

    欧拉回路

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8186    Accepted Submission(s): 2926

    Problem Description
    欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?
     
    Input
    测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M;随后的M行对应M条边,每行给出一对正
    整数,分别是该条边直接连通的两个节点的编号(节点从1到N编号)。当N为0时输入结 束。
     
    Output
    每个测试用例的输出占一行,若欧拉回路存在则输出1,否则输出0。
     
    Sample Input
    3 3
    1 2
    1 3
    2 3
    3 2
    1 2
    2 3
    0
     
    Sample Output
    1
    0

     简单的欧拉回路判断,欧拉回路入门:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 #include <string.h>
     5 #include <math.h>
     6 #include <vector>
     7 #include <stack>
     8 using namespace std;
     9 #define ll long long int
    10 int a[2000];
    11 int find(int x)
    12 {
    13     if(a[x]!=x)
    14     a[x]=find(a[x]);
    15         return a[x];
    16 }
    17 int main()
    18 {
    19     int n,m;
    20     while(cin>>n,n)
    21     {
    22         cin>>m;
    23         int i,x,y;
    24         int b[n+1];
    25         a[0]=11;
    26         for(i=1;i<=n;i++)
    27         a[i]=i;
    28         memset(b,0,sizeof(b));
    29         for(i=0;i<m;i++)
    30         {
    31             scanf("%d%d",&x,&y);
    32             b[x]++;
    33             b[y]++;
    34             if(y<x)
    35             swap(x,y);
    36             int fx=find(x);
    37             int fy=find(y);
    38             if(fy!=fx)
    39             a[fy]=fx;
    40         }
    41         int sum=0;
    42         for(i=1;i<=n;i++)
    43         {
    44             if(b[i]%2==0&&a[i]==1)
    45             sum++;
    46         }
    47         if(sum==n)
    48         cout<<1<<endl;
    49         else cout<<0<<endl;
    50         }
    51 
    52 }
    View Code
  • 相关阅读:
    As3支持的Html标签一览
    SharpPcap 3.4使用范例
    十六章:构建自定义集合(Part 2)
    十七章:反射、特性和动态编程(Part 1)
    十六章:构建自定义集合(Part 1)
    Implementing Finalize and Dispose to Clean Up Unmanaged Resources
    HDU1711Number Sequence(KMP)
    HDU2087剪花布条(KMP)
    HDU1671Phone List (trie树)
    HDU 1075 What are you talking about(trie树)
  • 原文地址:https://www.cnblogs.com/ERKE/p/3257239.html
Copyright © 2011-2022 走看看