10分求助,样例过了

P1197 [JSOI2008] 星球大战

hwwqy @ 2022-05-02 18:54:37

#include<bits/stdc++.h>
#define int long long
#define S 500100
using namespace std;
vector <int> G[S];
int a[S],fa[S],ans[S],dj[S];
bool djl[S];
int n,m,k;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if(ch=='-')f=-1; ch=getchar();}
    while (ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0'; ch=getchar();}
    return x*f;
}
void get_input()
{
    n=read(),m=read();
    for(int i=1;i<=m;i++)
    {
        int x=read(),y=read();
        G[x].push_back(y);
        G[y].push_back(x);
    }
    k=read();
    for(int i=1;i<=k;i++)
    {
        dj[i]=read();
        djl[dj[i]]=true;
    }
}

void init()
{
    for(int i=0;i<n;i++)
    {
        fa[i]=i;
    }
}
int find(int x)
{
    if(fa[x]==x)return x;
    return fa[x]=find(fa[x]);
}
void untie(int x,int y)
{

    if(find(x)!=find(y))fa[x]=find(y);
} 
bool same(int x,int y)
{
    return (find(x)==find(y));
} 

void solve()
{
    get_input();
    init();
    int ltk=n-k;
    for(int i=0;i<n;i++)
    {       

        for(int j=0;j<G[i].size()&&!djl[i];j++)
        {           
            if(!djl[G[i][j]])
            {
                if(!same(G[i][j],i))ltk--;
                untie(i,G[i][j]);           
            }
        } 
    }
    ans[k+1]=ltk;
    for(int i=k;i>=1;i--)
    {
        ltk++;
        djl[dj[i]]=false;
        for(int j=0;j<G[dj[i]].size();j++)
        {           

            if(!djl[G[dj[i]][j]]&&!same(dj[i],G[dj[i]][j]))
            {               
                ltk--;
                untie(G[dj[i]][j],dj[i]);                   
            }
        }
        ans[i]=ltk;
    }
    for(int i=1;i<=k+1;i++)cout<<ans[i]<<endl;

}

signed main()
{
    solve();
    return 0;
}
/*
8 13
0 1 
1 6
6 5
5 0
0 6
1 2
2 3
3 4
4 5
7 1
7 2
7 6
3 6
5
1
6
3
5
7
*/

by hwwqy @ 2022-05-05 16:02:14

#include<bits/stdc++.h>
#define int long long
#define S 500100
using namespace std;
vector <int> G[S];
int a[S],fa[S],ans[S],dj[S];
bool djl[S];
int n,m,k;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if(ch=='-')f=-1; ch=getchar();}
    while (ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0'; ch=getchar();}
    return x*f;
}
void get_input()
{
    n=read(),m=read();
    for(int i=1;i<=m;i++)
    {
        int x=read(),y=read();
        G[x].push_back(y);
        G[y].push_back(x);
    }
    k=read();
    for(int i=1;i<=k;i++)
    {
        dj[i]=read();
        djl[dj[i]]=true;
    }
}

void init()
{
    for(int i=0;i<n;i++)
    {
        fa[i]=i;
    }
}
int find(int x)
{
    if(fa[x]==x)return x;
    return fa[x]=find(fa[x]);
}
void untie(int x,int y)
{
    x=find(x),y=find(y);
    if(x!=y)    fa[x]=y;
} 
bool same(int x,int y)
{
    return (find(x)==find(y));
} 

void solve()
{
    get_input();
    init();
    int ltk=n-k;
    for(int i=0;i<n;i++)
    {       

        for(int j=0;j<G[i].size()&&!djl[i];j++)
        {           
            if(!djl[G[i][j]])
            {
                ltk--;
                untie(i,G[i][j]);           
            }
        } 
    }
    ans[k+1]=ltk;
    for(int i=k;i>=1;i--)
    {
        ltk++;
        djl[dj[i]]=false;
        for(int j=0;j<G[dj[i]].size();j++)
        {           

            if(!djl[G[dj[i]][j]])
            {               
                if(!same(G[dj[i]][j],dj[i]))ltk--;
                untie(G[dj[i]][j],dj[i]);                   
            }
        }
        ans[i]=ltk;
    }
    for(int i=1;i<=k+1;i++)cout<<ans[i]<<endl;

}

signed main()
{
    solve();
    return 0;
}
/*
8 13
0 1 
1 6
6 5
5 0
0 6
1 2
2 3
3 4
4 5
7 1
7 2
7 6
3 6
5
1
6
3
5
7
*/

|