codeforces292——D. Connected Components(并查集+前缀和优化)

简介: codeforces292——D. Connected Components(并查集+前缀和优化)

原题链接

题意:

20200401134307494.png

思路:

很巧妙的一个题。

如果不断开的话就是简单的并查集计算连通块的问题。

先来考虑一下暴力的做法,去掉[l,r]之间的边后,直接暴力将[1,l-1]和[r+1,m]的边连接起来,计算出连通块的个数,大概就是这样:

while(k--){
        int l,r;
        scanf("%d%d",&l,&r);
        for(int i=1;i<=n;i++) root[i]=i;
        int res=n;
        for(int i=1;i<l;i++){
            int u=a[i],v=b[i];
            u=Find(u),v=Find(v);
            if(u!=v) root[u]=v,res--;
        }
        for(int i=r+1;i<=m;i++){
            int u=a[i],v=b[i];
            u=Find(u),v=Find(v);
            if(u!=v) root[u]=v,res--;
        }
        printf("%d\n",res);
    }

显然很难卡过去这道题。因为每个区间只是在每次询问后不计这个区间的边,也就相当于每次询问都是相互独立的,即任何一段区间的连通块个数和连边情况都是不会变的。这样就可以用前后缀和来维护,每次询问时只需要将[1,l-1]和[r+1,m]区间连接计算连通块个数即可。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
char F[200];
inline void out(I_int x)
{
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0)
    {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
ll ksm(ll a,ll b,ll p)
{
    ll res=1;
    while(b)
    {
        if(b&1)res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int maxn=2e4+10,mod=1e8;
const double PI = atan(1.0)*4;
const double eps=1e-6;
int n,m,x[maxn],y[maxn];
struct node{
    int root[510],cnt;
    void init(){
       for(int i=1;i<=500;i++) root[i]=i;
       cnt=0;
    }
    int Find(int x){
        if(x!=root[x]) root[x]=Find(root[x]);
        return root[x];
    }
    void Union(int x,int y){
        x=Find(x),y=Find(y);
        if(x!=y) root[x]=y,cnt++;
    }
}l[maxn],r[maxn];
int main(){
    n=read(),m=read();
    for(int i=1;i<=m;i++)
        x[i]=read(),y[i]=read();
    l[0].init();
    for(int i=1;i<=m;i++){
        l[i]=l[i-1];
        l[i].Union(x[i],y[i]);
    }
    r[m+1].init();
    for(int i=m;i;i--){
        r[i]=r[i+1];
        r[i].Union(x[i],y[i]);
    }
    int k=read();
    while(k--){
        int ll=read(),rr=read();
        node res=l[ll-1];
        for(int i=1;i<=n;i++)
            res.Union(i,r[rr+1].Find(i));
        cout<<n-res.cnt<<endl;
    }
    return 0;
}

参考:

目录
相关文章
|
8月前
|
人工智能
poj 2299 Ultra-QuickSort 求逆序数 树状数组解法
所谓离散化,我们的了解就是对原数组排序,然后用所在位置的下标代替原数,这样我们就可以把数据范围缩小到1-500000,这个数组是开的下的。
24 0
|
9月前
|
机器学习/深度学习 人工智能 BI
The Great Hero(Codeforces Round #700 (Div. 2))模拟+贪心思想和排序
The Great Hero(Codeforces Round #700 (Div. 2))模拟+贪心思想和排序
39 0
|
10月前
UVa11157 - Dynamic Frog(动态规划)
UVa11157 - Dynamic Frog(动态规划)
38 0
|
机器学习/深度学习 Windows
The 2022 ICPC Asia Regionals Online Contest (I)-D题题解(DFS暴搜+剪枝+打表去重+二分)
时间复杂度:O(227 + nlog(n) + T * log(n)),227是DFS打表的时间,nlog(n)是vertor排序的时间,T*log(n)是询问次数和二分搜答案的时间。(如果算错了,评论或者私信指出谢谢)
129 0
The 2022 ICPC Asia Regionals Online Contest (I)-D题题解(DFS暴搜+剪枝+打表去重+二分)
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
71 0
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
119 0
|
机器学习/深度学习
AtCoder Beginner Contest 215 F - Dist Max 2 (二分 单调队列)
AtCoder Beginner Contest 215 F - Dist Max 2 (二分 单调队列)
111 0
|
机器学习/深度学习 人工智能 算法
CF1446D Frequency Problem(思维 前缀和 根号分治 双指针)
CF1446D Frequency Problem(思维 前缀和 根号分治 双指针)
69 0
AtCoder Beginner Contest 203 Pond(二分+二维前缀和)
大体思路: 二分,将原矩阵根据二分的值变成01矩阵,如果元素值> val 就变为1,否则0 对于k * k 的矩阵,统计区域内元素之和,如果 sum < ⌊k2 / 2⌋ + 1,意味着当前k * k矩阵的中位数小于x,而x是我们的答案(最小中位数), ①sum < ⌊k2 / 2⌋ + 1 情况下x取得太大,r = mid ②反之,x还可能取更小的,l = mid 但是需要注意下l的初始值,当取0 or 1的时候是会wa掉的:
215 0
AtCoder Beginner Contest 203 Pond(二分+二维前缀和)

热门文章

最新文章