Arbiter
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 878 Accepted Submission(s): 442
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
题目意思:
给n个点、m条边的无向图,问最少删除多少条边后图中不含奇圈。
思路:
不含奇圈,很明显是二分图。那么题目就是一副图删除多少条边成为二分图。
题中n最多为15,就想到状压,1为左边,0为右边,左边和右边两个集合内部若含边则num++。ans=min(ans,num)即可。
题中说了可能有重边,这里wa了一次。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 using namespace std; 10 11 #define N 16 12 #define inf 999999999 13 14 int max(int x,int y){return x>y?x:y;} 15 int min(int x,int y){return x<y?x:y;} 16 int abs(int x,int y){return x<0?-x:x;} 17 18 bool visited[1<<N]; 19 20 main() 21 { 22 int t; 23 int n, m; 24 int map[N][N]; 25 int i, j, k; 26 int x, y; 27 int a[N]; 28 int b[N]; 29 cin>>t; 30 while(t--){ 31 scanf("%d %d",&n,&m); 32 memset(map,0,sizeof(map)); 33 while(m--){ 34 scanf("%d %d",&x,&y); 35 map[x][y]++;map[y][x]++;; 36 } 37 memset(visited,false,sizeof(visited)); 38 int c[N]; 39 c[0]=1; 40 for(i=1;i<n;i++) c[i]=c[i-1]<<1; 41 int cnt=1<<n; 42 int l1, l2; 43 int ans=inf; 44 for(i=0;i<cnt;i++){ 45 l1=l2=0; 46 int num=0; 47 for(j=0;j<n;j++){ 48 if(c[j]&i) a[l1++]=j; 49 else b[l2++]=j; 50 } 51 for(j=0;j<l1;j++){ 52 for(k=j+1;k<l1;k++){ 53 if(map[a[j]][a[k]]) num+=map[a[j]][a[k]]; 54 } 55 } 56 for(j=0;j<l2;j++){ 57 for(k=j+1;k<l2;k++){ 58 if(map[b[j]][b[k]]) num+=map[b[j]][b[k]]; 59 } 60 } 61 ans=min(ans,num); 62 } 63 printf("%d\n",ans); 64 } 65 }