题意:给一个无向图。每次查询破坏一条边,每次输出查询后连通图的个数。

思路:并查集。逆向思维,删边变成加边。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#define inf -100000000
#define LL long long
#define maxn 100005
using namespace std;
struct Edge
{
    int x,y;
};
int parent[maxn];
int findset(int p)
{
    return (parent[p]==p)?p:(parent[p]=findset(parent[p]));
}
Edge edge[maxn];
int query[maxn];
bool build[maxn];
int ans[maxn];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(build,,sizeof(build));
        ; i<=m; ++i)
            scanf("%d%d",&edge[i].x,&edge[i].y);
        int q;
        scanf("%d",&q);
        ; i<=q; ++i)
        {
            scanf("%d",&query[i]);
            build[query[i]]=true;
        }
        ; i<=n; ++i)
            parent[i]=i;
        ;
        ; i<=m; ++i)
            if(!build[i])
            {
                int fa=findset(edge[i].x),fb=findset(edge[i].y);
                if(fa!=fb)
                    parent[fa]=fb;
            }
        ; i<=n; ++i)
            if(findset(i)==i) cnt++;
        ; --i)
        {
            ans[i]=cnt;
            int fa=findset(edge[query[i]].x),fb=findset(edge[query[i]].y);
            if(fa!=fb)
            {
                parent[fa]=fb;
                cnt--;
            }
        }
        ; i<=q; ++i)
            ) printf("%d",ans[i]);
            else printf(" %d",ans[i]);
        printf("\n");
    }
    ;
}

URAL 1671 Anansi's Cobweb (并查集)的更多相关文章

  1. ural 1671 Anansi's Cobweb

    这道题是并差集的简单应用 #include <cstdio> #include <cstring> #include <algorithm> #define max ...

  2. 1671. Anansi's Cobweb(并查集)

    1671 并查集 对于询问删除边之后的连通块 可以倒着加边 最后再倒序输出 #include <iostream> #include<cstdio> #include<c ...

  3. URAL 1320 Graph Decomposition(并查集)

    1320. Graph Decomposition Time limit: 0.5 secondMemory limit: 64 MB There is a simple graph with an ...

  4. URAL 1682 Crazy Professor (并查集)

    [题目链接] http://acm.timus.ru/problem.aspx?space=1&num=1682 [题目大意] 给出k,从1开始不断地加一并把这个数写在黑板上,如果写上的数字和 ...

  5. URAL(timus)1709 Penguin-Avia(并查集)

    Penguin-Avia Time limit: 1.0 secondMemory limit: 64 MB The Penguin-Avia airline, along with other An ...

  6. URAL - 1003:Parity (带权并查集&2-sat)

    Now and then you play the following game with your friend. Your friend writes down a sequence consis ...

  7. ural1671 Anansi's Cobweb

    Anansi's Cobweb Time limit: 1.0 secondMemory limit: 64 MB Usatiy-Polosatiy XIII decided to destroy A ...

  8. 51nod 1204 Parity(并查集应用)

    1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串 ...

  9. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

随机推荐

  1. innodb_fast_shutdown中值为1或者2的区别是?

    innodb_fast_shutdown=0 , 1 , 2时的意思分别是 0 把buffer pool中的脏页刷到磁盘和合并insert buffer,当然包括redo log也会写到磁盘中. 2 ...

  2. ios 给uiview创作遮罩

    mask一定是PNG格式的图像,去掉背景 .jpg格式图片没有alpha values 遮罩是通过图片透明度的信息实现与颜色无关 UIImage*_maskingImage =[UIImage ima ...

  3. C++/C#中堆栈、对象内存模型、深浅拷贝、Array.Clone方法

    转载自:http://blog.csdn.net/jarvischu/article/details/6425534 目录 1.      C++/C#中对象内存模型................. ...

  4. HTML5自学笔记[ 9 ]HTML5实现元素的拖放

    要想在html5中实现元素的拖放,被拖放元素就必须设置属性draggable="true";被拖放元素被放置的地方是另外一个元素,该元素是目标元素:这两个元素在拖放过程中都会触发不 ...

  5. Qt之Meta-Object系统

    简述 Qt的元对象系统(Meta-Object System)提供了信号与槽机制,可用于对象间通信.运行时类别信息和动态属性系统. 元对象系统基于三个方面: QObject类:为objects提供了一 ...

  6. 给文本标签UILabel添加长按复制功能

    http://www.111cn.net/sj/iOS/104236.htm http://blog.csdn.net/lrenjun/article/details/12582927 自定义一个可复 ...

  7. 149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  8. 创建PO/SO

    IF P_ZY EQ 'X'."直营订单 调拨单 PERFORM FRM_INIT_PO_HEADER. PERFORM FRM_INIT_PO_ITEM. PERFORM FRM_INIT ...

  9. CRM创建物料FM2

    这是在佛山好帮手时受启发而研究出来的,创建物料,带单位,类型组 经测试....算了,不说了,有什么限制自己测去...今天心情不好... FUNCTION ZLY_CREATE_PRODUCT_UNIT ...

  10. HTML5 视频规范简介

    HTML5 视频规范简介  创建于 2013-02-03, 周日 00:56  作者 白建鹏 HTML 一词是“超文本标记语言”(Hyper-Text Markup Language)的缩写,是用于描 ...