标签:therefore cannot tarjan namespace country rate dfs problem scanf
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:
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
Output
Sample Input
5 5 1 4 1 5 2 5 3 4 4 5 0 0
Sample Output
2
Hint
题目大意 有n个骑士和m个对憎恨关系,每次圆桌会议至少3人参加且人数必须为奇数,互相憎恨的骑士不能邻座。问有多少骑士不可能参加任何一场圆桌会议。
首先直接按照憎恨关系建图总之我做不出来。所以考虑补图,补图的意义是可以邻座的关系建出来的图(正难则反)。那么一个骑士可以参加某场圆桌会议就等价于存在一个长度为奇数的点-双连通分量(下面称这么一个东西为奇环)包含它。
所以可以考虑把所有极大点-双连通分量求出来,然后dfs染色进行判断。如果一个点-双联通分量中存在一个长度为奇环,那么这个点-双连通分量内的所有奇环覆盖了这中间所有点。(画画图就知道了)
所以如果一个点-双连通分量不能够被染色,就可以把它包含的所有点标为可以参加某场会议。
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
标签:therefore cannot tarjan namespace country rate dfs problem scanf
原文地址:http://www.cnblogs.com/yyf0309/p/7341449.html