zoukankan      html  css  js  c++  java
  • HDU 3018 Ant Trip

    Ant Trip

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

    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
    Hint
    New ~~~ Notice: if there are no road connecting one town ,tony may forget about the town. In sample 1,tony and his friends just form one group,they can start at either town 1,2,or 3. In sample 2,tony and his friends must form two group.
     
    Source
     
    Recommend
    gaojie
     
     
    思路:一笔画问题,给你几个集合,要你判断使用几笔可以把所有的边遍历一次
    欧拉路径的变形,先使用并查集表示每一个集合,然后分别判断,这里有两种笔
    画,第一种是从一点出发回到同一点,第二种是从一点出发,不会到这一点,然后
    根据欧拉路径的条件既可以解出此题
     
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    using namespace std;
    int the_last_sum1;
    int the_last_sum2;
    int map[100010];
    int sum[100010];
    int n,m;
    int first,end;
    int indegree[100010];
    int find(int x)
    {
        while(map[x] != x)
        {
            x = map[x];
        }
        return x;
    }
    void Merge(int x,int y)
    {
        int p = find(x);
        int q = find(y);
        if(p < q)
           map[q] = p;
        else
           map[p] = q;
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            memset(indegree,0,sizeof(indegree));
            memset(sum,0,sizeof(sum));
            for(int i = 1;i <= n;i ++)
            {
                map[i] = i;
            }
            while(m --)
            {
                scanf("%d%d",&first,&end);
                if(first == end)
                   continue ;
                indegree[first] ++;
                indegree[end] ++;
                Merge(first,end);
            }
            for(int i = 1;i <= n;i ++)
            {
                if(indegree[i] == 0)
                   map[i] = -1;
            }
            for(int i = 1;i <= n;i ++)
            {
                if(indegree[i] % 2 == 1)
                    sum[find(i)] ++;
            }
            the_last_sum1 = 0;
            the_last_sum2 = 0;
            for(int i = 1;i <= n;i ++)
            {
                if(map[i] == i && sum[i] == 0)
                    the_last_sum1 ++;
                if(indegree[i] % 2 == 1 && indegree[i] != 0)
                    the_last_sum2 ++;
            }
            //printf("%d %d
    ",the_last_sum1,the_last_sum2);
            printf("%d
    ",(the_last_sum1 + the_last_sum2 / 2));
        }
        return 0;
    }
  • 相关阅读:
    leetcode 105. 从前序与中序遍历序列构造二叉树
    leetcode 96. 不同的二叉搜索树
    leetcode 21. 合并两个有序链表
    leetcode 617. 合并二叉树
    leetcode 101. 对称二叉树
    欧拉定理和扩展欧拉定理。
    [NOI2018] 屠龙勇士
    [SDOI2010]古代猪文
    [SDOI2013]方程
    扩展Lucas定理
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3368096.html
Copyright © 2011-2022 走看看