【POJ - 2942】Knights of the Round Table(点双连通分量,二分图判断奇环奇圈)
題干:
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 0Sample Output
2Hint
Huge input file, 'scanf' recommended to avoid TLE.?
題目大意:
亞瑟王要在圓桌上召開騎士會議,為了不引發騎士之間的沖突,并且能夠讓會議的議題有令人滿意的結果,每次開會前都必須對出席會議的騎士有如下要求: 1、? 相互憎恨的兩個騎士不能坐在直接相鄰的2個位置; 2、? 出席會議的騎士數必須是奇數,這是為了讓投票表決議題時都能有結果。 ? 如果出現有某些騎士無法出席所有會議(例如這個騎士憎恨所有的其他騎士),則亞瑟王為了世界和平會強制把他剔除出騎士團。 ?????? 現在給定準備去開會的騎士數n,再給出m對憎恨對(表示某2個騎士之間使互相憎恨的),問亞瑟王至少要剔除多少個騎士才能順利召開會議?
一句話題意:
一些騎士,他們有些人之間有矛盾,現在要求選出一些騎士圍成一圈,圈要滿足如下條件:1.人數大于1。2.總人數為奇數。3.有仇恨的騎士不能挨著坐。問有幾個騎士不能和任何人形成任何的圓圈。
解題報告:
注意這里的col數組不是給tarjan用的,而是給二分圖染色用的。
注意割點會被flag[]=1多次,所以不能在過程中紀錄ans,而是要標記can[]=1,最后遍歷can數組看有多少等于1的。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAXN = 1000 + 5; const int MAXM = 2e6 + 5; bool maze[MAXN][MAXN],can[MAXN]; struct Edge {int u,v;int ne; } e[MAXM]; int dfn[MAXN],low[MAXN],stk[MAXN],clk,tot,index,bcc; int flag[MAXN],tmp[MAXN],col[MAXN],head[MAXN]; int n,m; void init() {for(int i = 1; i<=n; i++) {dfn[i]=low[i]=can[i]=0;//如果不初始化fa???head[i] = -1;}tot=0;clk=bcc=index=0; } void add(int u,int v) {e[++tot].u = u;e[tot].v = v;e[tot].ne = head[u];head[u] = tot; } bool bfs(int st) {queue<int> q;q.push(st);col[st] = 0;while(!q.empty()) {int cur = q.front();q.pop();for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].v;if(flag[v] == 0) continue;if(col[v] != -1) {if(col[v] == col[cur]) return 1;}else {col[v] = !col[cur];q.push(v);}}}return 0; } void tarjan(int x,int fa) {dfn[x] = low[x] = ++clk;stk[++index] = x;for(int i = head[x]; ~i; i = e[i].ne) {int v = e[i].v;if(v == fa) continue;if(dfn[v] == 0) {tarjan(v,x);low[x] = min(low[x],low[v]);if(low[v] >= dfn[x]) {bcc++;//這里點雙也用bcc表示一下。。for(int i = 1; i<=n; i++) flag[i] = 0,col[i]=-1; int cnt = 0;tmp[++cnt] = x; while(1) {int tmpp = stk[index];index--;tmp[++cnt] = tmpp; // col[tmp] = bcc; //這里不需要記錄顏色,col數組用來二分圖染色比較好。 if(tmpp == v) break;//注意這里不是tmp==x,因為x還要在棧內,做其他點雙聯通分量的成員。而當前bcc,將x單獨處理已下架就好。 } //別忘處理割點:for(int j = 1; j<=cnt; j++) flag[tmp[j]] = 1;if(bfs(x) == 1) {for(int j = 1; j<=cnt; j++) can[tmp[j]]=1;}} }else low[x] = min(low[x],dfn[v]);} } int main() {while(~scanf("%d%d",&n,&m)) {if(n == 0 && m == 0) break;memset(maze,1,sizeof maze);init();// 每次都是 寫好了init函數,忘了加到主函數中。。 for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);maze[u][v] = maze[v][u] = 0;}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(i!=j && maze[i][j]) add(i,j);}}for(int i = 1; i<=n; i++) {if(dfn[i] == 0) tarjan(i,-1);}int ans = 0;for(int i = 1; i<=n; i++) {ans += can[i];}printf("%d\n",n - ans);}return 0 ; }?
總結
以上是生活随笔為你收集整理的【POJ - 2942】Knights of the Round Table(点双连通分量,二分图判断奇环奇圈)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sshd.exe - sshd进程是什么
- 下一篇: 【HDU - 6574】Rng(概率,古