CF292D.Connected Components
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
We already know of the large corporation where Polycarpus works as a system administrator. The computer network there consists of n computers and m cables that connect some pairs of computers. In other words, the computer network can be represented as some non-directed graph with n nodes and m edges. Let's index the computers with integers from 1 to n, let's index the cables with integers from 1 to m.
Polycarpus was given an important task — check the reliability of his company's network. For that Polycarpus decided to carry out a series of k experiments on the computer network, where the i-th experiment goes as follows:
- Temporarily disconnect the cables with indexes from l__i to r__i, inclusive (the other cables remain connected).
- Count the number of connected components in the graph that is defining the computer network at that moment.
- Re-connect the disconnected cables with indexes from l__i to r__i (that is, restore the initial network).
Help Polycarpus carry out all experiments and for each print the number of connected components in the graph that defines the computer network through the given experiment. Isolated vertex should be counted as single component.
我们已经知道 Polycarpus 任职于一家大型公司,担任系统管理员。该公司计算机网络由 n 台计算机和 m 根连接某些计算机对的电缆组成。换言之,该计算机网络可表示为一个含 n 个节点与 m 条边的无向图。我们将计算机编号为 1 至 n 的整数,将电缆编号为 1 至 m 的整数。
Polycarpus 接到了一项重要任务——检验公司网络的可靠性。为此,他决定在计算机网络上执行一系列 k 次实验;其中第 i 次实验按如下步骤进行:
- 临时断开编号在区间 [li,ri] 内(含端点)的所有电缆(其余电缆保持连接状态);
- 统计此时定义计算机网络的图中的连通分量个数;
- 重新连接编号在区间 [li,ri] 内(含端点)的所有已断开电缆(即恢复初始网络)。
请帮助 Polycarpus 完成全部实验,并对每次实验输出此时定义计算机网络的图中的连通分量个数。孤立顶点应被计为一个连通分量。
输入格式
The first line contains two space-separated integers n, m (2 ≤ n ≤ 500; 1 ≤ m ≤ 104) — the number of computers and the number of cables, correspondingly.
The following m lines contain the cables' description. The i-th line contains space-separated pair of integers x__i, y__i (1 ≤ x__i, y__i ≤ n; x__i ≠ y__i) — the numbers of the computers that are connected by the i-th cable. Note that a pair of computers can be connected by multiple cables.
The next line contains integer k (1 ≤ k ≤ 2·104) — the number of experiments. Next k lines contain the experiments' descriptions. The i-th line contains space-separated integers l__i, r__i (1 ≤ l__i ≤ r__i ≤ m) — the numbers of the cables that Polycarpus disconnects during the i-th experiment.
第一行包含两个以空格分隔的整数 n、m(2 ≤ n ≤ 500;1 ≤ m ≤ 104),分别表示计算机的数量和电缆的数量。
接下来的 m 行描述了各条电缆。第 i 行包含两个以空格分隔的整数 xi、yi(1 ≤ xi, yi ≤ n;xi = yi),表示第 i 条电缆所连接的两台计算机的编号。注意:一对计算机之间可能有多条电缆。
下一行包含一个整数 k(1 ≤ k ≤ 2⋅104),表示实验的次数。接下来的 k 行描述了各次实验。第 i 行包含两个以空格分隔的整数 li、ri(1 ≤ li ≤ ri ≤ m),表示 Polycarpus 在第 i 次实验中要断开的电缆编号范围(即从第 li 条到第 ri 条电缆)。
输出格式
Print k numbers, the i-th number represents the number of connected components of the graph that defines the computer network during the i-th experiment.
输出 k 个数,其中第 i 个数表示第 i 次实验中所定义的计算机网络图的连通分量数量。
输入输出样例
输入#1
6 5 1 2 5 4 2 3 3 1 3 6 6 1 3 2 5 1 5 5 5 2 4 3 3
输出#1
4 5 6 3 4 2
输入解题思路,AI测评打分。不知道怎么写?