[ 并查集 ] Codechef March Cook-off 2018 Div 1 Chef Chooses a Subset

定义边的权值为连接的两点权值的 and a n d ,那么一个连通块所有点的 and a n d 值等于所有边权的 and a n d 值。
枚举答案的 and a n d 值,再枚举权值包含它的所有边,用并查集求出最大连通块就好了。
复杂度为 O(α3logn) O ( α 3 log ⁡ n )

#include<bits/stdc++.h>
using namespace std;
char nc() {
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
void Read(int& x) {
    char c=nc();
    for(;c<'0'||c>'9';c=nc());
    for(x=0;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-48,c=nc());
}
const int N=100010;
int k,n,m,T,cnt,cur;
int u[N],v[N],f[N],sz[N];
int a[N];
int Res;
long long Ans;
vector<int>g[N];
int Find(int x) {
    return f[x]==x?x:f[x]=Find(f[x]);
}
void Un(int x,int y) {
    if(a[x]!=cur) a[x]=cur,f[x]=x,sz[x]=1;
    if(a[y]!=cur) a[y]=cur,f[y]=y,sz[y]=1;
    x=Find(x);y=Find(y);
    if(x==y) return;
    f[y]=x;sz[x]+=sz[y];
    Res=max(Res,sz[x]);
}
int main() {
    Read(T);
    while(T--) {
        Read(n);
        for(int i=0;i<n;i++) g[i].clear();
        for(int i=1;i<n;i++) {
            Read(u[i]);Read(v[i]);
            g[u[i]&v[i]].push_back(i);
        }
        Ans=n;
        for(int i=1;i<n;i++) {
            ++cur;Res=0;
            for(int j=i;j<n;j=(j+1)|i)
                for(int p=0;p<g[j].size();p++)
                    Un(u[g[j][p]],v[g[j][p]]);
            Ans=max(Ans,1ll*Res*i);
        }
        printf("%lld\n",Ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值