zoukankan      html  css  js  c++  java
  • Knights of the Round Table-POJ2942(双连通分量+交叉染色)

    Knights of the Round Table

    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

    Hint

    Huge input file, 'scanf' recommended to avoid TLE. 

    Source


    题意:

    由于无视武士之间容易打架,Merlin智者为了发生武士打架,有两个规则:
    (1):相互仇视的武士不能挨着做,由于是坐在圆桌周围,每一个武士有两个相邻的武士。
    (2):围着圆桌做的武士数目必须是奇数个,以确保在发生问题时,能够投票解决。
    满足以上两个条件的武士可以坐下来,如果一个武士不可能被安排坐下,他将被从武士的名单中去除。
    智者想知道有多少武士将被去除。

    方法:
    构造无向图,将不相互仇视的建立边。
    (1):搜索双连通分量。DFS过程中,用一个栈保存所有经过的点,判断割点,碰到割点就将标记栈的顶点并退栈,直到当前节点停止标记当前割点。标记过的节点在同一个连通分量里。
    (2):交叉染色搜索奇偶。在一个节点大于2的双连通分量中,必定存在一个圈经过所有的节点,如果这个圈是奇圈,则该连通分量的所有点都满足,如果是偶圈,如果包含奇圈,必定还有一个奇圈经过剩余的所有节点,因此一个双连通分量里只要存在一个奇圈。
    (3):一个点可能满足多个连通分量

    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #include <cmath>
    #include <cstdlib>
    #include <algorithm>
    #define LL long long
    
    using namespace std;
    
    const int Max = 1100; 
    
    typedef struct Node
    {
    	int num;
    
    	int next;
    }Line;
    
    Line Li[Max*100];
    
    int top;
    
    int Head[Max];
    
    int low[Max],dfn[Max];
    
    int Map[Max][Max];
    
    int n,m;
    
    int Num,num,ant;
    
    int Que[Max];
    
    int vis[Max];
    
    int part[Max],color[Max];
    
    void AddNum(int u)
    {
    	Li[top].num=num;
    	Li[top].next = Head[u];
    	Head[u] = top++;
    }
    
    void dfs(int u,int father)
    {
    	vis[u]=1;//表示正在访问
    
    	dfn[u]=low[u]=Num++;
    
    	Que[ant++]=u;
    
    	for(int i=1;i<=Map[u][0];i++)
    	{
    		if(vis[Map[u][i]]==1&&Map[u][i]!=father)
    		{
    			low[u]=min(low[u],dfn[Map[u][i]]);
    		}
    		if(vis[Map[u][i]]==0)
    		{
    			dfs(Map[u][i],u);
    
    			low[u]=min(low[Map[u][i]],low[u]);
    
    			if(low[Map[u][i]]>=dfn[u])//子树形成环
    			{
    				// 标记
    				AddNum(u);
    
    				for(int j=Que[ant];j!=Map[u][i];AddNum(j))
    				{
    					j=Que[--ant];
    				}
    
    				num++ ;
    			}
    		}
    	}
    
    	vis[u]=2;//表示已经访问并且处理完
    }
    
    int OddCycle(int u,int flag)//判断奇环
    {
    	color[u]=flag;
    
    	for(int i=1;i<=Map[u][0];i++)
    	{
    		if(!part[Map[u][i]])
    		{
    			continue;
    		}
    
    		if(color[Map[u][i]]==0 && OddCycle(Map[u][i],-flag))
    		{
    			return 1;
    		}
    		if(color[Map[u][i]]==flag)
    		{
    			return 1;
    		}
    	}
    	return 0;
    }
    
    int main()
    {
    	int u,v;
    
    	while(~scanf("%d %d",&n,&m)&&(n+m))
    	{
    
    		// 数据处理
    		memset(Map,0,sizeof(Map));
    
    		for(int i=1;i<=m;i++)
    		{
    			scanf("%d %d",&u,&v);
    
    			Map[u][v]=Map[v][u]=1;
    		}
    
    		for(int i=1;i<=n;i++)
    		{
    			Map[i][i]=1;
    		}
    		//建图
    		for(int i=1;i<=n;i++)
    		{
    			for(int j=1;j<=n;j++)
    			{
    				Map[0][j]=Map[i][j];
    			}
    
    			for(int j=1;j<=n;j++)
    			{
    				if(!Map[0][j])
    					Map[i][++Map[i][0]]=j;
    			}
    		}
    
    		memset(vis,0,sizeof(vis)); // 标记是否被访问,以及访问的状态
    		
    		memset(Head,-1,sizeof(Head));
    
    		Num = 0; num=0 ; top=0;ant = 0;
    
    		//去除割边
    
    		for(int i=1;i<=n;i++)
    		{
    			if(!vis[i])
    			{
    				Num = 0;
    				dfs(i,0);
    			}
    		}
    
    		memset(vis,0,sizeof(vis));
    
    		for(int i=0;i<num;i++)//判断奇环
    		{
    
    			memset(part,0,sizeof(part));
    
    			memset(color,0,sizeof(color));
    
    			for(int j=1;j<=n;j++)
    			{
    				for(int k=Head[j];k!=-1;k=Li[k].next)
    				{
    					if(Li[k].num == i)
    					{
    						part[j]=1;
    						break;
    					}
    				}
    			}
    
    			for(int j=1;j<=n;j++)
    			{
    				if(part[j])
    				{
    					if(OddCycle(j,1))
    					{
    						for(int k=1;k<=n;k++)
    						{
    							vis[k]+=part[k];
    						}
    					}
    					break;
    				}
    			}
    		}
    
    		int ans = 0;
    
    		for(int i=1;i<=n;i++)
    		{
    			if(!vis[i])
    			{
    				ans++;
    			}
    		}
    
    		printf("%d
    ",ans);
    	}
    	return 0;
    }


  • 相关阅读:
    luogu P3295 [SCOI2016]萌萌哒
    luogu P4916 魔力环
    CF997C Sky Full of Stars
    CF961G Partitions
    android屏蔽软键盘并且显示光标
    设置和获取Android中各种音量
    自定义广播
    发送广播
    android取高度
    Java数字格式化
  • 原文地址:https://www.cnblogs.com/juechen/p/5255895.html
Copyright © 2011-2022 走看看