POJ 2942 Knights of the Round Table (point dual connected components, even graphs)

Source: Internet
Author: User

Test instructions: A number of knights to meet, 3 people and above to gather a table, some of whom have hated each other, certainly do not sit in the same table adjacent position, and a table can only odd number of people to founding. Give a lot of people to hate each other diagram, ask how many persons will not open the meeting (note: The conference does not require simultaneous, a person open more will not conflict)?

Analysis:

To the mutual dislike of the diagram, then turn to each other like it, sweep again, if not mutual dislike to think of each other like, Matrix to adjacency table first.

Two points with a side connection represent the ability to sit in a piece. Then find a circle out, in the circle of the point there are odd number of people can certainly make up 1 tables. Circle? That's the simple ring, which is the same as the definition of a point's two connected components: each point can be in one or more simple loops at the same time. So, as long as there is a ring, they can gather a table (each ring open a table, with 1 people to participate in multi-table does not conflict).

But how to solve the odd problem? If a point is a double-connected component is a even graph (that is, a bipartite graph), then there must be even rings. Think, the diagram is double connected, then there must be a simple ring, 1 simple rings if the odd number, coloring method must be a conflict. Then the problem can be solved by using the even graph to decide.

Realize:

(1) Dislike each other diagram to each other like diagrams.

(2) to find a double connected component, and to classify the points within the two connected components of the same point. (Note that the diagram may not be connected)

(3) Black and white coloring decision even graph, non-even graph left, even graph ignored. 3 of the points of the point of the two connected components are also ignored.

(4) As long as 1 points can be in either non-even graph, mark it as "can meet."

(5) Statistics on which people can not open the meeting. (It must be those 3 people below, even a few people want to sit a piece of)

1 //#include <bits/stdc++.h>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <stack>6#include <vector>7 #defineLL Long Long8 #definePII pair<int,int>9 using namespacestd;Ten Const intn= ++5; One Const intinf=0x7f7f7f7f; A intN, M, bcc_cnt, Dfn_clock;//number of points of two connected components - intG[n][n], dfn[n], low[n], bcc_no[n], col[n], flag[n]; -  thestack<pair<int,int> >Stac; -vector<int>Bcc[n], vect[n]; -  -  + voidDFS (intXintFar ) - { +dfn[x]=low[x]=++Dfn_clock; A      for(intI=0; I<vect[x].size (); i++) at     { -         intt=Vect[x][i]; -         if(!Dfn[t]) -         { - Stac.push (Make_pair (x,t)); - DFS (t,x); inlow[x]=min (low[x],low[t]); -             if(low[t]>=Dfn[x]) to             { +bcc[++bcc_cnt].clear (); -                  while(1) the                 { *                     intA=Stac.top (). First; $                     intb=Stac.top (). Second;Panax Notoginseng Stac.pop (); -                     if(bcc_no[a]!=bcc_cnt) the                     { +bcc_no[a]=bcc_cnt; A Bcc[bcc_cnt].push_back (a); the                     } +                     if(bcc_no[b]!=bcc_cnt) -                     { $bcc_no[b]=bcc_cnt; $ Bcc[bcc_cnt].push_back (b); -                     } -                     if(A==x && b==t) Break; the                 } -             }Wuyi         } the         Else if(Dfn[t]<dfn[x] && T!=far)//in particular, it is important to note that the phrase "dfn[t]<dfn[x" is necessary, especially when finding a double-connected component. Otherwise it may be messy.  -         { Wu Stac.push (Make_pair (x,t)); -low[x]=min (low[x],dfn[t]); About         } $     } - } -  - voidFIND_BCC ()//find the points of the two connected components, placed in the BCC A { +Bcc_cnt= dfn_clock=0; thememset (Low,0,sizeof(Low)); -memset (Bcc_no,0,sizeof(Bcc_no)); $memset (DFN,0,sizeof(DFN)); the      for(intI=1; i<=n; i++) the         if(!dfn[i]) DFS (i,-1); the } the  - intColorintNum//Judging if even graphs, even graphs do not contain odd circles in { thecol[bcc[num][0]]=2; thedeque<int> Que;que.push_back (bcc[num][0]); About      while(!que.empty ())//Wide Search Coloring the     { the         intx=Que.front (); Que.pop_front (); the          for(intI=0; I<bcc[num].size (); i++) +         { -             intt=Bcc[num][i]; the             if(X!=t&&!g[x][t])//as long as there are sidesBayi             { the                 if(Col[t]==col[x])return false;//colors are the same, non-even graphs the                 if(!col[t])//no dye, just go in. -                 { -col[t]=3-Col[x]; the Que.push_back (t); the                 } the  the             } -         } the     } the     return true; the }94  the intcolor_it () the { thememset (Flag,0,sizeof(flag));98      for(intI=1; i<=bcc_cnt; i++) About     { -memset (Col,0,sizeof(col));//0 Each time because it might be a bit of a two-connected component101         if(Bcc[i].size () <3)Continue;//not enough people to meet102         if(Color (i) = =false)//Not even graphs103              for(intj=0; J<bcc[i].size (); J + +)//These people can have a meeting, mark.104flag[bcc[i][j]]=1; the     }106 107     intCnt=0;108      for(intI=1; i<=n; i++)//Count who can't hold a meeting109         if(!flag[i]) cnt++; the     returnCNT;111 } the 113 intMain () the { theFreopen ("Input.txt","R", stdin); the     intA, B;117      while(SCANF ("%d%d", &n,&m), n+m)118     {119          for(intI=1; i<=n; i++) vect[i].clear (); -Memset (G,0,sizeof(g));121 122          for(intI=0; i<m; i++)123         {124scanf"%d%d",&a,&b); theg[a][b]=g[b][a]=1;126         }127          for(intI=1; i<=n; i++)//Turn adjacency table -              for(intj=i+1; j<=n; J + +)129                 if(!G[i][j]) Vect[i].push_back (j), Vect[j].push_back (i); the 131 find_bcc (); theprintf"%d\n", Color_it ());133 134     }135 136     return 0;137}
AC Code

POJ 2942 Knights of the Round Table (point dual connected components, even graphs)

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.