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

    Ant Trip

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

    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   |   We have carefully selected several similar problems for you:  3013 3015 3016 3011 3010 
    题意:给出N个节点,M个边,问要遍历一遍所有的边,需要的最小group数目。
    思路:首先通过并查集判断有几个联通块。然后对于每个联通块,统计需要多少笔。
    如果,联通块中,度为奇数节点的个数为0或2个,那就可以一笔画完。
    反之,因为一笔最多消去两个度为奇数的节点,所以要最少用奇度数节点个数/2笔。
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int n,m,tot,ans;
    int vis[100010];
    int fa[100010],num[100010];
    int into[100010],du[100010];
    int find(int x){
        if(fa[x]==x)    return fa[x];
        else return fa[x]=find(fa[x]);
    }
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF){
            tot=0;ans=0;
            memset(du,0,sizeof(du));
            memset(vis,0,sizeof(vis));
            memset(num,0,sizeof(num));
            memset(into,0,sizeof(vis));
            for(int i=1;i<=n;i++)    fa[i]=i;
            for(int i=1;i<=m;i++){
                int x,y;
                scanf("%d%d",&x,&y);
                into[x]++;into[y]++;
                int dx=find(x);int dy=find(y);
                if(dx!=dy)    fa[dy]=dx;
            }
            for(int i=1;i<=n;i++){
                int now=find(i);
                if(!vis[now]){
                    vis[now]=1;
                    num[++tot]=now;
                }
                if(into[i]%2!=0)    du[now]++;
            }
            for(int i=1;i<=tot;i++){
                if(into[num[i]]==0)    continue;
                if(du[num[i]]==0)    ans++;
                ans+=du[num[i]]/2;
            }
            cout<<ans<<endl;
        }
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    sqlserver sql优化案例及思路
    mysql执行计划常用说明
    MYSQL 的rownum
    innodb crash
    spring-mybatis源码追踪
    mylyn提交到JIRA的日期格式错误
    [google面试CTCI] 2-1.移除链表中重复元素
    [google面试CTCI] 2-0.链表的创建
    [google面试CTCI] 1-8.判断子字符串
    [google面试CTCI] 1-7.将矩阵中特定行、列置0
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/8092850.html
Copyright © 2011-2022 走看看