点的双联通+二分图的判定(poj2942)

Knights of the Round Table

Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 10804   Accepted: 3553

Description

Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising that in recent years the kingdom of King Arthur has experienced an unprecedented increase in the number of knights. There are so many knights now, that it is very rare that every Knight of the Round Table can come at the same time to Camelot and sit around the round table; usually only a small group of the knights isthere, while the rest are busy doing heroic deeds around the country.

Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:

  • The knights should be seated such that two knights who hate each other should not be neighbors at the table. (Merlin has a list that says who hates whom.) The knights are sitting around a roundtable, thus every knight has exactly two neighbors.
  • An odd number of knights should sit around the table. This ensures that if the knights cannot agree on something, then they can settle the issue by voting. (If the number of knights is even, then itcan happen that ``yes" and ``no" have the same number of votes, and the argument goes on.)

Merlin will let the knights sit down only if these two rules are satisfied, otherwise he cancels the meeting. (If only one knight shows up, then the meeting is canceled as well, as one person cannot sit around a table.) Merlin realized that this means that there can be knights who cannot be part of any seating arrangements that respect these rules, and these knights will never be able to sit at the Round Table (one such case is if a knight hates every other knight, but there are many other possible reasons). If a knight cannot sit at the Round Table, then he cannot be a member of the Knights of the Round Table and must be expelled from the order. These knights have to be transferred to a less-prestigious order, such as the Knights of the Square Table, the Knights of the Octagonal Table, or the Knights of the Banana-Shaped Table. To help Merlin, you have to write a program that will determine the number of knights that must be expelled.

Input

The input contains several blocks of test cases. Each case begins with a line containing two integers 1 ≤ n ≤ 1000 and 1 ≤ m ≤ 1000000 . The number n is the number of knights. The next m lines describe which knight hates which knight. Each of these m lines contains two integers k1 and k2 , which means that knight number k1 and knight number k2 hate each other (the numbers k1 and k2 are between 1 and n ).

The input is terminated by a block with n = m = 0 .

Output

For each test case you have to output a single integer on a separate line: the number of knights that have to be expelled.

Sample Input

5 5
1 4
1 5
2 5
3 4
4 5
0 0

Sample Output

2题意:一群骑士要召开圆桌会议,坐在圆桌上的人都有两个邻居满足两个要求:每个人与身边的邻居都不能有矛盾,且围坐的人数必须是奇数题目给出m对矛盾关系,问有多少人一定是不能参加会议的分析:1.首先做补图,把题中给出的边舍去,把没有给出的边建图2.然后在图中找奇数环,求点的双连通块3.在双连通块中找奇数点的环,如果一个联通块中存在一个奇数环,那么该连通块中的每个人都有可能成为一个奇数环中的一员,此时把可能成为一员的人标记4.找奇数环实际上就是判断该联通块是否是二分图,即用黑白交叉染色法判断,如果某条边的两个端点有相同的颜色,就存在了奇数环程序:

  1 #include"stdio.h"
  2 #include"string.h"
  3 #include"stdlib.h"
  4 #include"algorithm"
  5 #include"queue"
  6 #include"math.h"
  7 #include"iostream"
  8 #include"vector"
  9 #define M 1009
 10 #define inf 0x3f3f3f3f
 11 #define eps 1e-9
 12 #define PI acos(-1.0)
 13 #include"map"
 14 #include"vector"
 15 #include"set"
 16 #include"string"
 17 #include"stack"
 18 #define LL __int64
 19 using namespace std;
 20 struct node
 21 {
 22     int u,v,next;
 23 }edge[M*M*2];
 24 int t,head[M],dfn[M],low[M],cut[M],belong[M*M*2],use[M],color[M],exist[M];
 25 int indx,num;
 26 int G[M][M];
 27 stack<int>q;
 28 vector<int>bian[M];
 29 void init()
 30 {
 31     t=0;
 32     memset(head,-1,sizeof(head));
 33 }
 34 void add(int u,int v)
 35 {
 36     edge[t].u=u;
 37     edge[t].v=v;
 38     edge[t].next=head[u];
 39     head[u]=t++;
 40 }
 41 int dfs(int u)//交叉染色判断二分图
 42 {
 43     for(int i=0;i<(int)bian[u].size();i++)
 44     {
 45         int v=bian[u][i];
 46         if(color[v]==-1)
 47         {
 48             color[v]=color[u]^1;
 49             int tt=dfs(v);
 50             if(tt)
 51                 return 1;
 52         }
 53         else if(color[v]==color[u])
 54             return 1;
 55     }
 56     return 0;
 57 }
 58 void tarjan(int u,int fa,int id)
 59 {
 60     dfn[u]=low[u]=++indx;
 61     for(int i=head[u];~i;i=edge[i].next)
 62     {
 63         int v=edge[i].v;
 64         if(i==(id^1))continue;
 65         if(!dfn[v])
 66         {
 67             q.push(i);//把边入栈
 68             tarjan(v,u,i);
 69             low[u]=min(low[u],low[v]);
 70             if(dfn[u]<=low[v])//求割点
 71             {
 72                 cut[u]++;
 73                 int x;
 74                 num++;
 75                 int cnt=0;
 76                 map<int,int>mp;
 77                 do//当搜到一个割点u的时候把栈中边出栈,所有出栈的边都属于一个点双连通块
 78                 {
 79                     x=q.top();
 80                     q.pop();
 81                     belong[x]=belong[x^1]=num;
 82                     bian[edge[x].u].push_back(edge[x].v);//建立连通块联通图
 83                     bian[edge[x].v].push_back(edge[x].u);
 84                     if(!mp[edge[x].u])//离散化点
 85                     {
 86                         mp[edge[x].u]=1;
 87                         use[cnt++]=edge[x].u;
 88                     }
 89                     if(!mp[edge[x].v])
 90                     {
 91                         mp[edge[x].v]=1;
 92                         use[cnt++]=edge[x].v;
 93                     }
 94
 95                 }while(i!=x);
 96                 color[use[0]]=1;
 97                 if(dfs(use[0]))//如果存在奇数环,把点标记
 98                 {
 99                     for(int i=0;i<cnt;i++)
100                         exist[use[i]]=1;
101                 }
102                 for(int i=0;i<cnt;i++)//清理初始化边和color标记
103                 {
104                     bian[use[i]].clear();
105                     color[use[i]]=-1;
106                 }
107             }
108         }
109         else if(low[u]>dfn[v])
110         {
111             low[u]=dfn[v];
112             q.push(i);//把边入栈
113         }
114     }
115     if(fa<0)
116         cut[u]--;
117 }
118 int slove(int n)
119 {
120     for(int i=0;i<=n;i++)
121         bian[i].clear();
122     indx=num=0;
123     memset(cut,0,sizeof(cut));
124     memset(dfn,0,sizeof(dfn));
125     memset(use,0,sizeof(use));
126     memset(color,-1,sizeof(color));
127     memset(exist,0,sizeof(exist));
128     for(int i=1;i<=n;i++)
129     {
130         if(!dfn[i])
131             tarjan(i,-1,-1);
132     }
133     int ans=0;
134     for(int i=1;i<=n;i++)
135     {
136         if(!exist[i])
137             ans++;
138     }
139     return ans;
140 }
141 int main()
142 {
143     int n,m;
144     while(scanf("%d%d",&n,&m),m||n)
145     {
146         init();
147         memset(G,0,sizeof(G));
148         for(int i=1;i<=m;i++)
149         {
150             int a,b;
151             scanf("%d%d",&a,&b);
152             G[a][b]=G[b][a]=1;
153         }
154         for(int i=1;i<=n;i++)
155         {
156             for(int j=i+1;j<=n;j++)
157             {
158                 if(!G[i][j])
159                 {
160                     add(i,j);
161                     add(j,i);
162                 }
163             }
164         }
165         int ans=slove(n);
166         printf("%d\n",ans);
167     }
168     return 0;
169 }

 
时间: 2024-12-24 21:15:56

点的双联通+二分图的判定(poj2942)的相关文章

poj2942 Knights of the Round Table,无向图点双联通,二分图判定

点击打开链接 无向图点双联通,二分图判定 <span style="font-size:18px;">#include <cstdio> #include <stack> #include <vector> #include <algorithm> #include <cstring> using namespace std; struct Edge{ int u, v; }; const int maxn = 1

UVA 1364 - Knights of the Round Table(双连通+二分图判定)

UVA 1364 - Knights of the Round Table 题目链接 题意:有n个圆桌骑士,知道一些骑士互相憎恨,现在要开圆桌会议,每次最少3个人,必须是奇数人数,并且互相憎恨的骑士不能在相邻,问有多少骑士是一次都无法参加的 思路:把每个骑士可以相邻的连边,然后做双连通分量,然后对于每个连通分量,利用二分图染色判定去判断是否是奇圈 代码: #include <cstdio> #include <cstring> #include <vector> #in

双联通分量与二分图

我这是耽搁了多长时间才把它整完哈哈哈哈哈: 双联通分量 在无向图中,如果无论删去哪条边都不能使得 u 和 v 不联通,则称 u 和 v 边双连通: 在无向图中,如果无论删去哪个点(非 u 和 v)都不能使得 u 和v 不联通,则称 u 和 v 点双连通. 割点:删去该点,图分裂为多个连通块. 割边:也叫"桥",删去该边,图分裂为多个连通块. 点双连通分量 类似地,定义$ d f n_u 和 low_u$. 如果 v 是 u 的子结点,并且 $low_v ≥ d f n_u $则点 u

hihocoder #1190 : 连通性&#183;四 点双联通分量

http://hihocoder.com/problemset/problem/1190?sid=1051696 先抄袭一下 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho从约翰家回到学校时,网络所的老师又找到了小Hi和小Ho. 老师告诉小Hi和小Ho:之前的分组出了点问题,当服务器(上次是连接)发生宕机的时候,在同一组的服务器有可能连接不上,所以他们希望重新进行一次分组.这一次老师希望对连接进行分组,并把一个组内的所有连接关联的服务器也视为这个组内

POJ 2942 - Knights of the Round Table(双连通图 Tarjan + 二分判定)

POJ 2942 - Knights of the Round Table(双连通图 Tarjan + 二分判定) ACM 题目地址: POJ 2942 - Knights of the Round Table 题意: 有N个骑士,给出某些骑士之间的仇恨关系,骑士们开会时会围坐在一个圆桌旁.一次会议能够顺利举行,要满足两个条件: 任意相互憎恨的两个骑士不能相邻 开会人数为大于2的奇数 若某个骑士任何会议都不能参加,那么就必须将他踢出,给出骑士之间的仇恨关系,问最少需要踢出多少个骑士? 分析: 把

UVA 1364 - Knights of the Round Table (找双连通分量 + 二分图染色法判断)

都特么别说话,我先A了这道题! 卧槽啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议,为了不引发骑士之间的冲突, 并且能够让会议的议题有令人满意的结果,每次开会前都必须对出席会议的骑士有如下要求: 1.  相互憎恨的两个骑士不能坐在直接相邻的2个位置: 2.  出席会议的骑士数必须是奇数,这是为了让投票表决议题时都能有结果. 注意:1.所给出的憎恨关系一定是双向的,不存在单向憎恨关系. 2.由于是圆桌会议,则每个出席的骑士身边必定刚好有2个骑士. 即每个骑士的座位两边都

[HDOJ4738]Caocao&#39;s Bridges(双联通分量,割边,tarjan)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 给一张无向图,每一条边都有权值.找一条割边,使得删掉这条边双连通分量数量增加,求权值最小那条. 注意有重边,ACEveryDay里群巨给的意见是tarjan的时候记录当前点是从哪条边来的. 注意假如桥的权值是0的时候也得有一个人去炸…… 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7

UVA - 10765 Doves and bombs (双联通分量)

链接 :  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34798 给N个点的无向图并且联通,问删除每次一个点之后还剩多少联通分量. 找割顶 如果删除的是割顶 联通分量就会增加,否则还是1(因为原图是联通图),删除割顶之后 联通块的数目 就要看该割顶在几个双联通分量里出现过. #pragma comment(linker, "/STACK:10240000,10240000") #include <a

POJ 3177 Redundant Paths(边双联通图)

Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forc