BZOJ 1015 [Star Wars StarWar]

Face questions

The meaning of problems

  Given an undirected graph, each point, and determining the number of deleting a communication component.

answer

  Difficult to delete points while maintaining the number of China Unicom component, but if the reverse operation, the question becomes each time to the number of connected components without adding a point to Ituri and requirements. It can be disjoint-set maintenance.


Code

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int maxn=4e5+5;
vector<int> e[maxn];
int fa[maxn];
bool vis[maxn];
int arr[maxn],ans[maxn];
int ff(int u){ return fa[u]==u?u:fa[u]=ff(fa[u]); }
int main(){
    int i,j,n,m;
    int u,v;
    int cnt;
    scanf("%d%d",&n,&m);
    while (m--){
        scanf("%d%d",&u,&v);
        e[u].push_back(v); e[v].push_back(u);
    }
    scanf("%d",&m);
    for (i=0;i<n;i++){ fa[i]=i; vis[i]=true; }
    for (i=0;i<m;i++){
        scanf("%d",&arr[i]); vis[arr[i]]=false;
    }
    cnt=n-m;
    for (i=0;i<n;i++) if (vis[i]){
        for (j=0;j<e[i].size();j++){
            u=i; v=e[i][j];
            if (!vis[v]) continue;
            u=ff(u); v=ff(v);
            if (u!=v){ cnt--; fa[u]=v; }
        }
    }
    ans[m]=cnt;
    for (i=m-1;i>=0;i--){
        vis[arr[i]]=true; cnt++;
        for (j=0;j<e[arr[i]].size();j++){
            u=arr[i]; v=e[arr[i]][j];
            if (!vis[v]) continue;
            u=ff(u); v=ff(v);
            if (u!=v){ cnt--; fa[u]=v; }
        }
        ans[i]=cnt;
    }
    for (i=0;i<=m;i++) printf("%d\n",ans[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/Kilo-5723/p/12190175.html