文章

85

粉丝

0

获赞

578

访问

13.8k

头像
土豪我们做朋友吧 题解:75%超时解
P1129
发布于2026年3月7日 17:20
阅读数 170

#include <bits/stdc++.h>
using namespace std;

const int MAX =100005;
int fa[MAX];
int money[MAX];
int find(int x) {
    if (x == fa[x]) {
        return x;
    }
    fa[x] = find(fa[x]);
    return fa[x];
}

void Deal(int N) {
    int choice;
    cin >> choice;
    if (choice == 1) {
        int a,b;
        cin >> a >> b;
        if (find(a) != find(b)) {
            fa[find(a)] = find(b);
        }
    }
    if (choice == 2) {
        int people;
        cin >> people;
        int father = find(people);
        int maxMoney = -1;
        int helper = -1;
        // 遍历查找同组中钱最多且不是自己的人
        for (int i = 1; i <= N; i++) {
            if (find(i) == father && money[i] > 0 && i != people) {
                if (money[i] > maxMoney) {
                    maxMoney = money[i];
                    helper = i;
                }
            }
        }
        if (helper == -1) {
            co...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发