zoukankan      html  css  js  c++  java
  • [HDOJ1878]欧拉回路

    欧拉回路

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


    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 <cstring>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 int n,m;
     8 int d[1001];
     9 int pre[1001];
    10 
    11 int find(int x)
    12 {
    13     if(pre[x] == 0)
    14         return x;
    15     int ans = find(pre[x]);
    16     pre[x]=ans;
    17     return ans;
    18 }
    19 
    20 void unite(int a, int b)
    21 {
    22     int x = find(a);
    23     int y = find(b);
    24     if(x != y)
    25     {
    26         pre[x] = y;
    27     }
    28 }
    29 
    30 int main() 
    31 {
    32     int tmp1, tmp2, count = 0;
    33     bool success = true;
    34     while(scanf("%d", &n) && n != 0)
    35     {
    36         memset(d, 0, sizeof(d));
    37         memset(pre, 0, sizeof(pre));
    38         count = 0;
    39         scanf("%d", &m);
    40         success = true;
    41         for(int i = 1; i <= m; i++)
    42         {
    43             scanf("%d %d", &tmp1, &tmp2);
    44             unite(tmp1, tmp2);
    45             d[tmp1]++;
    46             d[tmp2]++;
    47         }
    48         for(int i = 1; i <= n; i++)
    49         {
    50             if(d[i] % 2 != 0)
    51             {
    52                 cout << 0 << endl;
    53                 success = false;
    54                 break;
    55             }
    56         }
    57         if(!success)
    58         {
    59             continue;
    60         }
    61         for(int i = 1; i <= n; i++)
    62         {
    63             if(pre[i] == 0)
    64             {
    65                 count++;
    66             }
    67         }
    68         if(count > 1)
    69         {
    70             cout << 0 << endl;
    71         }
    72         else
    73         {
    74             cout << 1 << endl;
    75         }
    76     }
    77     return 0;
    78 }
    View Code
  • 相关阅读:
    form标签
    roadmap
    自我介绍
    3 week work—Grid Layout
    3 week work—Position
    2nd week
    objects
    个人简介
    7th week :DOM BOM
    颜色表示法
  • 原文地址:https://www.cnblogs.com/kirai/p/4558353.html
Copyright © 2011-2022 走看看