https://oj.neu.edu.cn/problem/1387
Give a point n <= 100000, the side <= 1000000 of the graph, to find the number of Unicom block, and each block contains the points
Because the points are too large, fill the map will be dense map, and even to establish a fill map to O (n^2), can only dig up the nature of the Unicom block, BFS, complement the map, from the original map to find a complementary block:
In the original image is not directly adjacent points, in the complement map must belong to the same unicom block
Each point only belongs to one unicom block, so find a unicom block can be deleted after the link to all the points, the scale of the graph is reduced
This looks like this: 1. Prepare a set to place all unexplored points, and put 1~n in when initialized
2. Take a point from the collection into the queue (new Unicom block)
3. When the queue is not empty, take a point from the queue U and pop up, the original image in the direct connection with the point of the mark, traversing the collection, will be in the collection (that is, unexplored) and unmarked points (these points belong to this unicom block) and deleted from the collection, the mark deleted. Repeat execution until the queue is empty
4. Set is not idling 2, empty end
Considering the deletion operation and the time problem, the implementation of the collection is of course to select the linked list, the two-way list implemented by the array can be
Optimization has two: first, through the original any coins map of the Unicom block, and the second is to delete the searched points, so that each time the unmarked point is found than from 1 cycles to n better (constant optimization (error))
#include <cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<queue>using namespacestd;Const intMAXN = 1e5+ -, MAXM = 1e6+ -, INF =0x3f3f3f3f;structlnk{intVal; intpre, NXT;} LK[MAXN];structedge{intV, NXT;} E[MAXM*2];intHEAD[MAXN], tot, block_cnt, N, M;intADJ[MAXN], VIS[MAXN], NUM[MAXN];voidAddedge (intUintv) {E[tot]=(Edge) {V, head[u]}; Head[u]= tot++;}voidDeleintx) {Lk[lk[x].nxt].pre=Lk[x].pre; LK[LK[X].PRE].NXT=lk[x].nxt;}voidsrc () { for(inti =1; I <= N; i++) {Vis[i]= Adj[i] =0; } Queue<int>p; Block_cnt=0; while(lk[0].nxt! =-1){ //puts ("blk++");Q.push (lk[0].nxt); //printf ("Take%d\n", lk[0].nxt);vis[lk[lk[0].nxt].val] =1; Dele (lk[0].nxt); Block_cnt++; NUM[BLOCK_CNT]=1; while(!Q.empty ()) { intx =Q.front (); X=Lk[x].val; //printf ("%d\n", x);Q.pop (); for(inti = head[x]; ~i; i =e[i].nxt) { intv =e[i].v; ADJ[V]=1; } for(inti = lk[0].NXT; ~i; i =lk[i].nxt) { intW =Lk[i].val; if(!vis[w] &&!Adj[w]) {Q.push (w); VIS[W]=1; Dele (i); NUM[BLOCK_CNT]++; } } for(inti = head[x]; ~i; i =e[i].nxt) { intv =e[i].v; ADJ[V]=0; } } }}intMain () {intT; scanf ("%d", &t); while(t--) {scanf ("%d%d", &n, &m); for(inti =1; I <= N; i++) Head[i]= -1; Tot=0; while(m--){ intu, v; scanf ("%d%d", &u, &v); Addedge (U, v); Addedge (V, u); } for(inti =1; I <= N; i++) {Lk[i].val=i; Lk[i].pre= I1; LK[I].NXT= i+1; } lk[n].nxt= -1; lk[0].NXT =1; SRC (); Sort (num+1, num+block_cnt+1); printf ("%d\n", block_cnt); for(inti =1; I <= block_cnt; i++) {printf ("%d%c", num[i], i = = block_cnt?'\ n':' '); } } return 0;}/*3 5 7 1 2 1 3 1 4 1 5 2 3 2 4 2 5 6 9 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6 3 3 1 2 2 3 3 1*/
View Code
Link list plus BFS to complement diagram Unicom block