BZOJ1098 Office Building Biu (bfs+ list optimization)

Source: Internet
Author: User

Links: http://www.lydsy.com/JudgeOnline/problem.php?id=1098
Analysis: see note.

1 //complement graph connected block BFS + chain list optimization2#include <cstdio>3#include <queue>4#include <vector>5#include <algorithm>6 using namespacestd;7 8 structEdge {9     intto, NXT;Ten }; One  A structNode { -     intPre, NXT; - }; the  - Const intMAXM =2000000+5; - Const intMAXN =100000+5; -  + intN, M, CNT; - intHEAD[MAXN], VIS[MAXN], VI[MAXN]; +Edge E[MAXM <<1]; A Node L[MAXN]; atvector<int>ans; -  - voidAdd_edge (intUintv) { -E[++cnt].to = v; E[CNT].NXT = Head[u]; Head[u] =CNT; -e[++cnt].to = u; E[CNT].NXT = Head[v]; HEAD[V] =CNT; - } in  - voidDel_node (intp) { toL[L[P].PRE].NXT =l[p].nxt; +L[l[p].nxt].pre =L[p].pre; - } the  * voidBFs () { $queue<int> q;//maintain nodes to be expanded in the current fill graph connected blockPanax Notoginseng      while(l[0].nxt! =0) {//The current list is not empty, indicating that the node does not belong to any of the connected blocks -         intp = l[0].NXT;//enumerates a node that exists in a linked list and becomes the first node in a new connected block theDel_node (P);//Remove this node from the linked list +Q.push (P);//node Queue A         intsum =1;//current number of connected blocks is 1 the          while(!q.empty ()) {//continuously to the node in the current connected block to find the complement graph of the connected nodes are removed from the list and joined to the queue +p = Q.front (); Q.pop ();//eject a node in a connected block -              for(inti = head[p]; I i = e[i].nxt)//enumerates the edges of this node and marks $Vis[e[i].to] =1; $              for(inti = l[0].NXT; I i = l[i].nxt)//finds a node that is not currently part of any connected block, and if it is not marked, is a complement to the P node, the number of nodes of this connected block is increased and the Unicom node is added to the queue and removed from the list -                 if(!vis[i]) {sum++; Q.push (i); Del_node (i);} -              for(inti = head[p]; I i = e[i].nxt) vis[e[i].to] =0;//Recovery token, the flagged node may be a complement to the nodes in the current block (in the queue) that need to be expanded the         } -Ans.push_back (sum);//saves the number of nodes in the complete connected block in the arrayWuyi     } the } -  Wu intMain () { -scanf"%d%d", &n, &m); About      for(inti =1, u, v; I <= m; i++) {//Building Map $scanf"%d%d", &u, &v); - Add_edge (U, v); -     } -      for(inti =1; I <= N; i++) {//initializes the node 1 ~ N to a list (maintaining nodes that are not in any of the connected blocks), L[0].NXT points to the header node in the current list, and the list is empty if l[0].nxt = 0 AL[i].pre = i-1; +L[I].NXT = i +1; the} l[0].NXT =1; L[N].NXT =0; -BFS ();//A connecting block for the BFS $printf"%d\n", Ans.size ());//number of output connected blocks theSort (Ans.begin (), Ans.end ());//sort the number of connected blocks in ascending order the      for(inti =0; I < ans.size (); i++)//the number of connected blocks is output in ascending order theprintf"%d", Ans[i]); the     return 0; -}

BZOJ1098 Office Building Biu (bfs+ list optimization)

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.