zoukankan      html  css  js  c++  java
  • BZOJ 1015 星球大战

    思路其实很好想:就是倒着维护并查集。

    然而?细节超多。

    首先,每个节点可能被攻击多次,只算刚开始攻击的那一次。

    第二,考虑清楚并查集的从属关系。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define maxe 950050
    #define maxv 950050
    using namespace std;
    struct edge
    {
    int v,nxt;
    }e[maxe];
    int g[maxv],n,m,k,x,y,now[maxv],ans[maxv],cnt=0,nume=0,father[maxv],first[maxv];
    bool vis[maxv],judge[maxv];
    void addedge(int u,int v)
    {
    e[++nume].v=v;
    e[nume].nxt=g[u];
    g[u]=nume;
    }
    void dfs(int x,int fath)
    {
    vis[x]=true;father[x]=fath;
    for (int i=g[x];i;i=e[i].nxt)
    {
    if ((vis[e[i].v]==false) && (judge[e[i].v]==true))
    dfs(e[i].v,fath);
    }
    }
    int getfather(int x)
    {
    if (x!=father[x])
    father[x]=getfather(father[x]);
    return father[x];
    }
    void work(int x)
    {
    if (first[now[x]]!=x)
    {
    ans[x]=cnt;
    return;
    }
    int then=now[x];
    cnt++;
    for (int i=g[then];i;i=e[i].nxt)
    {
    int v=e[i].v;
    if (judge[v]==true)
    {
    int f=getfather(v),f2=getfather(now[x]);
    if (f!=f2)
    {
    cnt--;
    father[f]=f2;
    }
    }
    }
    judge[now[x]]=true;
    ans[x]=cnt;
    }
    int main()
    {
    memset(g,0,sizeof(g));
    memset(judge,true,sizeof(judge));
    memset(vis,false,sizeof(vis));
    memset(first,0,sizeof(first));
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++)
    {
    scanf("%d%d",&x,&y);
    addedge(x,y);
    addedge(y,x);
    }
    scanf("%d",&k);
    for (int i=1;i<=k;i++)
    {
    scanf("%d",&now[i]);
    father[now[i]]=now[i];
    judge[now[i]]=false;
    if (first[now[i]]==0)
    first[now[i]]=i;
    }
    for (int i=0;i<n;i++)
    {
    if ((vis[i]==false) && (judge[i]==true))
    {
    father[i]=i;
    cnt++;
    dfs(i,i);
    }
    }
    ans[k+1]=cnt;
    for (int i=k;i>=1;i--)
    work(i);
    for (int i=1;i<=k+1;i++)
    printf("%d ",ans[i]);
    return 0;
    }

  • 相关阅读:
    什么是 bean 的自动装配?
    什么是 Spring 的内部 bean?
    什么是 Spring 的 MVC 框架?
    Spring AOP and AspectJ AOP 有什么区别?
    解释 JDBC 抽象和 DAO 模块?
    volatile 类型变量提供什么保证?
    一个 Spring Bean 定义 包含什么?
    什么是 Spring MVC 框架的控制器?
    使用 Spring 访问 Hibernate 的方法有哪些?
    什么是 Callable 和 Future?
  • 原文地址:https://www.cnblogs.com/ziliuziliu/p/5239023.html
Copyright © 2011-2022 走看看