HDU 5867 Sparse Graph (2016 Dalian network race I bfs+ complement map)

Source: Internet
Author: User

Test instructions: give you n point M edge to form an no-map, ask you to find the point of the map on the complement to each point distance of the minimum value, each edge distance of 1

Complement map: Complete graph minus Original

Full picture: Every two points connected to the graph

In fact, is a skilled bfs, we can see that although a lot of points but few sides, the use of vectors to save each point in the original image can reach the other points, and then use BFS to find the starting point can be at the other points (each distance is 1, so the earlier the distance is shorter), Then update the starting point to continue looking for: we need to use an array to record some points that cannot be reached at this point (that is, the point at which the vector is the starting point), but every other point is timed out by the starting point, so we are using a queue to store the points that have not yet been reached. Just looking for these points every time can be reached by the beginning of this time.

#include <Set>#include<map>#include<queue>#include<stack>#include<cmath>#include<vector>#include<string>#include<cstdio>#include<cstring>#include<stdlib.h>#include<iostream>#include<algorithm>using namespacestd;#defineEPS 1E-8/*Note that there may be output -0.000*/#defineSGN (x) (X<-eps -1:x<eps? 0:1)//X is a comparison of two floating-point numbers, note the return integer#defineCvs (x) (x > 0.0 x+eps:x-eps)//floating point Conversion#defineZero (x) (((x) >0? ( x):-(x)) <eps)//determine if it equals 0#defineMul (A, B) (a<<b)#defineDir (A, B) (a>>b)typedefLong Longll;typedef unsignedLong Longull;Const intinf=1<< -;Const DoublePi=acos (-1.0);Const intmod=1e9+7;Const intmax=200010; Vector<int> Edge[max];//edges that can be connectedintVis[max];//mark at this time can walkintAns[max];structnode{intNpoi,step;}; Queue<node>Bque;queue<int> nque;//Save the points that need to govoidInit (intN) {     while(!bque.empty ()) Bque.pop ();  while(!nque.empty ()) Nque.pop ();  for(intI=1; i<=n; ++i) {ans[i]=-1;        Edge[i].clear (); Vis[i]=0; }    return;}voidMark (intPintF//Mark these points can go{    intlen=edge[p].size ();  for(intI=0; i<len; ++i) vis[edge[p][i]=F; return;}voidBfs (intNintS//the BFS of the complement diagram{    intlen=n-1;    Node tem,hh; Tem.step=0, tem.npoi=s;    Bque.push (TEM); Vis[s]=1;  while(!Bque.empty ()) {tem=Bque.front ();        Bque.pop (); Mark (Tem.npoi,1);//according to the original image of the edge mark cannot go        inttmp=LEN,TMP2;  while(tmp--) {TMP2=nque.front ();//these points are not coming.Nque.pop (); if(!VIS[TMP2]) {Len--; Hh.step=tem.step+1; Hh.npoi=TMP2;                Bque.push (HH); ANS[TMP2]=Hh.step; }            ElseNque.push (TMP2); if(!len)return; } Mark (Tem.npoi,0); }    return;}intMain () {intt,n,m; intu,v,s; scanf ("%d",&t);  while(t--) {scanf ("%d%d",&n,&m);        Init (n);  for(intI=0; i<m; ++i) {scanf ("%d%d",&u,&v);            Edge[u].push_back (v);        Edge[v].push_back (U); } scanf ("%d",&s);  for(intI=1; i<=n; ++i)if(i!=s) nque.push (i);        Bfs (n,s); intlen=n-1;  for(intI=1; i<=n; ++i) {if(i!=s) {--Len; if(len) printf ("%d", Ans[i]); Elseprintf ("%d\n", Ans[i]); }        }    }    return 0;}

HDU 5867 Sparse Graph (2016 Dalian network race I bfs+ complement map)

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.