【并查集】LA3644 化合物爆炸

A secret service developed a new kind of explosive that attain its volatile property only when a specific
association of products occurs. Each product is a mix of two different simple compounds, to which we
call a binding pair. If N > 2, then mixing N different binding pairs containing N simple compounds
creates a powerful explosive. For example, the binding pairs A+B, B+C, A+C (three pairs, three
compounds) result in an explosive, while A+B, B+C, A+D (three pairs, four compounds) does not.
You are not a secret agent but only a guy in a delivery agency with one dangerous problem: receive
binding pairs in sequential order and place them in a cargo ship. However, you must avoid placing in
the same room an explosive association. So, after placing a set of pairs, if you receive one pair that
might produce an explosion with some of the pairs already in stock, you must refuse it, otherwise, you
must accept it.
An example. Lets assume you receive the following sequence: A+B, G+B, D+F, A+E, E+G,
F+H. You would accept the first four pairs but then refuse E+G since it would be possible to make the
following explosive with the previous pairs: A+B, G+B, A+E, E+G (4 pairs with 4 simple compounds).
Finally, you would accept the last pair, F+H.
Compute the number of refusals given a sequence of binding pairs.

Input

The input will contain several test cases, each of them as described below. Consecutive
test cases are separated by a single blank line.
Instead of letters we will use integers to represent compounds. The input contains several lines.
Each line (except the last) consists of two integers (each integer lies between 0 and 105
) separated by
a single space, representing a binding pair.
Each test case ends in a line with the number ‘-1’. You may assume that no repeated binding pairs
appears in the input.

Output

For each test case, the output must follow the description below.
A single line with the number of refusals.
Sample Input
1 2
3 4
3 5
3 1
2 3
4 1
2 6
6 5
-1

Sample Output

3

题目大意

有K种化合物,每种化合物由两种元素组成。装车的时候,如果K种化合物正好含有K种元素,则会爆炸。问在最少有多少化合物不能被装车。

思路

用一条边表示一种化合物,边的两个顶点分别代表组成它的两个元素。易知,当形成环时,正好符合有K条边和K个顶点,会爆炸。反之,不符合,不会爆炸。
所以我们遍历化合物:

  • 如果它的两个元素在同一连通分量里,则加入后会形成环。则该化合物不能装车。
  • 否则将两种元素union起来。即加入化合物。

代码

import java.util.Scanner;
public class LA3644 {
    private static class UF{
        private int[] parent;
        private byte[] rank;
        UF(int N){
            // 编号从1开始,数组多开一个元素
            parent = new int[N + 1];
            rank = new byte[N + 1];
            for(int i = 0; i <= N; i++){
                parent[i] = i;
            }
        }
        private int find(int p){
            while (p != parent[p]){
                parent[p] = parent[parent[p]];
                p = parent[p];
            }
            return p;
        }
        public boolean same(int p, int q){
            return find(p) == find(q);
        }
        public void union(int p, int q){
            int rootP = find(p), rootQ = find(q);
            if(rank[rootP] < rank[rootQ]){
                parent[rootP] = rootQ;
            }else if(rank[rootP] > rank[rootQ]){
                parent[rootQ] = rootP;
            }else {
                parent[rootQ] = rootP;
                rank[rootP]++;
            }
        }
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while (in.hasNext()){
            int a, b;
            UF uf = new UF(100001);
            int res = 0;
            while (true){
                a = in.nextInt();
                if(a == -1) break;
                b = in.nextInt();
                if(uf.same(a, b)){
                    res++;
                }else{
                    uf.union(a, b);
                }
            }
            System.out.println(res);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/a617976080/article/details/88734263