HDU 5333 Undirected Graph LCT + BIT

Source: Internet
Author: User
Tags cmath

HDU 5333 Undirected Graph LCT + BIT

 

Undirected Graph Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 184 Accepted Submission (s): 38



Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

There is an undirected graph N Vertices and M Edges. Then Yuta does Q Operations on this graph. Each operation is described by two integers L, R (1 ≤ L ≤ R ≤ n) And can be split into three steps:

1. Delete all the edges which have at least one vertice outside the range [L, R] .

2. Yuta wants you to tell him the number of connected component of the graph.

3. Restore the graph.

This task is too hard for Rikka to solve. Can you help her?
Input There are at most 100 testcases and there are at least 97 testcases N, m, q ≤ 1000 .

For each testcase, the first line contains three numbers N, m, q (n, q ≤ 105, m ≤ 2 × 105) .

Then M Lines follow. Each line contains two numbers Ui, vi (1 ≤ ui, vi ≤ 105) Which describe an edge of the graph.

Then Q Lines follows. Each line contains two numbers Li, Ri (1 ≤ L ≤ R ≤ n) Which describe an operation.
Output For each operation you need print a single line with a single number-the answer of this operation.
Sample Input
3 3 21 21 32 31 21 3

Sample Output
21

Author XJZX
Source 2015 Multi-University Training Contest 4:

 

An undirected graph (with self-ring and heavy edge) with m edges given n points q queries

For a query: [l, r]

Delete one or both of the edges, such as {u, v} u and v, that are not in the [l, r] range, and calculate the number of connected components of the residual graph.

Each query is independent of each other, that is, each query is deleted from the source image.

Ideas:

It makes sense to write a piece of code, and the card constant gets stuck ..

 

# Pragma comment (linker,/STACK: 1024000000) # include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Include
            
              Using namespace std; template
             
               Inline bool rd (T & ret) {char c; int sgn; if (c = getchar (), c = EOF) return 0; while (c! = '-' & (C <'0' | c> '9') c = getchar (); sgn = (c = '-')? -1: 1; ret = (c = '-')? 0: (c-'0'); while (c = getchar (), c> = '0' & c <= '9 ') ret = ret * 10 + (c-'0'); ret * = sgn; return 1;} template
              
                Inline void pt (T x) {if (x <0) {putchar ('-'); x =-x;} if (x> 9) pt (x/10); putchar (x % 10 + '0');} typedef long ll; typedef pair
               
                 Pii; const int N = 1e5 + 100; const int inf = 10000000; struct BIT {int c [N], maxn; void init (int n) {maxn = n; memset (c, 0, (10 + n) * sizeof (int);} int lowbit (int x) {return x &-x;} int sum (int x) {int ans = 0; while (x) ans + = c [x], x-= lowbit (x); return ans;} int query (int l, int r) {return sum (r)-sum (l-1);} void change (int x, int val) {while (x <= maxn) c [x] + = val, x + = lowbit (x) ;}} bit; struct Node * nu Ll; struct Node {Node * fa, * ch [2]; int val; int mi, min_id, id; bool rev; inline void put () {printf (% d: fa: % d [% d, % d] val: % d ma: % d, % d rev: % d, id, fa-> id, ch [0]-> id, ch [1]-> id, val, mi, min_id, rev);} inline void clear (int _ id) {fa = ch [0] = ch [1] = null; rev = 0; id = _ id; mi = inf; min_id = 0; val = 0 ;} inline void push_up () {if (this = null) return; if (val) {mi = min (id, min (ch [0]-> mi, ch [1]-> mi); if (id <= Min (ch [0]-> mi, ch [1]-> mi) min_id = id; else if (ch [0]-> mi <min (ch [1]-> mi, id) min_id = ch [0]-> min_id; else min_id = ch [1]-> min_id;} else {mi = min (ch [0]-> mi, ch [1]-> mi ); if (ch [0]-> mi <ch [1]-> mi) min_id = ch [0]-> min_id; else min_id = ch [1]-> min_id;} inline void push_down () {if (this = null) return; if (rev) {ch [0]-> flip (); ch [1]-> flip (); rev = 0 ;}} inline void setc (Node * p, int d) {ch [d] = p; p-> fa = this ;} Inline bool d () {return fa-> ch [1] = this;} inline bool isroot () {return fa = null | fa-> ch [0]! = This & fa-> ch [1]! = This;} inline void flip () {if (this = null) return; swap (ch [0], ch [1]); rev ^ = 1 ;} inline void go () {// update to this if (! Isroot () fa-> go (); push_down ();} inline void rot () {Node * f = fa, * ff = fa-> fa; int c = d (), cc = fa-> d (); f-> setc (ch [! C], c); this-> setc (f ,! C); if (ff-> ch [cc] = f) ff-> setc (this, cc); else this-> fa = ff; f-> push_up ();} inline Node * splay () {go (); while (! Isroot () {if (! Fa-> isroot () d () = fa-> d ()? Fa-> rot (): rot ();} push_up (); return this;} inline Node * access () {// After access, this Is A splay to the root, and this is already the root of this splay. for (Node * p = this, * q = null; p! = Null; q = p, p = p-> fa) {p-> splay ()-> setc (q, 1); p-> push_up ();} return splay ();} inline Node * find_root () {Node * x; for (x = access (); x-> push_down (), x-> ch [0]! = Null; x = x-> ch [0]); return x;} void make_root () {access ()-> flip ();} void cut () {// remove the subtree Of This vertex from access (); ch [0]-> fa = null; ch [0] = null; push_up ();} void cut (Node * x) {if (this = x | find_root ()! = X-> find_root () return; else {x-> make_root (); cut () ;}} void link (Node * x) {if (find_root () = x-> find_root () return; else {make_root (); fa = x ;}}; Node pool [N], * tail; node * node [N]; void init (int n) {tail = pool; null = tail ++; null-> clear (0); for (int I = 1; I <= n; I ++) {node [I] = tail ++; node [I]-> clear (I) ;}} void debug (Node * x) {if (x = null) return; x-> put (); debug (x-> ch [0]); debug (x-> ch [1]);} int n, m, q; s Truct BST {int f [N]; void init (int n) {for (int I = 1; I <= n; I ++) f [I] = I ;} int find (int x) {return x = f [x]? X: f [x] = find (f [x]);} void Union (int u, int v) {u = find (u); v = find (v ); if (u = v) return; if (u> v) swap (u, v); f [u] = v ;}} cha; void insert (int x, int y) {// puts (** =); for (int I = 1; I <= max (x, y); I ++) debug (node [I]), puts (); if (cha. find (x) = cha. find (y) {node [y]-> access (); // puts (**); for (int I = 1; I <= max (x, y); I ++) debug (node [I]), puts (); int id = node [y]-> min_id; if (y <= id) Return; // printf (change id: % d, id); bit. change (id,-1); node [id]-> val --; node [id]-> cut (node [x]);} else cha. union (x, y); // puts (---------); for (int I = 1; I <= x; I ++) debug (node [I]), puts (); bit. change (y, 1); node [y]-> make_root (); node [y]-> val ++; node [y]-> push_up (); node [y]-> fa = node [x]; // puts (@); for (int I = 1; I <= x; I ++) debug (node [I]), puts ();} int ans [N]; struct {struct Edge {int to, n Ex, id;} edge [N <1]; int head [N], edgenum; void init (int n) {memset (head,-1, (10 + n) * sizeof (int); edgenum = 0;} void add (int u, int v, int id = 0) {Edge E = {v, head [u], id}; edge [edgenum] = E; head [u] = edgenum ++;} E, Q; int main () {while (~ Scanf (% d, & n, & m, & q) {E. init (n); Q. init (n); cha. init (n); for (int I = 0, u, v; I <m; I ++) {rd (u), rd (v ); if (u = v) {I --, m --; continue;} if (u <v) E. add (v, u); else E. add (u, v) ;}for (int I = 0, u, v; I <q; I ++) {rd (u); rd (v); Q. add (v, u, I);} bit. init (n); for (int I = 1; I <= n; I ++) {for (int j = E. head [I]; ~ J; j = E. edge [j]. nex) insert (I, E. edge [j]. to); for (int j = Q. head [I]; ~ J; j = Q. edge [j]. nex) ans [Q. edge [j]. id] = n-bit. query (Q. edge [j]. to, I) ;}for (int I = 0; I <q; I ++) pt (ans [I]), puts ();} return 0 ;} /* 7 9 11 21 31 51 64 74 62 76 24 32 7ans: 3 */
               
              
             
            
           
          
         
        
       
     
    
   
  
 


 

 

Related Article

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.