Killjee and cities
Practice
3.1 (17 votes)
Data structures
Disjoint set
Easy
Graphs
Problem
72% Success 2729 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Killjee lives in Codaland. Codaland has n cities and m bidirectional roads connecting these cities, but not all cities are connected. Now the President of Codaland has passed an order to build q new roads one by one connecting two different cities.

Killjee wonders how many new roads are needed to connect all the cities of Codaland after construction of each road as per President's plan.

Note:- There can be multiple number of roads between two same cities.

INPUT FORMAT

First line of input contain a single integer n, denoting number of cities.

Second line of input contains a single integer m, denoting number of current roads in Codaland.

Third line of input contains a single integer s, where \(s=2\).

m lines follow each having two space separated integers \(a,b\), denoting a road between city a and b.

Next line contains a single integer q, number of new roads to be built.

Next line of input contains a single integer s, where \(s=2\).

q lines follow each having two space separated integers \(a,b\), denoting a road between city a and b.

OUTPUT FORMAT

Output q space separated integers, \(i_{th}\) of which denotes number of roads needed to connect all the cities of Codaland after creation of first i roads.

INPUT CONSTRAINTS

\(1 \le n,q \le 10^6\)

\(0 \le m \le 10^6\)

\(1 \le a,b \le n; a!=b\)

All the roads in input are valid.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
42 votes
Tags:
Disjoint setEasyMath
Points:30
10 votes
Tags:
Data StructuresDisjoint setEasy
Points:30
79 votes
Tags:
ApprovedData StructuresDisjoint setEasyOpen