求大佬帮助优化

P1197 [JSOI2008] 星球大战

木易先生 @ 2021-01-18 12:05:26

麻烦各位大佬看一下,倒叙并查集优化后还是只有30分。。求助qwq(不会用链表存,有没有别的方式```c

#include<stdio.h>
#include<string.h>
int fa[600002], n, m, a[600002][2], k, k1[600002], zhuang[600002], wei[600002], q, num,check[600002],check1[600002];
void chu()
{
    for (int i = 0; i < n; i++)
        fa[i] = i;
}
int find(int x)
{
    if (fa[x] == x)return x;
    fa[x] = find(fa[x]);
    return fa[x];
}
void merge(int i, int j)
{
    fa[find(i)] = find(j);
}
int main()
{
    scanf("%d%d", &n, &m);
    chu();
    for (int i = 0; i < m; i++)
        scanf("%d%d", &a[i][0], &a[i][1]);
    scanf("%d", &k);
    for (int i = 0; i < k; i++)
        scanf("%d", &k1[i]),zhuang[k1[i]]=1;//读入
    for (int i = 0; i < m; i++)
        if (zhuang[a[i][0]] == 0 && zhuang[a[i][1]] == 0)merge(a[i][0], a[i][1]);
    for (int i = 0; i < n; i++)
        if (zhuang[i] == 0 && check[find(i)] == 0)check[find(i)] = 1, num++;
    wei[k] = num;
    for (int i = k-1; i >=0; i--)
    {
        zhuang[k1[i]] = 0, memset(check, 0, sizeof(check)), memset(check1, 0, sizeof(check1)),num++;int x = 121,o=k1[i];
        for (int i = 0; i < m; i++)
        {   
            if ((a[i][0] == o || a[i][1] == o) && zhuang[a[i][0]] == 0 && zhuang[a[i][1]] == 0)
            {
                int z = find(a[i][1]), x = find(a[i][0]);
                    merge(a[i][0], a[i][1]);
                    if(check1[z]==0||check1[x]==0)
                    num--;
                    check1[find(a[i][1])] = 1;
                    check1[find(a[i][0])] = 1;
            }
        }
        wei[i] = num;
    }
    for (int i = 0; i <=k ; i++)
        printf("%d\n",wei[i]);
}

|