HDU 3118 判断奇圈

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 }
时间: 2024-10-21 20:04:15

HDU 3118 判断奇圈的相关文章

[转载]HDU 3478 判断奇环

题意:给定n个点,m条边的无向图(没有重边和子环).从给定点出发,每个时间走到相邻的点,可以走重复的边,相邻时间不能停留在同一点,判断是否存在某个时间停留在任意的n个点. 分析: (1)首先,和出发点的位置没有关系.因为可以走重复的边,且时间没有限制大小. (2)图必须是联通的 (3) 1)图为:2-0-1-3 从0点出发(时间为0),一个时间后到达1或2(时间为1),再一个时间后到达0或3(时间为2)... 可以发现,点分为两类,奇数时间到达和偶数时间到达,答案为NO 2)图为:2-0-1-2

poj 2942 Knights of the Round Table 【双连通缩点+判奇圈】【经典】

题目:poj 2942 Knights of the Round Table 题意:n个骑士经常一起开会,其中有一些两两相互憎恨,他们不能同一桌,开会要表决一些事情,所以必须奇数个人,最少3个,求永远也参加不了会议的人的个数. 分析:这个题目两点 首先,建图求双连通缩点 建图的话,因为相互憎恨的不能再一块,所以要建补图,让能够在一起的所有的连接,这样的话,如果能存在环且环上的点是奇数个的话就可以参加会议,标记求不能参加的即可. 建好图之后用tarjan算法双连通缩点,把在一个环上的点保存起来.

HDU 3118 Arbiter 判定奇圈

题目来源:HDU 3118 Arbiter 题意:翻译过来就是不能有奇圈 每走一步状态会变化 当他回到起点时如果和原来的状态不一样 可能会死 求至少去掉多少条边可以避免这种状况发生 思路:二分图是没有奇圈的 最多就15个点 我们用状态压缩枚举那些点是在二分图的一边和另外一边 确定二分图之后枚举输入的边 每条边连接的不能是同一集合的点 不符合就去掉 最后取小 #include <cstdio> #include <cstring> #include <cstdlib> #

Hdu 4594 Difference(奇圈判断+差分约束)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4598 思路:由题意可知两相连点ai符号一定相反,所以若存在奇圈则一定无解.染色,colour[i]==1表示为正,colour[i]==2表示为负.由于(b)条件为充要条件,所以对于图中的点| a[i]-a[j] | >= T,对于非图中点| a[i]-a[j] | < T,即| a[i]-a[j] | <= T-1 .所以图中点,若colour[i]==1,a[i]-a[j] >=

FZU2181+poj2942(点双连通+判奇圈)

分析:我们对于那些相互不憎恨的人连边,将每次参加会议的所有人(不一定是全部人,只需人数>=3且为奇数)看做一个点双联通分量,那么每个点都至少有两个点与他相邻.即需要保证双联通分量中存在奇圈.至于如何判奇圈,这里有一个性质:一个图是二分图当且仅当图中不存在奇圈.至于如何判断一个图是否是二分图,可以采用交替染色的方式判断. 传送门:FZU 2181 快来买肉松饼 #include<iostream> #include<cstdio> #include<cstring>

poj2942圆桌骑士(点双连通分量+二分图染色法判奇圈)

之前一直不明白点双连通分量能用来干嘛,比如边双连通分量可以问加几条边能变成边双连通,这个题目是这样的,每个圆桌会议至少三个骑士参加,因为需要表决意见,所以骑士数目必须是奇数个,直到那些骑士互相憎恨,也就是不能坐在一起的,把能坐在一起的建边,求无法参加任何会议的骑士的个数,重点是任何会议,这点非常关键,这道题之前一直卡在这里,还有就是有的人属于好几种双连通分量,所以全部标记之后再减掉比较好,至于奇数个怎么处理呢,今天才知道原来二分图的判断可以解决奇圈的问题,因为如果是二分图的话,我们染色,相邻的涂

1119: 零起点学算法26——判断奇偶数

1119: 零起点学算法26--判断奇偶数 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 2419  Accepted: 1508[Submit][Status][Web Board] Description 输入一个整数,判断是奇数还是偶数 Input 输入1个正整数(多组数据) Output 如果是奇数,输出odd否则输出even(每组数据一行) Sample Input 2 Sample O

POJ2942 Knights of the Round Table 点双连通分量,逆图,奇圈

题目链接: poj2942 题意: 有n个人,能够开多场圆桌会议 这n个人中,有m对人有仇视的关系,相互仇视的两人坐在相邻的位置 且每场圆桌会议的人数仅仅能为奇书 问有多少人不能參加 解题思路: 首先构图,将全部的仇视关系视为一条边,最后再取已经得到的图的逆图, 这样图上连接的边就代表能够相邻而坐的关系 然后就是找奇圈了,首先就是要找图中的环(点双连通分量) 这个环为奇环的条件:不是二分图||这个环中的部分点属于其它奇环 这个推断能够通过将已找到的环进行dfs黑白染色推断 最后不在奇环内的总和即

hdu 4587 判断孤立点+割点+ 删除点之后,剩下多少连通分量

做了很久...... 题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4587 先枚举删除的第一个点,第二个点就是找割点,没有割点当然也有答案 学到的: 1.图论硬套模板不太现实,比如这道题,我能想到孤立点是特殊情况,删除孤立点,连通分支个数会减少一,但是一直处理不好,最后按缩点的做法搞了, 判断是不是孤立点的方法: 就是先用一个数组scnt[i]=j,vv[j]++  表示点i在以j为祖先的联通分支里,而且每次都让vv[j]++,就使得vv[j