团体程序设计天梯赛-练习集 L2 (并查集)

L2-007 家庭房产 (25 分)

#include 
#include 
#include 
using namespace std;
const int N = 1e3 + 10, M = 1e4 + 10;

struct Person {
    int id;
    int num, area;
}person[N];
struct Family {
    int id;
    int cnt, num, area;
    double res1, res2;
}family[M];

int p[M];
bool st[M];

int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}
void merge(int a, int b) {
    int aa = find(a), bb = find(b);
    if (aa < bb) {
        p[bb] = aa;
    } else {
        p[aa] = bb;
    }
}
bool cmp(Family a, Family b) {
    if (a.res2 != b.res2) return a.res2 > b.res2;
    return a.id < b.id;
}

int main() {
    for (int i = 0; i < M; ++ i) {
        p[i] = i;
    }
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++ i) {
        int id, id1, id2;
        cin >> id >> id1 >> id2;
        person[i].id = id;
        st[id] = true;
        if (id1 != -1) {
            st[id1] = true;
            merge(id1, id);
        }
        if (id2 != -1) {
            st[id2] = true;
            merge(id2, id);
        }
        int k;
        cin >> k;
        while (k -- ) {
            int id3;
            cin >> id3;
            st[id3] = true;
            merge(id, id3);
        }
        int num, area;
        cin >> num >> area;
        person[i].num = num;
        person[i].area = area;
    }
    for (int i = 1; i <= n; ++ i) {
        int fa = find(person[i].id);
        family[fa].id = fa;
        family[fa].num += person[i].num;
        family[fa].area += person[i].area;
    }
    for (int i = 0; i < M; ++ i) {
        if (st[i]) {
            int fa = find(i);
            family[fa].cnt ++ ;
        }
    }
    int res = 0;
    for (int i = 0; i < M; ++ i) {
        if (family[i].cnt) {
            res ++ ;
            family[i].res1 = family[i].num * 1.0 / family[i].cnt;
            family[i].res2 = family[i].area * 1.0 / family[i].cnt;
        }
    }
    cout << res << endl;
    sort(family, family + M, cmp);
    for (int i = 0; i < res; ++ i) {
        printf("%04d %d %.3lf %.3lf", family[i].id, family[i].cnt, family[i].res1, family[i].res2);
        if (i != res - 1) cout << endl;
    }
}

L2-013 红色警报 (25 分)

#include 
using namespace std;
const int N = 5e4 + 10;

int n, m;
int p[N];
bool st[N];
int a[N], b[N];

int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}
void merge(int u, int v) {
    int uu = find(u), vv = find(v);
    p[uu] = vv;
}
int count() {
    int cnt = 0;
    for (int i = 0; i < n; ++ i) {
        if (p[i] == i) {
            cnt ++ ;
        }
    }
    return cnt;
}

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; ++ i) {
        p[i] = i;
    }
    for (int i = 0; i < m; ++ i) {
        cin >> a[i] >> b[i];
        merge(a[i], b[i]);
    }
    int cnt0 = count();
    int k;
    cin >> k;
    for (int i = 1; i <= k; ++ i) {
        int x;
        cin >> x;
        st[x] = true;
        for (int j = 0; j < n; ++ j) {
            p[j] = j;
        }
        for (int j = 0; j < m; ++ j) {
            if (!st[a[j]] && !st[b[j]]) {
                merge(a[j], b[j]);
            }
        }
        int cnt1 = count();
        if (cnt1 == cnt0 || cnt1 == cnt0 + 1) {
            printf("City %d is lost.\n", x);
        } else {
            printf("Red Alert: City %d is lost!\n", x);
        }
        cnt0 = cnt1;
    }
    if (k == n) {
        cout << "Game Over.";
    }
}

你可能感兴趣的:(天梯赛)