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)