Uvalive 3523 Knight of Round table Round Table Knight (undirected graph-point dual-connected component)

Source: Internet
Author: User

Because the knight who hates each other cannot be adjacent, the knight can be connected to the non-side, the meeting request is odd, the problem is to seek no more than the number of nodes on a simple odd circle.

Tarjan directly set previously written, the result is wrong, to pay special attention to the side to join the stack of time ... I don't understand the algorithm.

#include <bits/stdc++.h>using namespacestd;#defineBug (x) cout<< #x << ' = ' <<x<<endl;Const intMAXV = ++5;Const intMaxe = MAXV*MAXV;//Open Smallintdfn[maxv],low[maxv],bccno[maxv],dfs_clock,bcc_cnt;BOOLIscut[maxv];vector<int>BCC[MAXV];inthead[maxv],fro[maxe],to[maxe],nxt[maxe],ecnt;intEdges[maxe],top;voidAddedge (intUintv) {fro[ecnt]=u; TO[ECNT]=v; NXT[ECNT]=Head[u]; Head[u]= ecnt++;}voidTarjan (intUintFA) {Dfn[u]= Low[u] = + +Dfs_clock;  for(inti = Head[u]; ~i; i =Nxt[i]) {        intv =To[i]; if(!Dfn[v]) {edges[++top] =i;            Tarjan (V,i); Low[u]=min (low[v],low[u]); if(Low[u] >=Dfn[u]) {Iscut[u]=true; Bcc_cnt++; Bcc[bcc_cnt].clear ();//bcc starting from 1                intu,v;  Do{                    intE = edges[top--]; U= Fro[e], V =To[e]; if(Bccno[u]! = bcc_cnt) {Bcc[bcc_cnt].push_back (U); bccno[u]=bcc_cnt;} if(Bccno[v]! = bcc_cnt) {Bcc[bcc_cnt].push_back (V); bccno[v]=bcc_cnt;} } while(u!=u| | v!=v); }        }Else  if((i^1)! = FA && Dfn[v] < Dfn[u]) {Low[u] = min (low[u],dfn[v]); Edges[++top] = i; }//= = higher than ^ priority! The second condition is less, what's the reason for WA ?    }}voidFIND_BCC (intN) {Top=0; memset (DFN,0,sizeof(DFN)); memset (Iscut,0,sizeof(Iscut)); memset (Bccno,0,sizeof(BCCNO)); Dfs_clock= BCC_CNT =0;  for(inti =0; I < n; i++) {        if(!Dfn[i]) {Tarjan (i,-1); Iscut[i]= ~nxt[head[i]];// ! (Nxt[head[i]] = =-1);//Tree Ghent, if there is only one child node false        }    }}intCOLOR[MAXV];BOOLBipartite (intUintBB) {     for(inti = Head[u]; ~i; i =Nxt[i]) {        intv = to[i];if(Bccno[v]! = b)Continue; if(Color[v] = = Color[u])return false; if(!Color[v]) {Color[v]=3-Color[u]; if(!bipartite (V,B))return false; }    }    return true;}BOOLHATE[MAXV][MAXV];BOOLINODD[MAXV];intMain () {//freopen ("In.txt", "R", stdin);    intn,m;  while(~SCANF ("%d%d", &n,&m) && (n| |m)) {memset (hate,0,sizeof(hate));  while(m--){            intU,v; scanf"%d%d", &u,&v); U--, v--; HATE[U][V]= Hate[v][u] =true; } memset (Head,-1,sizeof(head)); ECNT =0;  for(inti =0; I < n; i++)         for(intj = i+1; J < N; J + +)if(!Hate[i][j])        {Addedge (i,j); Addedge (j,i);       } FIND_BCC (n); //bug (bcc_cnt);memset (Inodd,0,sizeof(inodd));  for(inti =1; I <= bcc_cnt; i++) {memset (color,0,sizeof(color));  for(intj =0; J < Bcc[i].size (); J + +) Bccno[bcc[i][j]] = i;//cut points, which belong to multiple connected components, so special handling            intU = bcc[i][0]; Color[u]=1; if(!bipartite (u,i)) {                 for(intj =0; J < Bcc[i].size (); J + +) Inodd[bcc[i][j]] =true; }        }        intAns =N;  for(intI=0; I < n; i++)if(Inodd[i]) ans--; printf ("%d\n", ans); }    return 0;}

Uvalive 3523 Knight of Round table Round Table Knight (undirected graph-point dual-connected component)

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.