1. 程式人生 > >Ant Trip(區別於二分匹配中最小路徑覆蓋的一筆畫問題)

Ant Trip(區別於二分匹配中最小路徑覆蓋的一筆畫問題)

end src 並且 col group 就是 size http align

題目鏈接:

http://acm.hdu.edu.cn/showproblem.php?pid=3018

題目:

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 2009 Multi-University Training Contest 12 - Host by FZU
 1 /*
 2 問題 給出n個頂點和m條邊,n<=100000,m<=200000,若要遍歷所有的邊且每條邊只能走一次,問至少需要幾個起點
 3 保證沒有重復道路,保證道路兩端的城市是不同的,存在孤立的點,例如 3個點,1條邊,1和2聯通,那麽3就是孤立的點
 4 
 5 解題思路 簡單總結就是一筆畫問題,畫的時候,要麽a--->b(一條線),即a和b均為奇點(度數為奇數的點),要麽a--->a(一個圈),即該圈中沒有奇點
 6 由於連通圖中不可能存在奇數個奇點,換句話說連通圖中奇點要麽不出現,即存在歐拉回路,只需要一筆,要麽成對出現,每一對需要一筆
 7 那麽總的筆畫數等於 所有奇點的個數除以2(孤立的點度數為0,為偶數) 加上 歐拉回路數
 8  
 9 輸入時計算每個頂點的度數,使用並查集將圖分成一塊一塊,遍歷每一個頂點,找出奇點計數並且標記其根節點;再遍歷頂點,
10 如果不是孤立的點 且 是根 且 該根沒有被標記過,即為歐拉回路。  
11 */
12 #include<stdio.h>
13 #include<string.h>
14 int deg[200010],fat[100010],book[100010];
15 void merge(int a, int b);
16 int getf(int x);
17 
18 int main()
19 {
20     int n,m;
21     int i,a,b;
22     while(scanf("%d%d",&n,&m) != EOF)
23     {
24         memset(deg,0,sizeof(deg));
25         for(i=1;i<=n;i++){
26             fat[i]=i;
27         }
28         for(i=1;i<=m;i++){
29             scanf("%d%d",&a,&b);
30             deg[a]++;
31             deg[b]++;
32             merge(a,b);
33         }
34         
35         memset(book,0,sizeof(book)); 
36         int singsum=0;
37         for(i=1;i<=n;i++){
38             if(deg[i] & 1){
39                 book[ getf(i) ]=1;//標記以該點為根的一塊圖中存在奇點 
40                 singsum++;
41             }
42         }
43         
44         int eulenum=0;
45         for(i=1;i<=n;i++){
46             if(deg[i] > 0 && i==fat[i] && 0 == book[i])//不是孤立的點 且 是根 且 該根沒有被標記過,即是歐拉回路 
47                 eulenum++;    
48         }
49         printf("%d\n",singsum/2+eulenum);
50     }
51     return 0;
52 }
53 void merge(int a, int b)
54 {
55     int t1,t2;
56     t1=getf(a);
57     t2=getf(b);
58     if(t1 != t2){
59         fat[t2]=t1;
60     }    
61 } 
62 int getf(int x)
63 {
64     return fat[x]==x ? x : fat[x]=getf(fat[x]);
65 }

Ant Trip(區別於二分匹配中最小路徑覆蓋的一筆畫問題)