求调代码

P1197 [JSOI2008] 星球大战

Xianzi_ @ 2024-11-14 17:19:20

rt, QwQ

#include "bits/stdc++.h"
#define I using
#define Like namespace
#define March7th std
I Like March7th;
struct node {
    int u, v, w;
}edge[400005];
int n, num, m, k;
int f[400005], a[400005], v[400005], ans[400005];
bool operator < (node a, node b){
    return a.w < b.w;
}
void init(){
    for (int i = 0; i < n; i++) f[i] = i;
}
int find(int x){
    if (f[x] == x) return x;
    else return f[x] = find(f[x]);
}
void Union(int x, int y){
    int a = find(x), b = find(y);
    if (a != b) f[a] = b, num--;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // freopen("P1197ans.out", "r", stdout);
    cin >> n >> m;
    num = n;
    for (int i = 0; i < m; i++){
        cin >> edge[i].u >> edge[i].v;
        edge[i].w = 0;
    }
    cin >> k;
    for (int i = 0; i < k; i++){
        cin >> a[i];
        v[a[i]] = k - i;
    }
    for (int i = 0; i < m; i++)
        edge[i].w = max(v[edge[i].u], v[edge[i].v]);
    sort(edge, edge + m);
    for (int i = 0, j = 0; i <= k; i++){
        for ( ; edge[i].w == i; j++) Union(edge[j].u, edge[j].v);
        ans[i] = num - k + i; 
    }
    for (int i = k; i >= 0; i--) cout << ans[i] << endl;
    return 0;
}

|