一来到夏令营,第一天上机就考试,

哎,简直不让人活了

这难道是给我们的见面礼???

 

A

 

https://www.luogu.org/problemnew/show/P1197

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>

using namespace std;

int n,m,q,len=0;
int last[400010],p[400010],father[400010],ans[400010];

struct node
{
    int x;
    int y;
    int next;
} qwq[1000010];

bool bz[400010];

void add(int x,int y)
{
    qwq[++len].x=x;
    qwq[len].y=y;
    qwq[len].next=last[x];
    last[x]=len;
}

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

int main()
{
    int x,y;
    scanf("%d %d",&n,&m);
    for(int i=0;i<=n;i++)
    {
        last[i]=-1;
        father[i]=i;
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d %d",&x,&y);    
        add(x,y);
        add(y,x);
    }
    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        scanf("%d",&p[i]);
        bz[p[i]]=true;
    }
    int tot1=n-q;
    m=m<<1;
    for(int i=1;i<=m;i++)
    {
        int x=qwq[i].x,y=qwq[i].y,t1=find(x),t2=find(y);
        if(!bz[x]&&!bz[y]&&t1!=t2)
        {
            tot1--;
            father[t1]=t2;    
        }
    }
    ans[q+1]=tot1;
    for(int k=q;k>=1;k--)
    {
        int x=p[k];
        bz[x]=false;
        tot1++;
        for(int i=last[x];i!=-1;i=qwq[i].next)
        {
            int t1=find(x),y=qwq[i].y,t2=find(y);
            if(!bz[y]&&t1!=t2)
            {
                tot1--;
                father[t1]=t2;
            }
        }
        ans[k]=tot1;
    }
    for(int i=1;i<=q+1;i++)
    {
        printf("%d\n",ans[i]);
    }
    return 0;
}
View Code

相关文章:

  • 2022-12-23
  • 2022-01-18
  • 2021-10-24
  • 2021-12-12
  • 2021-11-02
  • 2021-12-17
  • 2021-08-09
  • 2021-06-23
猜你喜欢
  • 2022-12-23
  • 2021-06-19
  • 2021-12-17
  • 2021-12-30
  • 2021-11-22
  • 2021-12-05
相关资源
相似解决方案