1. 程式人生 > >poj 2942 Knights of the Round Table - Tarjan

poj 2942 Knights of the Round Table - Tarjan

str 畫畫 class gin signed include rec col rules

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.

  題目大意 有n個騎士和m個對憎恨關系,每次圓桌會議至少3人參加且人數必須為奇數,互相憎恨的騎士不能鄰座。問有多少騎士不可能參加任何一場圓桌會議。

  首先直接按照憎恨關系建圖總之我做不出來。所以考慮補圖,補圖的意義是可以鄰座的關系建出來的圖(正難則反)。那麽一個騎士可以參加某場圓桌會議就等價於存在一個長度為奇數的點-雙連通分量(下面稱這麽一個東西為奇環)包含它。

  所以可以考慮把所有極大點-雙連通分量求出來,然後dfs染色進行判斷。如果一個點-雙聯通分量中存在一個長度為奇環,那麽這個點-雙連通分量內的所有奇環覆蓋了這中間所有點。(畫畫圖就知道了)

  所以如果一個點-雙連通分量不能夠被染色,就可以把它包含的所有點標為可以參加某場會議。

Code

  1 /**
  2  * poj
  3  * Problem#2942
  4  * Accepted
  5  * Time: 1141ms
  6  * Memory: 1232k
  7  */
  8 #include <iostream>
  9 #include <cstdio>
 10 #include <ctime>
 11 #include <cmath>
 12 #include <cctype>
 13 #include <cstring>
 14 #include <cstdlib>
 15 #include <fstream>
 16 #include <sstream>
 17 #include <algorithm>
 18 #include <map>
 19 #include <set>
 20 #include <stack>
 21 #include <queue>
 22 #include <vector>
 23 using namespace std;
 24 typedef bool boolean;
 25 #define smin(a, b)    a = min(a, b)
 26 
 27 ///map template starts
 28 typedef class Edge{
 29     public:
 30         int end;
 31         int next;
 32         int w;
 33         
 34         Edge(const int end = 0, const int next = -1, const int w = 0):end(end), next(next), w(w) {    }
 35 }Edge;
 36 
 37 typedef class MapManager{
 38     public:
 39         int ce;
 40         int *h;
 41         vector<Edge> edge;
 42         
 43         MapManager() {    }
 44         MapManager(int points):ce(0) {
 45             h = new int[(const int)(points + 1)];
 46             memset(h, -1, sizeof(int) * (points + 1));
 47         }
 48         
 49         inline void addEdge(int from, int end, int w) {
 50             edge.push_back(Edge(end, h[from], w));
 51             h[from] = ce++;
 52         }
 53         
 54         inline void addDoubleEdge(int from, int end, int w) {
 55             addEdge(from, end, w);
 56             addEdge(end, from, w);
 57         }
 58         
 59         inline void clear() {
 60             delete[] h;
 61             edge.clear();
 62         }
 63         
 64         Edge& operator [] (int pos) {
 65             return edge[pos];
 66         }
 67 }MapManager;
 68 #define m_begin(g, i) (g).h[(i)]
 69 #define m_endpos -1
 70 ///map template ends
 71 
 72 int n, m;
 73 boolean hated[1001][1001];
 74 MapManager g;
 75 
 76 inline boolean init() {
 77     scanf("%d%d", &n, &m);
 78     if(!n && !m)    return false;
 79     memset(hated[1], false, sizeof(hated[1]) * n);
 80     g = MapManager(n);
 81     for(int i = 1, u, v; i <= m; i++) {
 82         scanf("%d%d", &u, &v);
 83         hated[u][v] = hated[v][u] = true;
 84     }
 85     return true;
 86 }
 87 
 88 int cnt = 0;
 89 int cc = 0;
 90 stack<int> s;
 91 int visitID[1001];
 92 int exitID[1001];
 93 boolean visited[1001];
 94 int counter[1001];
 95 vector< vector<int> > contains;
 96 inline void init_tarjan() {
 97     for(int i = 1; i <= n; i++)
 98         for(int j = i + 1; j <= n; j++)
 99             if(i != j && !hated[i][j])
100                 g.addDoubleEdge(i, j, 0);
101     
102     cnt = 0, cc = 0;
103     memset(visited, false, sizeof(boolean) * (n + 1));
104 }
105 
106 void tarjan(int node, int fae) {
107     visitID[node] = exitID[node] = ++cnt;
108     visited[node] = true;
109     
110     for(int i = m_begin(g, node); i != m_endpos; i = g[i].next) {
111         if(i == fae)    continue;
112         int& e = g[i].end;
113         if(!visited[e]) {
114             s.push(i);
115             tarjan(e, i ^ 1);
116             smin(exitID[node], exitID[e]);
117             if(exitID[e] >= visitID[node]) {
118                 int j = 0;
119                 cc++;
120                 vector<int> l;
121                 do {
122                     j = s.top();
123                     s.pop();
124                     g[j].w = g[j ^ 1].w = cc;
125                     l.push_back(j);
126                     l.push_back(j ^ 1);
127 //                    printf("%d %d %d\n", g[j ^ 1].end, g[j].end, cc);
128                 } while (j != i);
129                 contains.push_back(l);
130             }
131         } else {
132             smin(exitID[node], visitID[e]);
133             if(visitID[e] < visitID[node])
134                 s.push(i);
135         }
136     }
137 }
138 
139 int color[1001]; 
140 boolean dfs(int node, int limit, int c) {
141     if(color[node] != -1)    return color[node] == c;
142     color[node] = c;
143     for(int i = m_begin(g, node); i != m_endpos; i = g[i].next) {
144         if(g[i].w != limit)    continue;
145         int& e = g[i].end;
146         if(!dfs(e, limit, c ^ 1))    return false;
147     }
148     return true;
149 }
150 
151 int res;
152 boolean aced[1001];
153 inline void solve() {
154     res = 0;
155     memset(aced, false, sizeof(int) * (n + 1));
156 //    cout << cc << endl;
157     for(int c = 0; c < (signed)contains.size(); c++) {
158         memset(color, -1, sizeof(int) * (n + 1));
159         boolean aFlag = dfs(g[contains[c][0]].end, c + 1, 0);
160         if(!aFlag)
161             for(int i = 0; i < (signed)contains[c].size(); i++)
162                 aced[g[contains[c][i]].end] = true;
163     }
164     for(int i = 1; i <= n; i++)
165         if(!aced[i])
166             res++;
167     printf("%d\n", res);
168 }
169 
170 inline void clear() {
171     g.clear();
172     contains.clear();
173 }
174 
175 int main() {
176     while(init()) {
177         init_tarjan();
178          for(int i = 1; i <= n; i++)
179             if(!visited[i])
180                 tarjan(i, -1);
181         solve();
182         clear();
183     }
184     return 0;
185 }

poj 2942 Knights of the Round Table - Tarjan