• 【HDOJ3018】【一笔画问题】【欧拉回路+并查集】


    http://acm.hdu.edu.cn/showproblem.php?pid=3018

    Ant Trip

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

    Problem Description
    Ant Country consist of N towns.There are M roads connecting the towns.

    Ant Tony,together with his friends,wants to go through every part of the country. 

    They intend to visit every road , and every road must be visited for exact one time.However,it may be a mission impossible for only one group of people.So they are trying to divide all the people into several groups,and each may start at different town.Now tony wants to know what is the least groups of ants that needs to form to achieve their goal.
     
    Input
    Input contains multiple cases.Test cases are separated by several blank lines. Each test case starts with two integer N(1<=N<=100000),M(0<=M<=200000),indicating that there are N towns and M roads in Ant Country.Followed by M lines,each line contains two integers a,b,(1<=a,b<=N) indicating that there is a road connecting town a and town b.No two roads will be the same,and there is no road connecting the same town.
     
    Output
    For each test case ,output the least groups that needs to form to achieve their goal.
     
    Sample Input
    3 3 1 2 2 3 1 3 4 2 1 2 3 4
     
    Sample Output
    1 2
    题目大意:给一个图【不保证连通】,求最少需要几画才能在不重复使用边的情况下走完全部边。
    题目分析:一笔画模板题。
    需要画数=每个连通图的画数之和。    每个连通图的画数=如果各个点的度全部是大于0的偶数则根据欧拉回路为1,如果存在奇数,则为奇数个数/2。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<queue>
     5 #include<vector>
     6 using namespace std;
     7 int in[100005];
     8 int pre[100005];
     9 int tot1[100005],tot2[100005];
    10 int n,m;
    11 int find(int x)
    12 {
    13     int xx=x;
    14     while(x!=pre[x])
    15     {
    16         x=pre[x];
    17     }
    18     while(pre[xx]!=x)
    19     {
    20         int t=pre[xx];
    21         pre[xx]=x;
    22         xx=t;
    23     }
    24     return x;
    25 }
    26 int main()
    27 {
    28     while(scanf("%d%d",&n,&m)==2)
    29     {
    30         memset(tot1,0,sizeof(tot1));
    31         memset(tot2,0,sizeof(tot2));
    32         memset(in,0,sizeof(in));
    33         for(int i = 1 ; i <= n ; i++)
    34         {
    35             pre[i]=i;
    36         }
    37         memset(in,0,sizeof(in));
    38         while(m--)
    39         {
    40             int a,b;
    41             scanf("%d%d",&a,&b);
    42             in[a]++;
    43             in[b]++;
    44             if(find(a)!=find(b))
    45             {
    46                 pre[find(a)]=find(b);
    47             }
    48         }
    49         for(int i = 1 ; i <= n ; i++)
    50         {
    51             int q=find(i);
    52             if(in[i]%2==0&&in[i])
    53             tot1[q]++;
    54             else if(in[i]%2)
    55             tot2[q]++;
    56         }
    57         int sum=0;
    58         for(int i = 1 ; i <= n ;i++)
    59         {
    60             if(tot2[i])
    61             {
    62                 sum+=tot2[i]/2;
    63             }
    64             else if(tot1[i])
    65             {
    66                 sum++;
    67             }
    68          } 
    69          cout << sum << endl;
    70         
    71     }
    72     return 0;
    73 }
     
  • 相关阅读:
    使用homebrew安装mysql
    MacOS下命令行安装神器brew
    mac Navicat 破解+汉化(亲测可用)
    eos开发(三)使用cleos命令行客户端操作EOS——关于钱包wallet和账户account
    eos开发(二)使用cleos命令行客户端操作EOS(钱包wallet基础操作)
    php职业规划
    temp
    事业随想
    系统时间错误
    加解密,token
  • 原文地址:https://www.cnblogs.com/MekakuCityActor/p/9034680.html
Copyright © 2020-2023  润新知