hdu acm 3018 Ant Trip

来源:互联网 发布:ubuntu磁盘重新分区 编辑:程序博客网 时间:2024/06/05 14:09

Ant Trip

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


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 31 22 31 34 21 23 4
 


 

Sample Output
12

 

 

package LanQiaoBei;import java.util.HashSet;import java.util.Scanner;import java.util.Set;public class EularCircuit_3018 {private static int vertexCount;private static int edgeCount;private static int[] rank;private static int[] parent;private static int[] degree;public static void main(String[] args) {Scanner sc = new Scanner(System.in);int v1, v2;while(sc.hasNextInt()) {vertexCount = sc.nextInt();edgeCount = sc.nextInt();rank = new int[vertexCount+1];parent = new int[vertexCount+1];degree = new int[vertexCount+1];makeSet();for(int i=0; i<edgeCount; i++) {v1 = sc.nextInt();v2 = sc.nextInt();degree[v1]++;degree[v2]++;union(v1, v2);}int count = count();//奇数点System.out.println(count);}}private static int count() {Set<Integer> graphRoot = new HashSet<Integer>();//所有连通子图for(int i=1; i<=vertexCount; i++) {graphRoot.add(find(i));}int oddcount = 0;//总的奇数点个数for(int i=1; i<=vertexCount; i++) {if(degree[i]==0) {graphRoot.remove(find(i));continue;}if((degree[i]&1)==1) {//奇数点oddcount++;graphRoot.remove(find(i));}}return (oddcount>>1) + graphRoot.size();}private static void union(int x1, int x2) {int r1 = find(x1);int r2 = find(x2);if(r1 != r2) {if(rank[r1] > rank[r2]) {parent[r2] = r1;} else {parent[r1] = r2;if(rank[r1] == rank[r2]) {rank[r2] ++;}}}}private static int find(int x1) {if(parent[x1] != x1) {parent[x1] = find(parent[x1]);}return parent[x1];}private static void makeSet() {for(int i=1; i<=vertexCount; i++) {parent[i] = i;rank[i] = 0;}}}
原创粉丝点击