"POJ 2942" Knights of the Round Table (dual-component + dye-awarded singular ring)

Source: Internet
Author: User
Tags fread

"POJ 2942" Knights of the Round Table (dual-component + dye-awarded singular ring)


Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 11661 Accepted: 3824

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.

Source

Central Europe 2005

The main topic: There are N Knights, M-hate relationship, each relationship a B represents a and B laosi,.

Now King Arthur wants to hold round tables regularly. The round table demanded a circle of knights. That is, every knight is bound to have about two neighboring knights. There is no direct animosity between the neighboring Knights.

The number of Knights holding round tables must be greater than 1. and must be an odd number.


Ask how many knights are required to hold the meeting normally.

There's a meaning I didn't read, that is, the knight who does not have to stay can participate in the same meeting, the meeting can be held in multiple sessions, only to leave the knight can participate in one of them.


This establishes the complement diagram, which is the Knight friendly relationship after the diagram. Points in different double-connected sub-graphs must not be able to attend the same round table. Because these points are either hatred, or just a friendly relationship, cannot be a ring.

This narrows the range to the same pair of connected components.

Here is a conclusion that, for a two-connected component, if there is a singular ring, then each point in the two-connected component must exist in at least one singular ring.

Since no matter what point has more than two to the path of the odd ring, two paths in the connection of the two points on the odd ring, the odd ring can be changed to even chain and odd chain so that the point to the two points between the parity of the number of points to make a choice to ensure that the odd ring must be formed.

The same, assuming that there is no singular ring, the points in all the two connected components do not exist in any singular ring.


The code is as follows:

#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm > #include <map> #include <set> #define LL long long#define Pr pair<int,int> #define FREAD () freopen (" In.in "," R ", stdin) #define FWRITE () freopen (" Out.out "," w ", stdout) using namespace std;const int INF = 0x3f3f3f3f;const int MSZ = 10000;const int mod = 1e9+7;const double eps = 1e-8;bool can[2333];bool in[2333];bool mp[2333][2333];bool vis[2333]; int col[2333];int dfn[2333],low[2333];stack <int> s;int n,tim;bool cal (int u) {//printf ("%d\n", u); queue <int > Q;q.push (U), memset (col,-1,sizeof (col)), col[u] = 1;while (!q.empty ()) {u = Q.front (); Q.pop (); for (int i = 1; I <= N; + +i) {if (U! = i && in[i] &&!mp[u][i]) {//printf ("%d->%d%d%d\n", u,i,col[u],col[i]); if (col[i] = = 1) { Col[i] = Col[u]^1;q.push (i);} else if (col[i] = = Col[u])return true;}}} return false;} void Tarjan (int u,int pre) {s.push (U);d fn[u] = low[u] = Tim++;vis[u] = 1;for (int i = 1; I <= n; ++i) {if (i = = U | | i = = P Re | | Mp[u][i]) continue;if (!vis[i]) {Tarjan (i,u); Low[u] = min (Low[u],low[i]), if (Low[i] >= Dfn[u]) {memset (in,0,sizeof ( IN)), while (S.top ()! = i) {in[s.top ()] = 1;s.pop ();} In[i] = 1;s.pop (); In[u] = 1;if (cal (U)) {for (int i = 1; I <= n; ++i) if (In[i]) can[i] = 1;}}} else Low[u] = min (Low[u],dfn[i]);}} int main () {//fread ();//fwrite (); int M,u,v;while (~SCANF ("%d%d", &n,&m) && (n+m)) {memset (mp,0,sizeof ( MP), while (m--) {scanf ("%d%d", &u,&v); Mp[u][v] = mp[v][u] = 1;}  memset (vis,0,sizeof (Vis)), memset (Can,0,sizeof (CAN)), Tim = 0;for (int i = 1; I <= n; ++i) if (!vis[i]) Tarjan (i,i); int ans = 0;for (int i = 1; I <= n; ++i) ans + = Can[i];p rintf ("%d\n", N-ans);} return 0;}





"POJ 2942" Knights of the Round Table (dual-component + dye-awarded singular ring)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.