POJ 2942--knights of the Round Table (double connected components of points)

Source: Internet
Author: User

It's a torture to do it ...

There were n Knights, and the Knights hated one another. Give the knight a relationship of mutual hatred. The Knights were going to the meeting, sitting in a circle, and the hated Knights could not be next to each other. The number of knights in a meeting cannot be less than three. Ask how many knights cannot hold a meeting.

Note: The meeting can be opened countless times, that is, a knight can actually open a number of meetings, so the total number of people may not be an odd number of meetings.

Figure out the diagram of a knight who does not hate each other, the connected knight can be next to each other. This way, if you have an odd circle, you can be sure that a circle of people can attend the meeting.

Properties: If some vertices within a two-connected component are in a singular circle (that is, the two connected components contain odd loops), then the other vertices of the two connected components are also in some odd circles

also know that the two-dimensional graph dyeing can judge the odd circle, so the strong connected component of each point to find the two-dimensional map dyeing OK. The key of this problem is to find the double connected component of the point.

/*********************************************memory:4752 kbtime:1141 mslanguage:g++result:accepted************* /#include <iostream> #include <cstdio> #include <cstring> #define PK puts ("KKK"); using namespace Std;const int N = 1005;const int M = N * n;struct Edge {int to, next;} Edge[m];int head    [N];int cnt_edge;void Add_edge (int u, int v) {edge[cnt_edge].to = v;    Edge[cnt_edge].next = Head[u];    Head[u] = cnt_edge++;    edge[cnt_edge].to = u;    Edge[cnt_edge].next = Head[v]; HEAD[V] = cnt_edge++;} int dfn[n], low[n], Idx;int stk[n], Top;int kind[n], Cnt;bool ok[n], in[n];int color[n];int mp[n][n];int n;bool dfs_color (    int u, int c) {color[u] = C;        for (int i = head[u]; i =-1; i = edge[i].next) {int v = edge[i].to;            if (In[v]) {if (!color[v] &&!dfs_color (V, 3-c)) return false;        else if (color[v] = = Color[u]) return false; }} return true; void Dfs (INT u, int pre) {Dfn[u] = low[u] = ++idx;    Stk[++top] = u;        for (int i = head[u]; i =-1; i = edge[i].next) {int v = edge[i].to;        if (v = = pre) continue;            if (!dfn[v]) {dfs (V, u);            Low[u] = min (Low[u], low[v]);                if (Low[v] >= dfn[u])//U is the cut point, the double connected component {memset (in, 0, sizeof in);                memset (color, 0, sizeof color);                CNT = 0;                int x;                int num = 0;                    do {x = stk[top--];                    kind[cnt++] = x;                    In[x] = true;                num++;                } while (x! = v);                if (Num <= 1) continue;                In[u] = true;                    if (!dfs_color (U, 1)) {Ok[u] = true;                while (cnt--) {ok[kind[cnt]] = true;}    }}} else Low[u] = min (Low[u], dfn[v]);   }}void Solve () { int ans = 0;    for (int i = 1; I <= n; ++i) Dfs (i,-1);    for (int i = 1; I <= n; ++i) if (ok[i]) ans++; printf ("%d\n", N-ans);}    void Init () {memset (head,-1, sizeof head);    memset (DFN, 0, sizeof DFN);    memset (OK, 0, sizeof OK);    Memset (MP, 0, sizeof MP); Cnt_edge = top = idx = 0;}    int main () {int m;    int u, v;        while (~SCANF ("%d%d", &n, &m)) {if (n = = 0 && m = = 0) break;        Init ();            for (int i = 1; I <= m; ++i) {scanf ("%d%d", &u, &v);        MP[U][V] = Mp[v][u] = 1; } for (int i = 1, i <= N; ++i) for (int j = 1; j <= N; ++j) if (i! = J &&!)        MP[I][J]) Add_edge (i, j);    Solve (); } return 0;}

  

POJ 2942--knights of the Round Table (double connected components of points)

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.