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): 434    Accepted Submission(s): 179


      
    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

    分析:首先,你用笔来画的话,只可能有2种,一:每回路,a——>b 二:形成回路,a——>...——>a

    对于图中的每一块,度数数为奇数的点必须是由第一种画出来的,所以奇数/2就是画的笔数

    由两种结合而成的图,也只是奇数/2

    特别的,如果图只有第二种的话,即该块中不存在奇数点,则只要画一笔

    对于整副图(每一块块组合而成),等于 :第一块奇数点/2+第二块奇数点/2+.......,最后得,图的总奇数点/2

    接着还要计算有多少块里不存在奇数点(不存在奇数点的那块中,一定没有第一种画法,只需要画一笔),累加起来就得到答案了。。。

    #include<stdio.h>
    #include<string.h>
    #define maxn 100005
    int father[maxn],degree[maxn],rank[maxn],count[maxn];
    int n,m;
    int find(int x)
    {//并查集查找根结点并进行状态压缩
    	if(x != father[x])
    		father[x] = find(father[x]);
    	return father[x];
    }
    void merge(int x,int y)
    {//合并两个集合
    	x = find(x);
    	y = find(y);
    	if(x != y)
    	{
    		if(rank[x] > rank[y])
    		{
    			father[y] = x;
    		}
    		else
    			if(rank[x] < rank[y])
    			{
    				father[x] = y;
    			}
    			else
    			{
    				father[x] = y;
    				rank[y] ++;
    			}
    	}
    }
    void init()
    {
    	int i,u,v;
    	for(i = 1; i <= n; i++)
    		father[i] = i;
    	memset(rank,0,sizeof(rank));
    	memset(degree,0,sizeof(degree));
    	memset(count,0,sizeof(count));
    	while( m-- )
    	{
    		scanf("%d%d",&u,&v);
    		merge(u,v);
    		degree[u]++;
    		degree[v]++;
    	}
    }
    int main()
    {
    	int sum1,i;
    	while(~scanf("%d%d",&n,&m))
    	{
    		init();
    		sum = 0;//sum记录笔画的数量
    		for(i = 1; i <= n; i++)
    		{//统计每个图的奇度顶点个数
    			if(degree[i]&1)
    				count[find(i)]++,sum++;
    		}
    		//含奇度顶点的图需要奇度顶点个数/2笔才能画完
    		sum/=2;
    		for(i = 1; i <= n; i++)
    		{
    			if(find(i) == i)
    			{
    				if(count[i] == 0 && degree[i] != 0)//欧拉回路并且不为一个单独的点
    					sum ++;
    			}
    		}
    		//输出总的笔画数
    		printf("%d\n",sum1);
    	}
    	return 0;
    }




  • 相关阅读:
    增长思维——模式&&组织
    BackUP
    增长思维——机会
    Android
    增长思维——作战地图
    Server架构 小知识
    Server
    产品思维——创新模式
    产品思维——用户体验
    博客迁移到~http://zhulingyu.com
  • 原文地址:https://www.cnblogs.com/LUO257316/p/3220823.html
Copyright © 2011-2022 走看看