hdu3118Arbiter (利用二分圖的定義,枚舉每種狀態),hdu3118arbiter枚舉
ArbiterTime Limit: 1000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 773 Accepted Submission(s): 401
Problem DescriptionArbiter 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.
InputThe 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).
OutputOutput 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
13 30 11 22 0
Sample Output
1題意:一個人從通道一個星球到另一個星球他的左手變右手,右手變左手,如果存在回到原來的星球他的左右手不是原來的狀態的話,這是致命的,為了使這種情況不能發生,最少需要冊除多少條邊。 也就是可以理解成:刪去最少的邊,使圖中不存在奇圈。二分圖的定義:在無向圖G中,至少有兩個點且如果存在迴路,那麼迴路必須為偶迴路。這樣才是二分圖。所以根據定義可知:把n個點分成兩部分,每一部分內的點集任意兩點不相連。把相連的邊就冊除。這樣的分法有2^n種。 最後找出冊除邊數最少的就是所要求的。#include<stdio.h>#include<queue>#include<string.h>#include<iostream>using namespace std;int map[20][20],n;int deletEdg(int k){ int ans=0; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) { if((k&(1<<i))==0&&(k&(1<<j))==0)ans+=map[i][j];//在同一部分的點它們相連的邊數全部冊除 else if((k&(1<<i))!=0&&(k&(1<<j))!=0)ans+=map[i][j]; } return ans;}int main(){ int m,t,a,b; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(map,0,sizeof(map)); while(m--) { scanf("%d%d",&a,&b); map[a][b]++; map[b][a]++;//必須這樣才能對 } int MIN=99999999; if(n==0||m==0)printf("0\n"); else{ for(int k=1;k<(1<<n);k++)//把n個數分成兩部分有2^n種狀態,每種狀態為k,在該狀態和第i位同為0的為第一部分,同為1的為另一部分 { int t=deletEdg(k); if(t<MIN)MIN=t; } printf("%d\n",MIN); } }}