POJ2942 Knights of the Round Table point dual connected components, inverse graph, odd ring

Source: Internet
Author: User

Topic Links:

poj2942




Test instructions

There are n individuals, you can open multiple round tables

Of these n individuals, M has a hostile relationship with one another, and two people who are hostile to each other sit in the adjacent position.

And the number of each round table is only for odd books.

Ask how many people are unable to attend




Problem Solving Ideas:

First, the composition, all the hatred of the relationship as a side, and finally take the image of the inverse diagram of the obtained,

So the connected edges on the graph represent the relationships that can sit adjacent to each other.

Then it is to find the odd circle, the first is to find the map of the ring (point two connected components)

This ring is the condition of the odd ring: not the dichotomy chart | | Some of the points in this ring belong to other odd rings.

This judgment can be judged by the DFS black-and-white staining of the found ring

The sum of the last not in the odd ring is the answer


As for why it is the point of the two connected components rather than the side of the two connected components can try this set of data:

6 8
1 4
1 5
1 6
2 4
2 5
2 6
3 6
4 5
0 0

The resulting inverse diagram is this:

If the electric double connected component (split cut point) is divided into (1 2 3) and (3 4 5 6) two parts

And if the side of the two connected components (cut edge) is only (1 2 3 4 5 6) part of the


Code:

#include <iostream> #include <cstdio> #include <cstring> #define MAXN 1050using namespace Std;struct node{int to,next;} Edge[2000500];int head[maxn],ss;int map[maxn][maxn];int in[maxn],odd[maxn],temp[maxn];int color[maxn];int DFN[MAXN]    , Low[maxn],num;int insta[maxn],sta[maxn],top;int n;void init () {memset (dfn,0,sizeof (DFN));    memset (head,-1,sizeof (head));    memset (insta,0,sizeof (Insta));    memset (map,0,sizeof (map));    memset (odd,0,sizeof (odd)); Top=num=ss=0;}    void Addedge (int a,int b) {edge[ss]= (node) {B,head[a]}; head[a]=ss++;}    int dfs (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]) continue;        if (color[v]==c) return 1;        else if (Color[v]) continue;    else if (Dfs (V,3-C)) return 1; } return 0;}    void Tarjan (int u,int pre) {dfn[u]=low[u]=++num;    Insta[u]=1;    Sta[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]) {Tarjan (v,u);            Low[u]=min (Low[u],low[v]);                if (Dfn[u]<=low[v]) {int s=0,d=-1;                memset (In,0,sizeof (in));                    while (D!=V)//Note is a V-point two-connected to another way of writing, in short, note that the cut point can belong to multiple connected components {D=sta[--top];                    In[d]=1;       insta[d]=0;                Can not let u=0 temp[s++]=d;                } in[u]=1;                memset (color,0,sizeof (color));                    if (Dfs (u,1))//black and white staining determination {odd[u]=1;                while (s!=0) odd[temp[--s]]=1;    }}} and else if (Insta[v]) low[u]=min (Low[u],dfn[v]);    }}void solve () {for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan (i,-1);    int ans=0; for (int i=1;i<=n;i++) if (!odd[i]) ans++; printf ("%d\n", ans);}    int main () {//Freopen ("In.txt", "R", stdin);    int m,a,b;        while (scanf ("%d%d", &n,&m) && (m+n)) {init ();            while (m--) {scanf ("%d%d", &a,&b);        Map[a][b]=map[b][a]=1;                    } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (I!=j&&!map[i][j])//Take inverse diagram        Addedge (I,J);    Solve (); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ2942 Knights of the Round Table point dual connected components, inverse graph, odd 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.