设为首页 加入收藏

TOP

HDU 5333 Undirected Graph LCT+BIT(一)
2015-11-21 00:56:20 来源: 作者: 【 】 浏览:3
Tags: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 with 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 with 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 2
1 2
1 3
2 3
1 2
1 3

Sample Output
2
1

Author XJZX
Source 2015 Multi-University Training Contest 4 题意:

?

给定n个点m条边的无向图(有自环有重边) q个询问

对于某个询问 query : [l,r]

把所有的边 形如 {u,v} u,v其中一个或者两个都不在区间[l,r]上的 都删除,求此时残余图的连通分量数。

每个询问都是互相独立的,也就是每个询问都是从原图删除而来。

思路:\

感觉很有道理的样子,写了一发,卡常数卡死人了。。

?

#pragma comment(linker, /STACK:1024000000)
#include 
  
   
#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 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 *null; 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() {//从链头开始更新到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 th
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ - 3281 Dining (ISAP EK Din.. 下一篇HDOJ 题目3564 Another LIS(线段..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: