0分求调,回复必关

P1197 [JSOI2008] 星球大战

Bobby0505 @ 2024-04-20 22:05:24

#include<bits/stdc++.h>
using namespace std;
vector < vector<int> >a;
int n,m,q,fa[400005],des[400005],v[400005],ans,pr[400005],tot;
int find(int x){
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
    cin>>n>>m;
    a.resize(n+4);
    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    for(int i=0;i<n;i++){
        fa[i]=i;
    }
    cin>>q;
    for(int i=1;i<=q;i++){
        cin>>des[i];
        v[des[i]]=1;
    }
    ans=n-q;
    for(int i=0;i<n;i++){
        if(!v[i]){
            for(auto j:a[i]){
                if(!v[j]){
                    if(find(i)!=find(j)){
                        ans--;
                        fa[find(i)]=find(j);
                    }
                }
            } 
        }
    }
    tot=tot+1;
    pr[tot]=ans;
    for(int i=q;i;--i){
        v[des[i]]=0;
        for(auto j:a[des[i]]){
            if(!v[i]){
                if(find(des[i])!=find(j)){
                    ans--;
                    fa[find(des[i])]=find(j);
                }
            }
        }
        tot=tot+1;
        pr[tot]=ans;
    }
    for(int i=tot;i;i--){
        cout<<pr[i]<<endl;
    }
    return 0;
}

by yu1128 @ 2024-04-21 18:26:48

同求


by a1111a @ 2024-04-21 19:15:29

太多问题了,说不过来


by a1111a @ 2024-04-21 19:17:25


by a1111a @ 2024-04-21 19:19:19


by a1111a @ 2024-04-21 19:32:02

然后我就不说了


by yu1128 @ 2024-04-21 21:07:42

@a1111a 别bb。

@Bobby0505 只需要把第二个循环改为

for(int j=0;j<a[des[i]].size();j++){
            if(!v[a[des[i]][j]]){
                if(find(des[i])!=find(a[des[i]][j]))ans--,fa[find(des[i])]=find(a[des[i]][j]);
            }
        }

by Bobby0505 @ 2024-04-24 21:22:26

thank you


|