uva 1329 Corporative Network && uva 1160 X-Plosives 并查集

A very big corporation is developing its corporative network. In the beginning each of the N enterprisesof the corporation, numerated from 1 to N, organized its own computing and telecommunication center.Soon, for amelioration of the services, the corporation started to collect some enterprises in clusters,each of them served by a single computing and telecommunication center as follow. The corporationchose one of the existing centers I (serving the cluster A) and one of the enterprises J in some othercluster B (not necessarily the center) and link them with telecommunication line. The length of theline between the enterprises I and J is |I −J|(mod 1000). In such a way the two old clusters are joinedin a new cluster, served by the center of the old cluster B. Unfortunately after each join the sum of thelengths of the lines linking an enterprise to its serving center could be changed and the end users wouldlike to know what is the new length. Write a program to keep trace of the changes in the organizationof the network that is able in each moment to answer the questions of the users.Your program has to be ready to solve more than one test case.

题目大意:n个点,两种操作

E i 查询节点i到根节点距离

I u v 将节点u链接到v节点下(此时u无父节点)

并查集,并维护每个节点到父节点距离

<strong>#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;

const int maxn = 2e4 + 10;
int fa[maxn], T, n, d[maxn];

int find(int x) {
    if (x == fa[x]) return x;
    int root = find(fa[x]);
    d[x] += d[fa[x]];
    fa[x] = root;
    return root;
}

int main() {
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i = 1; i <= n; i++) fa[i] = i;
        memset(d, 0, sizeof(d));
        char a;
        int x, y;
        while (1) {
            cin >> a;
            if (a == 'O') break;
            if (a == 'I') {
                cin >> x >> y;
                fa[x] = y;
                d[x] = abs(x - y) % 1000;
            }
            if (a == 'E') {
                cin >> x;
                find(x);
                cout << d[x] << endl;
            }
        }
    }
    return 0;
}
</strong>

A secret service developed a new kind of explosive that attain its volatile property only when a specificassociation of products occurs. Each product is a mix of two different simple compounds, to which wecall a binding pair. If N > 2, then mixing N different binding pairs containing N simple compoundscreates a powerful explosive. For example, the binding pairs A+B, B+C, A+C (three pairs, threecompounds) 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: receivebinding pairs in sequential order and place them in a cargo ship. However, you must avoid placing inthe same room an explosive association. So, after placing a set of pairs, if you receive one pair thatmight produce an explosion with some of the pairs already in stock, you must refuse it, otherwise, youmust 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 thefollowing 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.

每个化合物都由两种元素组成,例如AB,每一种化合物由两种简单的元素组成,现在有n种化合物要装车,如果出现说有k种化合物刚好由k种元组组成,就会发生化学发应。工人在每次装车的时候会检查是否有可能发生发应,有的话将放弃装车。问说最后有几个化合物没有装车。

将元素看作点,那么一个化合物就是一条边,k个化合物含有k个元素其实就是一个环,那么用并查集来维护集合就好。

<strong>#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 1e5 + 10;
int fa[maxn];

int find(int x) {
    if (x == fa[x]) return x;
    else return fa[x] = find(fa[x]);
}

int link(int x, int y) {
    int nx = find(x);
    int ny = find(y);
    if (nx == ny) return 0;
    else fa[nx] = ny;
    return 1;
}

int main() {
    int x, y;
    while (scanf("%d", &x) == 1) {
        for (int i = 0; i < maxn; i++) {
            fa[i] = i;    
        }
        int tot = 0;
        while (1) {
            if (x == -1) break;
            scanf("%d", &y);
            if (!link(x, y)) {
                tot ++;
            }
            scanf("%d", &x);
        }
        printf("%d\n", tot);
    }    
    return 0;
}
</strong>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值