40分蒟蒻在线求助

P1197 [JSOI2008] 星球大战

_TMT_ @ 2020-01-05 12:05:25

我把点转化成1~n存的,但应该没问题啊

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,q,ne,sum,ans[400005],head[400005],fa[400005],p[400005],flag[400005],t[400005];
struct edge
{
    int to,next;
}e[400005];
void ins(int u,int v)
{
    ne++;
    e[ne].to=v;
    e[ne].next=head[u];
    head[u]=ne;
}
int father(int x)
{
    return fa[x]==x?x:fa[x]=father(fa[x]);
}
void getfa(int u,int v)
{
    int x=father(u),y=father(v);
    fa[x]=y;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        ins(u+1,v+1);
        ins(v+1,u+1);
    }
    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        scanf("%d",&p[i]);
        flag[p[i]+1]=1;
    }
    for(int u=1;u<=n;u++)
    {
        if(flag[u]==0)
        {
            for(int i=head[u];i;i=e[i].next)
            {
                int v=e[i].to;
                if(flag[v]==0)
                {
                    getfa(u,v);
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(flag[i]==0&&t[father(i)]==0)
        {
            t[father(i)]=1;
            sum++;
        }
    }
    for(int i=q;i>=1;i--)
    {
        ans[i]=sum;
        for(int j=head[p[i]+1];j;j=e[j].next)
        {
            int v=e[j].to,f1=father(p[i]+1),f2=father(v);
            if(flag[v]==0)
            if(f1!=f2)
            {
                sum--;
                getfa(f1,f2);
            }
        }
        sum++;
        flag[p[i]+1]=0;
    }
    printf("%d\n",ans[1]);
    for(int i=1;i<=q;i++)printf("%d\n",ans[i]);
    return 0;
}

by _TMT_ @ 2020-01-05 12:14:22

已经AC了,最后输出第一行ans【1】改成sum就行了,蠢哭了


by _脑波_ @ 2020-01-05 13:28:18

呃呃呃


|