Two methods of Codeforces 510B non-circular graph finding (search and set)

Source: Internet
Author: User
Tags acos bitset cmath

Topic Connection: Http://codeforces.com/problemset/problem/510/B

Solution:

    1. Dfs
      Each time the value of the parent node is recorded and passed down, judge the new reach node:
      (1) did not walk → continue to search;
      (2) Walk && Not parent node (for the number of steps also to >=4) → Find the ring;
    2. and check Set
      Each node is mapped into a i*m+j from the starting point, respectively, and then add it to the right side of the node, if it is found that a node is already in the collection, then the ring has been found.

      • Dfs:
#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>//////////////////////#include <iostream>#include <algorithm>#include <string>#include <iterator>#include <sstream>#include <functional>#include <numeric>///////////////////////#include <vector>#include <map>#include <stack>#include <queue>#include <set>#include <bitset>#include <list>using namespace STD;#define LCH (x) ((x) << 1)#define RCH (x) ((x) <<1|1)#define DAD (x) ((x) >>1)#define LOWBIT (x) ((x) & ( -X))typedef  Long Long intLL;Const intINF =0x4f4f4f4f;Const DoubleEPS =1e-6;Const Long DoublePI =ACOs(0.0) *2.0;Const intN = -+Ten;intN,m;CharMaze[n][n];BOOLVis[n][n];Const intDx[] = {0,1,0,-1};Const intDy[] = {1,0,-1,0};BOOLOkBOOLDfsintXintYintPxintPyintCNT);intMain () {//freopen ("In.txt", "R", stdin);     while(scanf("%d%d%*c", &n,&m) = =2)    {memset(Vis,0,sizeof(VIS)); for(inti =0; I < n;        i++) gets (Maze[i]); OK =0; for(inti =0; I < n; i++) for(intj =0; J < M; J + +)if(!vis[i][j])if(Dfs (i,j,-1,-1,1))                      {puts("Yes");return 0; }puts("No"); }return 0;}BOOLDfsintXintYintPxintPyintCNT) {Vis[x][y] =1; for(inti =0; I <4; i++) {intNX = x +dx[i], NY = y + dy[i];if(0<= NX && NX < n &&0<= NY && NY < m && Maze[nx][ny] = Maze[x][y]) {if(!vis[nx][ny]) DFS (nx,ny,x,y,cnt+1);Else if((NX! = PX | | NY! = PY) && cnt>=4) OK =1; }    }returnOK;}
    • And check the set:
#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>//////////////////////#include <iostream>#include <algorithm>#include <string>#include <iterator>#include <sstream>#include <functional>#include <numeric>///////////////////////#include <vector>#include <map>#include <stack>#include <queue>#include <set>#include <bitset>#include <list>using namespace STD;#define LCH (x) ((x) << 1)#define RCH (x) ((x) <<1|1)#define DAD (x) ((x) >>1)#define LOWBIT (x) ((x) & ( -X))typedef  Long Long intLL;Const intINF =0x4f4f4f4f;Const DoubleEPS =1e-6;Const Long DoublePI =ACOs(0.0) *2.0;Const intN =10000+Ten;intPre[n];int_rank[n];CharMaze[n][n];intFindintx);intJoinintXinty);voidInivoid);intSame (intXinty);intMain () {ini ();intN,m;scanf("%d%d%*c", &n,&m); for(inti =0; I < n; i++) gets (Maze[i]); for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) {if(Maze[i][j] = = maze[i+1][J] && i <n-1)            {if(Same (I*m+j, (i+1) (*m+j)) {puts("Yes");return 0; }ElseJoin (I*m+j, (i+1) *m+j); }if(Maze[i][j] = = maze[i][j+1] && J < m1)            {if(Same (i*m+j,i*m+j+1))   {puts("Yes");return 0; }ElseJoin (i*m+j,i*m+j+1); }        }    }puts("No");return 0;}voidInivoid){intI for(i =0; i < N; i++) Pre[i] = i,_rank[i]=0;}intJoinintXintY) {intFX = Find (x), FY = find (y);if(FX = = FY)return 0;if(_rank[fx] > _rank[fy])    {Pre[fy] = FX; }Else{PRE[FX] = FY;if(_rank[fx] = = _rank[fy]) _rank[fy]++; }}//simplified notation, using recursion//int find (int x) {return x==pre[x]?x:pre[x]=find (pre[x]);}intFind (intx) {intR = x; while(r! = Pre[r]) R = Pre[r];inti = x,j; while(r! = Pre[i])        {j = pre[i];        Pre[i] = r;    i = j; }returnR;}intSame (intXintY) {returnFind (x) = = Find (y);}

Copyright NOTICE: This article for Bo Master original article, allow non-commercial reprint, reprint must note name author (CSDN tt2767) and this blog link: http://blog.csdn.net/tt2767.

Two methods of Codeforces 510B non-circular graph finding (search and set)

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.