邓锐杰的作业

· · 个人记录

P2504 [HAOI2006] 聪明的猴子

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

int fa[1000005], n, m, cnt, ans, sum2;
double sum;

struct d{
    int x, y;
    double w;
}a[10000005];

struct dd{
    int x, y;
}s[10000005];

int t[100005];

double jl(int i, int j) {
    return sqrt(1.0 * (s[i].x - s[j].x) * (s[i].x - s[j].x) + (s[i].y - s[j].y) * (s[i].y - s[j].y));
}
bool cmp(d x, d y) {
    return x.w < y.w;
}
int find(int x) {
    if (fa[x] == x) return x;
    return fa[x] = find(fa[x]);
}
void unionn(int x, int y) {
    fa[x] = y;
    return ;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> t[i];
    cin >> m;
    for (int i = 1; i <= m; i++) fa[i] = i;
    for (int i = 1; i <= m; i++) cin >> s[i].x >> s[i].y;
    ans = 0;
    for (int i = 1; i <= m; i++){
        for (int j = i + 1; j <= m; j++){
            ans++;
            a[ans].x = i;
            a[ans].y = j;
            a[ans].w = jl(i, j);
        }
    }
    sort(a + 1, a + ans + 1, cmp);
    for (int i = 1; i <= ans; i++) {
        int x = find(a[i].x), y = find(a[i].y);
        if (x == y) continue;
        unionn(x, y);
        cnt++;
        sum = max(sum, a[i].w);
    }
    for (int i = 1; i <= n; i++) if(sum <= t[i]) sum2++;
    cout << sum2;
    return 0;
}

P2330 [SCOI2005] 繁忙的都市

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

int n, m, fa[20005], cnt, sum;

struct d{
    int x, y, w;
}a[200005];

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

void unionn(int x, int y){
    x = find(x), y = find(y);
    if (x != y) fa[x] = y;
}
int cmp(d x, d y){
    return x.w < y.w;
}

signed main(){
    cin >> n >> m;
    for (int i = 1; i <= m; i++) cin >> a[i].x >> a[i].y >> a[i].w;
    for (int i = 1; i <= n; i++) fa[i] = i;
    sort(a + 1, a + m + 1, cmp);
    for (int i = 1; i <= m; i++){
        int x = find(a[i].x), y = find(a[i].y);
        if (x == y) continue;
        sum = a[i].w;
        unionn(x, y);
        cnt++;
    }
    cout << cnt << ' ' << sum;
    return 0;
}

P8191 [USACO22FEB] Moo Network G

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

int n, m, fa[4000005], cnt, total;
int sum;

struct d{
    int x, y;
    int w;
}a[4000005];
struct no{
    int x, y;
}ss[4000005];

int vivo(int i, int j){
    return (ss[i].x - ss[j].x) * (ss[i].x - ss[j].x) + (ss[i].y - ss[j].y) * (ss[i].y - ss[j].y);
}

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

void unionn(int x, int y){
    fa[x] = y;
}
bool cmp(d x, d y){
    return x.w < y.w;
}

bool cmp2(no x, no y){
    if (x.x == y.x) return x.y < y.y;
    return x.x < y.x;
}

signed main(){
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> ss[i].x >> ss[i].y;
    sort(ss + 1, ss + n + 1, cmp2);
    for (int i = 1; i <= n; i++) fa[i] = i;
    for (int i = 1; i <= n; i++){
        for (int j = i + 1; j <= min(n, i + 35); j++){
            total++;
            a[total].x = i;
            a[total].y = j;
            a[total].w = vivo(i, j);
        }
    }
    sort(a + 1, a + total + 1, cmp);
    for (int i = 1; i <= total; i++){
        int x = find(a[i].x), y = find(a[i].y);
        if (x == y) continue;
        sum += a[i].w;
        unionn(x, y);
        cnt++;
    }
    cout << sum;
    return 0;
}