#SRD0000086. Đầu bếp và các truy vấn đồ thị (Chef and Graph Queries)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đầ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 vertices and edges numbered from 1 to in input order, self-loops and parallel edges are allowed. For each interval , form a graph on all vertices but keep only edges whose indices belong to the inclusive range . Count its connected components, including isolated vertices. Queries are independent and do not modify the original list of edges.
Input
The first line contains . The next lines contain endpoints of undirected edges. Each of the last lines contains with .
Output
Output the number of connected components in the graph containing only edges , one result per query.
Subtasks
-
Subtask 1 (10 points): ; ; . ; ; and duplicate edges are allowed; .
-
Subtask 2 (20 points): ; ; . ; ; and duplicate edges are allowed; .
-
Subtask 3 (30 points): ; ; . ; ; and duplicate edges are allowed; .
-
Subtask 4 (40 points): ; ; . ; ; and duplicate edges are allowed; .
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 , both edges join vertices 1 and 2, leaving vertex 3 isolated: 2 components. For , all three vertices are connected: 1 component. For , only a self-loop on vertex 2 remains, so there are 3 components. For , all vertices are connected: 1 component.