hdu5876 Sparse Graph (shortest short of complement graph)

Source: Internet
Author: User

Title Link: hdu5876 Sparse Graph

Started with a vector at the beginning of the tle, then there is no then.

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5#include <vector>6#include <Set>7 using namespacestd;8 9 Const intN =200001;Ten Const intM =50001; One Const intINF =0x3f3f3f3f; A intN, M; - intD[n]; - intHead[n]; the intCNT; - structedge{ -     intNEX; -     intV, W; + }g[m]; - voidAdd_edge (intUintVintW) { +G[CNT].V =v; AG[CNT].W =W; atG[cnt].nex =Head[u]; -Head[u] = cnt++; - } -  - voidBFsints) { -     intu, V, i; inqueue<int>Q; -     Set<int>a;//non-contiguous points to     Set<int>b;//points that are not expanded +     Set<int>:: iterator it; -      for(i =1; I <= N; ++i) the A.insert (i); * A.erase (s); $ Q.push (s);Panax Notoginseng      while(!Q.empty ()) { -U =Q.front (); the Q.pop (); +          for(i = head[u]; ~i; i =G[i].nex) { Av =g[i].v; the             if(!A.count (v)) +                 Continue; - B.insert (v); $ A.erase (v); $         } -          for(it = A.begin (); It! = A.end (); it++){ -D[*it] = D[u] +1; theQ.push (*it); -         }Wuyi A.swap (b); the b.clear (); -     } Wu } - intMain () { About     intT, I, J, X, Y, S, F; $scanf"%d", &t); -      while(t--){ -scanf"%d%d", &n, &m); -memset (d, INF,sizeof(d)); AMemset (Head,-1,sizeof(head)); +CNT =0; the          for(i =0; I < m; ++i) { -scanf"%d%d", &x, &y); $Add_edge (x, Y,1); theAdd_edge (y, X,1); the         } thescanf"%d", &s); theD[s] =0; - BFS (s); inf =0; the          for(i =1; I <= N; ++i) { the             if(i = = s)Continue; About             if(D[i] = =inf) theprintf"-1\n"); the             Else if(!f) { theprintf"%d", D[i]); +f =1; -             } the             ElseBayiprintf"%d", D[i]); the         } theprintf"\ n"); -     } -     return 0; the}
View Code

hdu5876 Sparse Graph (shortest short of complement graph)

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.