Rokua P1141//bfs to find the sub-connected block size of undirected graphs (multiple queries)

Source: Internet
Author: User
Tags cmath

http://www.luogu.org/problem/show?pid=1141

To ask questions, the size of the sub-connected block of undirected graphs is sought.

Direct BFS, read a search one, over 60;

100% points 1,000,000 points, 100,000 queries, is obviously memory.

I was weak, and I started out. The number group records the coordinates of the first point of each point belonging to the connected block, and then writes a bunch of them.

Later asked a great God, like the mist to see the sky. The "name" of the connected block is recorded with CNT.

Learned this trick, does not mean too.

I still read a point, if the output is accessed, the DFS is not accessed, and then each DFS is cycled once, assigning values to all the points within the connected block. The code is as follows

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <string>5#include <cstring>6#include <queue>7#include <cstdlib>8#include <algorithm>9 #defineFor1 (i,n,m) for (int i= (n); i<= (m); i++)Ten #defineFor2 (i,n,m) for (int i= (n); i>= (m); i--) One using namespacestd; A intN,cnt=1, m,a[1005][1005],ans[1005][1005],v[1005][1005],f1[4]={0,0,1,-1},f2[4]={-1,1,0,0}; - structnode - { the     intx, y; - }; -Queue <node>Q; -  + voidBFsintXinty) - { +      A Q.push ({x, y}); atv[x][y]=CNT; -     intans1=0; -      while(!q.empty ()) -     {     -Node e=Q.front (); - Q.pop ();  inans1++; -For1 (I,0,3) to         { +             intx1=e.x+f1[i],y1=e.y+F2[i]; -             if(!v[x1][y1]&&a[x1][y1]==3-a[e.x][e.y]&&x1>0&&y1>0&&x1<=n&&y1<=N) the             { *              $v[x1][y1]=CNT;Panax Notoginseng Q.push ({x1,y1}); -             } the         } +     } AFor1 (I,1, N) theFor1 (J,1, N) +     { -         if(v[i][j]==CNT) $ans[i][j]=ans1; $              -     } -cnt++; the } - Wuyi intMain () the { -Cin>>n>>m; WuFor1 (I,1, N) -For1 (J,1, N) About     { $         CharCh=GetChar (); -          while(ch!='1'&&ch!='0') -Ch=GetChar (); -         if(ch=='1') Aa[i][j]=1; +         if(ch=='0') thea[i][j]=2; -     } $For1 (I,1, M) the     { the         intx, y; theCin>>x>>y; the         if(!V[x][y]) - BFS (x, y); incout<<ans[x][y]<<Endl;; the     } the}
Timeout Code

I'm so stupid, I didn't use CNT at all.

After the timeout, ask the great God.

He said the complexity should be O (n^2+m)

I'm a crazy face.

Take a closer look at his code and feel like he's got a bubble in his brain.

Then opened a t[I] array as a bucket, record the size of the Unicom block belonging to I.

Over DFS, ask for all connected blocks, and group them, respectively, to record the size.

Final output ...

The code is as follows

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <string>5#include <cstring>6#include <queue>7#include <cstdlib>8#include <algorithm>9 #defineFor1 (i,n,m) for (int i= (n); i<= (m); i++)Ten #defineFor2 (i,n,m) for (int i= (n); i>= (m); i--) One using namespacestd; A intN,cnt=1, m,a[1005][1005],ans[1005][1005],f1[4]={0,0,1,-1},f2[4]={-1,1,0,0}; - intt[1005000];//use a bucket to record each group of ANS - intv[1005][1005];//grouping, assigning the same value to points within the same connected block, using CNT to count to denote the "first name" of the connected block the structnode - { -     intx, y; - }; +Queue <node>Q; -  + intBFsintXinty) A { at      - Q.push ({x, y}); -v[x][y]=CNT; -     intans1=0; -      while(!q.empty ()) -     {     inNode e=Q.front (); - Q.pop ();  toans1++; +For1 (I,0,3) -         { the             intx1=e.x+f1[i],y1=e.y+F2[i]; *             if(!v[x1][y1]&&a[x1][y1]==3-a[e.x][e.y]&&x1>0&&y1>0&&x1<=n&&y1<=N) $             {Panax Notoginseng              -v[x1][y1]=CNT; the Q.push ({x1,y1}); +             } A         } the     } +cnt++; -     returnans1; $ } $  - intMain () - { theCin>>n>>m; -For1 (I,1, N)WuyiFor1 (J,1, N) the     { -         CharCh=GetChar (); Wu          while(ch!='1'&&ch!='0') -Ch=GetChar (); About         if(ch=='1') $a[i][j]=1; -         if(ch=='0') -a[i][j]=2; -     } AFor1 (I,1, N) +For1 (J,1, N) the         { -             if(!V[i][j]) $             { the                 inttmp=BFS (i,j); thet[cnt-1]=tmp; the         }} theFor1 (I,1, M) -     { in         intx, y; theCin>>x>>y; the          Aboutcout<<t[v[x][y]]<<Endl; the     } the}
View Code

Rokua P1141//bfs to find the sub-connected block size of undirected graphs (multiple queries)

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.