URAL 1671 Anansi's Cobweb (并查集)

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

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

#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,0,sizeof(build));
        for(int i=1; i<=m; ++i)
            scanf("%d%d",&edge[i].x,&edge[i].y);
        int q;
        scanf("%d",&q);
        for(int i=1; i<=q; ++i)
        {
            scanf("%d",&query[i]);
            build[query[i]]=true;
        }
        for(int i=1; i<=n; ++i)
            parent[i]=i;
        int cnt=0;
        for(int i=1; i<=m; ++i)
            if(!build[i])
            {
                int fa=findset(edge[i].x),fb=findset(edge[i].y);
                if(fa!=fb)
                    parent[fa]=fb;
            }
        for(int i=1; i<=n; ++i)
            if(findset(i)==i) cnt++;
        for(int i=q; i>=1; --i)
        {
            ans[i]=cnt;
            int fa=findset(edge[query[i]].x),fb=findset(edge[query[i]].y);
            if(fa!=fb)
            {
                parent[fa]=fb;
                cnt--;
            }
        }
        for(int i=1; i<=q; ++i)
            if(i==1) printf("%d",ans[i]);
            else printf(" %d",ans[i]);
        printf("\n");
    }
    return 0;
}

URAL 1671 Anansi's Cobweb (并查集)

时间: 2025-01-08 15:18:33

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

URAL 1982. Electrification Plan(并查集)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1982 Some country has n cities. The government has decided to electrify all these cities. At first, power stations in k different cities were built. The other cities should be connected with the power

URAL - 1966 - Cycling Roads(并查集 + 判线段相交)

题意:n 个点,m 条边(1 ≤ m < n ≤ 200),问所有点是否连通(两条边相交,则该 4 点连通). 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1966 -->>对于每条边,边上的两端点并入集合,枚举边与边,判断他们是否相交,是的话各点并入集合,最后看集合内元素的个数是否为n.. #include <cstdio> #include <cmath> const int MAXN = 200

URAL 1682 Crazy Professor (并查集)

[题目链接] http://acm.timus.ru/problem.aspx?space=1&num=1682 [题目大意] 给出k,从1开始不断地加一并把这个数写在黑板上,如果写上的数字和之前的数字满足 (a+b*b)%k=0或者(b+a*a)%k=0就在他们之间连一条线,如果黑板上出现环就结束,问能写几个数 [题解] 我们发现写到2k-1的时候,就一定会产生一个环,所以我们只要枚举数字 往满足要求的地方连边,判断是否出现环即可 [代码] #include <cstdio> #in

URAL1671 Anansi&#39;s Cobweb(离线做 + 并查集)

传送门 大意:给出一个无向图,删除Q条边,每删除一次就询问一次目前的连通块的数目. 思路:离线搞, 把删边转换为加边,每加一次边,若两个顶点不连通就用并查集把着这两个连通块合并. 代码: #include<cstdio> #include<cstring> #include<algorithm> #define MAXN 100005 using namespace std; int n, m, q; int s[MAXN], t[MAXN]; int ban[MAXN

Pilot Work Experience (URAL 1888 并查集+floyd)

Pilot Work Experience Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Leonid had n Oceanic Airlines flights during his business trip. He studied the latest issue of the monthly on-board magazine o

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

Penguin-Avia Time limit: 1.0 secondMemory limit: 64 MB The Penguin-Avia airline, along with other Antarctic airlines, experiences financial difficulties because of the world's economic crisis. People of Antarctica economize on flights and use trains

Anansi&#39;s Cobweb

Anansi's Cobweb Time limit: 1.0 secondMemory limit: 64 MB Usatiy-Polosatiy XIII decided to destroy Anansi's home — his cobweb. The cobweb consists of Nnodes, some of which are connected by threads. Let us say that two nodes belong to the same piece i

51nod 1204 Parity(并查集应用)

1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串(例如其中的第3到第5个数字)问他,该子串中包含了奇数个还是偶数个1,他会回答你的问题,然后你可以继续提问......你怀疑朋友的答案可能有错,或说同他之前的答案相互矛盾,例如:1 - 2 奇数,3 - 4 奇数,那么可以确定1 - 4 一定是偶数,如果你的朋友回答是奇数,就产生了矛盾.给出所有你朋友的

CodeForces 745C Hongcow Builds A Nation 并查集

题意: 给了你n个城市 m条边 k个政府 每个政府管辖的区域内不能和其他政府的区域有相连 即政府之间不存在路径 问你在维护这种关系的同时 最多再加多少条边 思路: 先找出来每个联通块 再找出来没有归属的孤立的点 把他们都放到最大的联通块里 然后每个联通块之间的点两两连边是n*(n-1)/2条边 最后算出来的ans-m就好了 (看别人的博客学了一个max_element 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a