现在的位置: 首页 > 综合 > 正文

hud3018 解题报告

2013年09月18日 ⁄ 综合 ⁄ 共 2075字 ⁄ 字号 评论关闭

Ant Trip

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


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 sample3 2,tony and his friends must form two group. 其实,先用并查集,分成几个部分,然后,把每个部分的奇点数相加除2就可以,也就是说,分的组数和奇数点的个数的一半相等,不过要注意只有一个点的集合,不能算,要被怱略掉,才行,这一点,我错了好几次,
 

#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int down[100005],setnum[100005],in[100005],re[100005],num[100005];
bool zh[100005];
int fin(int x)
{
      if(down[x]!=x)
      {
            down[x]=fin(down[x]);
      }
      return down[x];
}
int main()
{
      int n,m,k,a,b,i,s,e,j,temp;
      while(~scanf("%d%d",&n,&m))
      {
            for(i=0;i<=n;i++)
            {
                  zh[i]=true;
                  num[i]=0;
                  in[i]=0;
                  down[i]=i;

            }
            while(m--)
            {
                  scanf("%d%d",&s,&e);
                  a=fin(s);
                  b=fin(e);
                  if(a!=b)
                  {
                        down[a]=b;

                  }
                  in[s]++;
                  in[e]++;


            }

             bool zhe;
            int sum=0,ans;
            for(i=1,j=1;i<=n;i++)
            {
                  temp=fin(i);
                  zh[temp]=false;
                  if(in[i]%2)
                  {
                      num[temp]++;

                  }

            }

                 for(i=1;i<=n;i++)
                 {
                       if((zh[i]==false)&&(num[i]==0))
                       {
                             if(in[fin(i)])
                              sum++;

                       }
                       else if((zh[i]==true)&&(num[i]==0))
                       {
                          continue;
                       }
                       else
                       {
                             sum+=num[i]/2;
                       }

                 }
            printf("%d\n",sum);



      }

    return 0;
}


Source

抱歉!评论已关闭.