Ural 1671 Anansi's Cobweb

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 of N 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 to M in the order of description. Next line contains an integer Q 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 4
1 2
2 3
1 3
3 4
3
2 4 3
1 2 3
3 1
1 2
1
1
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 + 10
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值