Knights of the Round Table
Time Limit: 7000MS |
|
Memory Limit: 65536K |
Total Submissions: 10908 |
|
Accepted: 3585 |
Description
Being A knight is a very attractive career:searching for the Holy Grail, saving damsels in distress, and drinking with th E Other Knights is fun things to do. Therefore, it is not very surprising that in recent years the Kingdom of King Arthur have experienced an unprecedented incr Ease in the number of knights. There many knights now, that's it's very rare that every Knight of the Round Table can come at the same time to came Lot and sit around the round table; Usually small group of the Knights Isthere, while the rest is 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 this in the future no fights BRE AK out between the Knights. After studying the problem carefully, Merlin realized so the fights can only be prevented if the Knights is seated Acco Rding to the following the rules:
- The Knights should is seated such that, knights who hate each of the should not being neighbors at the table. (Merlin has a list that says who hates whom.) The Knights is sitting around a roundtable, thus every knight has exactly and neighbors.
- An odd number of knights should sit around the table. This ensures if the Knights cannot agree on something and then they can settle the issue by voting. (If the number of knights is even, then itcan happen that ' yes ' and ' no ' has the same number of votes, and the argument Goes on.)
Merlin would let the Knights sit down only if these and the rules is satisfied, otherwise he cancels the meeting. (If only one knight shows up and then the meeting was canceled as well, as one person cannot sit around a table.) Merlin realized that this means that there can is knights who cannot is part of any seating arrangements that respect thes E rules, and these knights would never be able-to-sit at the Round Table (one such case was if a knight hates every other kn Ight, but there is many other possible reasons). If A knight cannot sit at the Round table, then he cannot is a member of the Knights of the Round Table and must be Expell Ed from the order. These knights has to is 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 had to write a program that would 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 the 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-integers k1 and K2, which means, knight number K1 and knight number K2 hate each o ther (the numbers k1 and K2 are between 1 and N).
The input is terminated by a block with n = m = 0.
Output
For the test case, you have the to output a single integer on a separate line:the number of knights that has to be expelled.
Sample Input
5 51 41 52 53 44 50 0
Sample Output
2
Hint
Huge input file, ' scanf ' recommended to avoid TLE.
Test instructions: Some knights, they have a mutual dislike of the relationship between the two nasty people can not sit next to each meeting to have an odd number of knights sitting in the round table, can never sit in the Round Table Knight will be removed, ask the number of knights removed.
Analysis: This question is completely according to Rujia's Great White Book "Algorithm competition Introduction Classics-Training Guide" writes. Simply talk about the procedure. First, the non-annoying relationship is connected with the edge, the establishment of the graph G. The title is converted to the number of nodes that are not on any single "Simple Odd Circle". All nodes on a simple circle are bound to belong to the same double-connected component, and because of the singular ring in the two-connected component, every point of it will be on the odd ring, then the point of the connected component should not be deleted if there is a singular ring. While the two-dimensional graph does not exist singular ring, so to determine whether there is a singular ring, just to determine whether the connected component is a two-point graph. Note that, because a point may belong to more than two connected components, so the processing time can not directly add the number of two connected components, but each time the existence of a singular ring of the two connected components, the first mark not to reject the point, and then to count the points to be eliminated.
The two-connected component of the point is determined by the cross-staining method by Tarjan and judging the dichotomy graph.
Title Link: http://poj.org/problem?id=2942
Code Listing:
#include <set> #include <map> #include <cmath> #include <queue> #include <stack> #include <ctime> #include <cctype> #include <string> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;typedef long Long ll;typedef unsigned int uint;typedef unsigned long long ull;const int MAXN = + 5;const int maxv = 4000000 + 5;struct node{int V , Next; }graph[maxv];struct edge{int u,v; Edge () {} edge (int u,int v) {this, u = u; This is V = V; }};int n,m,a,b;bool tmap[maxn][maxn];int dfn[maxn];int low[maxn];int belong[maxn];int head[maxn];int num,idx,sccno; Stack<edge>sta;vector<int>bcc;int color[maxn];bool odd[maxn];void init () {memset (dfn,0,sizeof (DFN)); memset (low,0,sizeof (Low)); memset (head,-1,sizeof (head)); memset (tmap,false,sizeof (TMap)); memset (odd,false,sizeof (odd)); while (!sta.empty ()) Sta.pop (); idx=0; sccno=0; num=0;} void Add (int u,int v) {graph[num].v=v; Graph[num].next=head[u]; head[u]=num++;} void input () {for (int i=1;i<=m;i++) {scanf ("%d%d", &a,&b); Tmap[a][b]=tmap[b][a]=true; }}void Get_graph () {for (Int. i=1;i<=n;i++) for (int. j=i+1;j<=n;j++) {Add (!tmap[i][j); Add (i,j ); }}bool bipartite (int u,int colors) {color[u]=colors; for (int i=head[u];i!=-1;i=graph[i].next) {int v=graph[i].v; if (!belong[v]) continue; if (Color[v]==color[u]) return false; if (color[v]==0) {if (!bipartite (v,3-colors)) return false; }}return true;} void Tarjan (int u,int father) {low[u]=dfn[u]=++idx; for (int i=head[u];i!=-1;i=graph[i].next) {int v=graph[i].v; if (v==father) continue; if (!dfn[v]) {//v is not marked, then (U,V) is the tree Edge Sta.push (Edge (U,V));//Edge into the stack Tarjan (v,u); Low[u]=min (Low[u],low[v]); if (Low[v]>=dfn[u]) {//child nodes cannot reach nodes earlier than u, then u is cut point sccno++; Memset (Belong,0,sizeof (belong)); memset (color,0,sizeof (color)); Bcc.clear (); while (!sta.empty ()) {//Take the points of the two connected components out of the Edge e=sta.top (); Sta.pop (); Bcc.push_back (E.U); Belong[e.u]=sccno; Bcc.push_back (E.V); Belong[e.v]=sccno; if (e.u==u&&e.v==v) break; } if (!bipartite (bcc[0],1)) {//If it is not a binary figure for (int j=0;j<bcc.size (); j + +) Odd[bcc[j]]=true; }}}} else if (Dfn[v]<dfn[u]) {//Back Edge Sta.push (U,V);//edge in stack Low[u]=min (Low[u],dfn[v]); }}}void FIND_SCC () {for (int i=1;i<=n;i++) {if (!dfn[i]) Tarjan (i,-1); }}int Get_ans () {int ans=n; Get_graph (); FIND_SCC (); for (int i=1;i<=n;i++) if (odd[i]) ans--; return ans;} void Solve () {printf ("%d\n", Get_ans ());} int main () {while (scanf ("%d%d", &n,&m)!=eof&&n&&m) {init (); Input (); Solve (); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Poj_2942_knights of the Round Table (double-connected component + binary-graph determination of points)