zoukankan      html  css  js  c++  java
  • CSU 1601 War

    1601: War

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 127  Solved: 36
    [Submit][Status][Web Board]

    Description

    AME decided to destroy CH’s country. In CH’ country, There are N villages, which are numbered from 1 to N. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. To defend the country from the attack of AME, CH has decided to build some roads between some villages. Let us say that two villages belong to the same garrison area if they are connected.
    Now AME has already worked out the overall plan including which road and in which order would be attacked and destroyed. CH wants to know the number of garrison areas in his country after each of AME’s attack.

    Input

    The first line contains two integers N and M — the number of villages and roads, (2 ≤ N ≤ 100000; 1 ≤ M ≤ 100000). Each of the next M lines contains two different integers u, v (1<=u, v<=N)—which means there is a road between u and v. The next line contains an integer Q which denotes the quantity of roads AME wants to destroy (1 ≤ Q ≤ M). The last line contains a series of numbers each of which denoting a road as its order of appearance — different integers separated by spaces.

    Output

    Output Q integers — the number of garrison areas in CH’s country after each of AME's attack. Each pair of numbers are separated by a single space.

    Sample Input

    3 1
    1 2
    1
    1
    4 4
    1 2
    2 3
    1 3
    3 4
    3
    2 4 3

    Sample Output

    3
    1 2 3

    HINT

     

    Source

     
    题意:给N个点,M条边,给定Q次查询,问每次查询之后有几个独立连通块。
    分析:一开始算法是先用并查集找出有几个连通块,然后每次删边的时候看看端点的出入度是否为0,依此来计数,但是这么写了几发都是WA,也没找出有什么BUG,路过大神有兴趣的话可以和我探讨下啊~
    后来就换了算法,就是先用并查集找出最后有多少个连通块,之后从后往前加边。
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<queue>
    #include<algorithm>
    using namespace std;
    const int MAXN=100000+5;
    int p[MAXN],u[MAXN],v[MAXN],vis[MAXN],w[MAXN],cnt[MAXN];
    int n,m,Q,ans;
    
    int findfa(int x)
    {
        return p[x]==x?x:p[x]=findfa(p[x]);
    }
    void bin(int xx,int yy)
    {
        int x=findfa(xx);
        int y=findfa(yy);
        if(x!=y)
        {
            p[x]=y;
            ans--;
        }
    }
    int main()
    {
        while(scanf("%d %d",&n,&m)!=EOF)
        {
            ans=n;
            memset(vis,0,sizeof(vis));
    
            for(int i=1;i<=m;i++)
                scanf("%d %d",&u[i],&v[i]);
    
            scanf("%d",&Q);
            for(int i=1;i<=Q;i++)
            {
                scanf("%d",&w[i]);
                vis[w[i]]=1;
            }
            for(int i=1;i<=n;i++) p[i]=i;
            for(int i=1;i<=m;i++)
                if(vis[i]==0)
                    bin(u[i],v[i]);
    
            for(int i=Q;i>0;i--)
            {
                cnt[i]=ans;
                bin(u[w[i]],v[w[i]]);
            }
            for(int i=1;i<=Q;i++)
            {
                if(i==Q) printf("%d
    ",cnt[i]);
                else printf("%d ",cnt[i]);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    “TensorFlow 开发者出道计划”全攻略,玩转社区看这里!
    适合 C++ 新手学习的开源项目——在 GitHub 学编程
    【9303】平面分割
    【u114】旅行计划(12月你好)
    【u236】火炬
    【u233】单词化简
    Java Web整合开发(41) -- Forum
    1、服务器(软件)种类
    jquery trigger
    jQuery实现当按下回车键时绑定点击事件
  • 原文地址:https://www.cnblogs.com/clliff/p/4483717.html
Copyright © 2011-2022 走看看