小傻子求助

P1197 [JSOI2008] 星球大战

樱初音斗橡皮 @ 2019-03-08 19:40:31

RT,#9 TLE

#include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
using std::scanf;
using std::printf;
char _c_c_c_;
template<class _Tp>
_Tp read(_Tp& t)
{
    t=0;
    while ((_c_c_c_=getchar())>'9'||_c_c_c_<'0');
    t=_c_c_c_-'0';
    while ((_c_c_c_=getchar())<='9'&&_c_c_c_>='0')
        t=(t<<3)+(t<<1)+_c_c_c_-'0';
    return t;
}
int n, m;
int fa[200002];
int findanc(int x)
{
    if (x==fa[x])
        return x;
    else
        return fa[x]=findanc(fa[x]);
}
void merge(int x, int y)
{
    x=findanc(x), y=findanc(y);
    fa[x]=y;
}
const int N=400002;
int head[N];
struct node
{
    int frm, to, nxt, wei;
} edge[N];
int cnt=0;
void add_edge(int f, int t, int w=1)
{
    cnt++;
    edge[cnt].frm=f;
    edge[cnt].to=t;
    edge[cnt].nxt=head[f];
    edge[cnt].wei=w;
    head[f]=cnt;
    return;
}
int d[N];
bool del[N];
bool vis[N];
inline void del_edge(const int& f)
{
    del[f]=true;
    return;
}
void bfs(int i)
{
    std::queue<int> q;
    for (int j=head[i]; j!=-1; j=edge[j].nxt)
    {
        int t=edge[j].to;
        if (!del[t]&&!vis[t])
            q.push(t), vis[t]=true, merge(t, i);
    }
    while (!q.empty())
    {
        int t=q.front();
        q.pop();
        for (int j=head[t]; j!=-1; j=edge[j].nxt)
        {
            int tt=edge[j].to;
            if (!del[tt]&&!vis[tt])
                q.push(tt), vis[tt]=true, merge(tt, i);
        }
    }
    return;
}
std::stack<int> aaa;
int main()
{
    read(n);
    read(m);
    for (int i=1; i<=n; i++)
        fa[i]=i;
    for (int i=1; i<=n; ++i)
        head[i]=-1;
    for (int i=1; i<=m; ++i)
    {
        int f, t;
        read(f);
        read(t);
        add_edge(f+1, t+1);
        add_edge(t+1, f+1);
    }
    int k;
    read(k);
    for (int i=1; i<=k; ++i)
    {
        read(d[i]);
        d[i]++;
        del_edge(d[i]);
    }
    int ans=n;
    for (int i=1; i<=2*m; ++i)
        if (!del[edge[i].frm]&&!del[edge[i].to])
        {
            if (findanc(edge[i].frm)!=findanc(edge[i].to))
                merge(edge[i].frm, edge[i].to), --ans;
        }
    aaa.push(ans-k);
    for (int i=k; i>=1; --i)
    {
        del[d[i]]=false;
        for (int j=head[d[i]]; j!=-1; j=edge[j].nxt)
        {
            int t=edge[j].to;
            if (!del[t]&&findanc(d[i])!=findanc(t))
                --ans, merge(d[i], t);
        }
        aaa.push(ans-i+1);
    }
    while (!aaa.empty())
    {
        int t=aaa.top();
        aaa.pop();
        printf("%d\n", t);
    }
    return 0;
}

by xiaolou @ 2019-03-08 19:49:23

蛤?


by 伊卡洛斯之翼 @ 2019-03-08 20:37:09

在线膜yyc大佬qaq


|