1. 程式人生 > 其它 >hdu----(3118)Arbiter(構造二分圖)

hdu----(3118)Arbiter(構造二分圖)

Arbiter

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 800    Accepted Submission(s): 410

Problem Description

Arbiter is a kind of starship in the StarCraft science-fiction series. The Arbiter-class starship is a Protoss warship specializing in providing psychic support. Arbiters were crewed exclusively by Judicators; unlike other warships that were manned predominantly by Templar. The Judicator used the Arbiter as a base to provide support using space-time manipulation. Arbiters could weaken space-time, tearing rifts in the fabric of space-time, creating a vortex linking another location to the Arbiter’s location. This could be used to move personnel over long distances between stars. In the meantime of widely used Arbiter to transfer, KMXS, the captain of one Arbiter, was warning that some person had got a serious mental disorder after the trip on his Arbiter. By using mice as model animals, he found the sake, it’s because of chirality! Every person has chirality, either left-handed or right-handed. Actually all the persons must live with the food which has the same chirality. When one person took Arbiter from one star to another one, his chirality will be changed (from left-handed to right-handed or from right-handed to left-handed). If a person took a long trip and finally got back to his own star, however, his chirality might be changed to the opposite state other than his original, which would cause fatal mental disorder, or even death. KMXS has the channels map among the starts and he need to prohibit minimum number of channels from traveling so that wherever a person starts his traveling from when he gets his original star he’ll be safe. KMXS turns to your help.

Input

The first line of input consists of an integer T, indicating the number of test cases. The first line of each case consists of two integers N and M, indicating the number of stars and the number of channels. Each of the next M lines indicates one channel (u, v) which means there is a bidirectional channel between star u and star v (u is not equal to v).

Output

Output one integer on a single line for each case, indicating the minimum number of channels KMXS must prohibit to avoid mental disorder. Constraints 0 < T <= 10 0 <= N <= 15 0 <= M <= 300 0 <= u, v < N and there may be more than one channel between two stars.

Sample Input

1 3 3 0 1 1 2 2 0

Sample Output

1

Source

2009 Asia Wuhan Regional Contest Online

這道題的意思:

Arbiter

     “仲裁者”是《星際爭霸》科幻系列中的一種太空船。仲裁者級太空船是神族的戰船,專門提供精神力支援。不像其他戰船的人員主要是戰士階級,仲裁者所承載的都是統治階級。統治者以仲裁者為基地,用時空操控來提供支援。

       仲裁者可以克服時空限制,撕裂時空,產生裂縫,製造渦洞,把另一個空間連線到仲裁者所在空間,可以用來運送人員,跨越長途星際。

       正當仲裁者廣泛用於運輸的時候,一艘仲裁者的船長 KMXS 發出警告,有些人員在他的船上完成旅程之後,得到嚴重的精神錯亂。他用小白鼠做動物實驗,找到了原因,是生物化學中的“手性”!

       每個人 都有手性,是左手性或者右手性。事實上,所有人的生存都必須依靠相同手性的食物。一個人乘坐仲裁者從一個星球到另一個星球的時候,他的手性會改變,(從左 手性變成右手性,或者從右手性變成左手性。)如果一個人經過長途旅行,最後回到自己的星球,可是他的手性卻可能改變成跟原來的相反,那就會造成致命的精神 錯亂,甚至死亡。 

       KMXS 擁有星球之間的航道圖。他需要禁止通過航道的最少數目,以致無論一個人從哪裡出發,當他回到自己的星球,會是安全的。KMXS請求你的幫助。

Input

  第一行輸入有一個整數 T,表示測試用例的數目。

  每個用例的第一行有兩個整數 N 和 M,表示星球的數目和航道的數目。隨後的 M 行表示航道,每個 (u, v) 代表在星球 u 和星球 v 之間有一條雙向的航道(u 不等於 v)。

Output

        每個測試用例的結果輸出一行,用一個整數表示KMXS為了要避免人們精神錯亂, 必須禁止的最少航道數目。

Constraints

    0 < T <= 10

    0 <= N <= 15 0 <= M <= 300

    0 <= u, v < N在兩個星球之間可以有超過一條航道

Sample Input

    13 30 11 22 0

Sample Output

    1

很明顯: 這個意思是,可以從任意一個星球,到大另一個星球,但是每個星球都有手性.....

要我們求出,對於任何一個星球出發都不能通過奇數次傳遞迴到該星球...也就是不能形成奇數環..

而我們知道,二分圖的充要條件是: 至少存在兩個頂點,每一個點的迴路的長度都是偶數...

所以只需進行擦邊,來構成二分圖就可以了.,...

程式碼:

 1 #include<cstring>
 2 #include<cstdio>
 3 const int maxn=305;
 4 int aa[maxn],bb[maxn];
 5 int n,m;
 6 int main(){
 7   int test;
 8   //freopen("test.in","r",stdin);
 9   scanf("%d",&test);
10   while(test--){
11    scanf("%d%d",&n,&m);
12     for(int i=0;i<m;i++){
13       scanf("%d%d",aa+i,bb+i);
14     }
15     int ans=m+2;
16    for(int i=0;i<(1<<n);i++)   //2^n種情況,對於每一個星球都可以兩種情況要麼為左,要麼為右
17    {
18         int cnt=0;
19         for(int j=0;j<m;j++){
20             if(((i>>aa[j])%2)==((i>>bb[j])%2))  //屬於同一個世界,說明呵呵存在奇數環
21                cnt++;
22      }
23        if(ans>cnt)ans=cnt;
24    }
25      printf("%dn",ans);
26   }
27  return 0;
28 }