[CodeChef - GERALD07 ] Chef and Graph Queries

Posted SilverNebula

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[CodeChef - GERALD07 ] Chef and Graph Queries相关的知识,希望对你有一定的参考价值。

 

Read problems statements in Mandarin Chinese and Russian.

Problem Statement

Chef has a undirected graph G. This graph consists of N vertices and M edges. Each vertex of the graph has an unique index from 1 to N, also each edge of the graph has an unique index from 1 to M.

Also Chef has Q pairs of integers: LiRi (1 ≤ Li ≤ Ri ≤ M). For each pair LiRi, Chef wants to know: how many connected components will contain graph G if Chef erase all the edges from the graph, except the edges with indies X, where Li ≤ X ≤ Ri. Please, help Chef with these queries.

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains three integers NMQ. Each of the next M lines contains a pair of integers ViUi - the current edge of graph G. Each of the next Q lines contains a pair of integers LiRi - the current query.

Output

For each query of each test case print the required number of connected components.

Constraints

  • 1 ≤ T ≤ 1000.
  • 1 ≤ NMQ ≤ 200000.
  • 1 ≤ UiVi ≤ N.
  • 1 ≤ Li ≤ Ri ≤ M.
  • Sum of all values of N for test cases is not greater than 200000. Sum of all values of M for test cases is not greater than 200000. Sum of all values of Q for test cases is not greater than 200000.
  • Graph G can contain self-loops and multiple edges.

Example

Input:
2
3 5 4
1 3
1 2
2 1
3 2
2 2
2 3
1 5
5 5
1 2
1 1 1
1 1
1 1
Output:
2
1
3
1
1

 

破题坑了一晚上

题目大意?见上面超链接的中文题面。

 

树 LCT 动态维护生成树

将所有询问按右端点递增顺序排序,从1到m依次加边。

用LCT维护当前的生成树,生成树中保留的是编号尽量大的边。

同时用线段树维护生成树中保留了哪些边(权值线段树,存编号)。

查询时,在线段树上查询$[L,R]$范围内有多少条边,若查询结果为$ x $,则连通块数为$n-x$

注意有自环和重边,这使得维护生成树的时候不能一律删编号最靠前的边,而要先查询生成树中有没有重边。

细节蛮多的。

 

代码太丑,改天优化一下再放

 

以上是关于[CodeChef - GERALD07 ] Chef and Graph Queries的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ 3514: Codechef MARCH14 GERALD07加强版

bzoj3514Codechef MARCH14 GERALD07加强版

[CodeChef - GERALD07 ] Chef and Graph Queries

[BZOJ3514]CodeChef MARCH14 GERALD07加强版(LCT+主席树)

bzoj3514: Codechef MARCH14 GERALD07加强版

LCT+主席树BZOJ3514 Codechef MARCH14 GERALD07加强版