PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000086. Đầu bếp và các truy vấn đồ thị (Chef and Graph Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đầu bếp và các truy vấn đồ thị (Chef and Graph Queries)

Chef and Graph Queries

Source: CodeChef

Version: Phuoc Hung OJ Extended

Problem Statement

Given an undirected graph with NN vertices and MM edges numbered from 1 to MM in input order, self-loops and parallel edges are allowed. For each interval [l,r][l,r], form a graph on all NN vertices but keep only edges whose indices belong to the inclusive range [l,r][l,r]. Count its connected components, including isolated vertices. Queries are independent and do not modify the original list of edges.

Input

The first line contains N,M,QN,M,Q. The next MM lines contain endpoints u,vu,v of undirected edges. Each of the last QQ lines contains l,rl,r with 1≤l≤r≤M1\le l\le r\le M.

Output

Output the number of connected components in the graph containing only edges [l,r][l,r], one result per query.

Subtasks

  • Subtask 1 (10 points): 1≤N≤401\le N\le 40; 1≤M≤601\le M\le 60; 1≤Q≤601\le Q\le 60. 1≤N,M,Q≤2000001\le N,M,Q\le 200000; 1≤u,v≤N1\le u,v\le N; u=vu=v and duplicate edges are allowed; 1≤l≤r≤M1\le l\le r\le M.

  • Subtask 2 (20 points): 1≤N≤20001\le N\le 2000; 1≤M≤20001\le M\le 2000; 1≤Q≤20001\le Q\le 2000. 1≤N,M,Q≤2000001\le N,M,Q\le 200000; 1≤u,v≤N1\le u,v\le N; u=vu=v and duplicate edges are allowed; 1≤l≤r≤M1\le l\le r\le M.

  • Subtask 3 (30 points): 1≤N≤300001\le N\le 30000; 1≤M≤300001\le M\le 30000; 1≤Q≤300001\le Q\le 30000. 1≤N,M,Q≤2000001\le N,M,Q\le 200000; 1≤u,v≤N1\le u,v\le N; u=vu=v and duplicate edges are allowed; 1≤l≤r≤M1\le l\le r\le M.

  • Subtask 4 (40 points): 1≤N≤2000001\le N\le 200000; 1≤M≤2000001\le M\le 200000; 1≤Q≤2000001\le Q\le 200000. 1≤N,M,Q≤2000001\le N,M,Q\le 200000; 1≤u,v≤N1\le u,v\le N; u=vu=v and duplicate edges are allowed; 1≤l≤r≤M1\le l\le r\le M.

Examples

Input

3 5 4
1 3
1 2
2 1
3 2
2 2
2 3
1 5
5 5
1 2

Output

2
1
3
1

Explanation

For [2,3][2,3], both edges join vertices 1 and 2, leaving vertex 3 isolated: 2 components. For [1,5][1,5], all three vertices are connected: 1 component. For [5,5][5,5], only a self-loop on vertex 2 remains, so there are 3 components. For [1,2][1,2], all vertices are connected: 1 component.