Ural 1671 Anansi's Cobweb

来源:互联网 发布:java连接cassandra 编辑:程序博客网 时间:2024/06/12 00:54

1671. Anansi's Cobweb

Time limit: 1.0 second
Memory limit: 64 MB
Usatiy-Polosatiy XIII decided to destroy Anansi's home — his cobweb.The cobweb consists ofN nodes, some of which are connected by threads.Let us say that two nodes belong to the same piece if it is possible to get from one node to the other by threads. Usatiy-Polosatiy has already decided which threads and in what order he would tear and now wants to know the number of pieces in cobweb after each of his actions.

Input

The first line contains integers N and M — the number of nodes and threads in the cobweb, respectively(2 ≤ N ≤ 100000; 1 ≤ M ≤ 100000). Each of the next M lines contains two different integers — the 1-based indices of nodes connected by current thread. The threads are numbered from 1 toM in the order of description. Next line contains an integerQ which denotes the quantity of threads Usatiy-Polosatiy wants to tear(1 ≤QM). The last line contains numbers of these threads — different integers separated by spaces.

Output

Output Q integers — the number of pieces in Anansi's cobweb after each of Usatiy-Polosatiy's action. Separate numbers with single spaces.

Samples

inputoutput
4 41 22 31 33 432 4 3
1 2 3
3 11 211
3
Problem Author: Dmitry Poletaev (prepared by Alex Samsonov)
Problem Source: Ural SU Contest. Petrozavodsk Summer Session, August 2008

题意:

给n个点,m条边,q次询问

每次询问都删掉第x条边,(x是输入的),问剩下几个连通分量

题解:

倒着看,先用并查集维护所有没删的边,然后从删的最后一条边开始,一条边一条边的加,加之前记录剩几个连通分量,具体实现看代码.

c++:

#include <set>#include <map>#include <list>#include <cmath>#include <ctime>#include <deque>#include <queue>#include <stack>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <cassert>#include <cstdlib>#include <cstring>#include <sstream>#include <iostream>#include <algorithm>#define pi acos(-1.0)#define maxn 100000 + 10using namespace std;typedef long long int LLI;struct Edge {    int from;    int to;    bool flag;    Edge() {        flag = true;    }};Edge edge[maxn];int del[maxn];int Map[maxn],cnt,re[maxn];int find_father(int x) {    if (Map[x] == x) return x;    return Map[x] = find_father(Map[x]);}int main() {//    freopen("in.txt", "r", stdin);//    freopen("25.out", "w", stdout);    int n,m,q;    scanf("%d%d",&n,&m);    cnt = n;    for(int i= 1; i <= m; i ++) {        scanf("%d%d",&edge[i].from,&edge[i].to);    }    scanf("%d",&q);    for(int i = 1; i <= q; i ++) {        scanf("%d",&del[i]);        edge[del[i]].flag = false;    }    for(int i = 1; i <= n; i ++)  Map[i] = i;    for (int i = 1; i <= m; i++) {        if (edge[i].flag == true) {            int x = find_father(edge[i].from);            int y = find_father(edge[i].to);            if (x != y) {                Map[x] = y;                cnt--;            }        }    }    for(int i = q; i >= 1; i --) {        re[i] = cnt;        int x = edge[del[i]].from;        int y = edge[del[i]].to;        x = find_father(x);        y = find_father(y);        if(x != y) {            Map[x] = y;            cnt  --;        }    }    for(int i = 1;i < q;i ++)   printf("%d ",re[i]);    printf("%d\n",re[q]);    return 0;}





java(明明跟C++的写的一样,但是java慢,所以超时了,但是我毕竟辛苦写半天,也粘上来吧):

import java.math.BigInteger;import java.util.Arrays;import java.util.Scanner;public class Main {public static int Map[] = new int[100000 + 10];public static int find_father(int x) { if (Map[x] == x) return x;   return Map[x] = find_father(Map[x]);  }public static void main(String args[]) {Scanner Cin = new Scanner(System.in);int n = Cin.nextInt(), cnt = n;int m = Cin.nextInt();int[] delete = new int[100000 + 10];int[] re = new int[100000 + 10];Edge[] edges = new Edge[100000 + 10];for (int i = 1; i <= m; i++) {edges[i] = new Edge();edges[i].from = Cin.nextInt();edges[i].to = Cin.nextInt();}int q = Cin.nextInt();for (int i = 1; i <= q; i++) {delete[i] = Cin.nextInt();edges[delete[i]].flag = false;}for (int i = 1; i <= n; i++) {Map[i] = i;}for (int i = 1; i <= m; i++) {if (edges[i].flag == true) {int x = find_father(edges[i].from);int y = find_father(edges[i].to);if (x != y) {Map[x] = y;cnt--;}}}for(int i = q;i >= 1;i --){re[i] = cnt;int x = edges[delete[i]].from;int y = edges[delete[i]].to;x = find_father(x);y = find_father(y);if(x != y){Map[x] = y;cnt  --;}}for(int i = 1;i < q;i ++)System.out.printf("%d ", re[i]);System.out.println(re[q]);}}class Edge {int from;int to;boolean flag;Edge() {flag = true;}}


0 0
原创粉丝点击