AtCoder Regular Contest 093 E - Bichrome Spanning Tree 最小生成树

题意

给一个n个点m条边的无向图,边有边权。每条边可以涂成黑色或白色。问有多少种涂色方案使得图中存在一棵即有黑边又有白边的生成树,且满足上述条件的最小生成树的边权和恰好为X。
n<=1000,m<=2000 n <= 1000 , m <= 2000

分析

val[i] v a l [ i ] 表示一定包含第i条边的最小生成树的边权和。
对于所有 val[i]<X v a l [ i ] < X 的边,它们的颜色一定是相同的,否则就会出现一棵边权和小于X的生成树。
对于所有 val[i]=X v a l [ i ] = X 的边,它们至少存在一条边与所有最小生成树的边的颜色不同。
对于所有 val[i]>X v a l [ i ] > X 的边,它们对答案没有影响,所以可以随便涂。
剩下的随便做就好了。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long LL;

const int N=1005;
const int MOD=1000000007;
const int inf=1000000000;

int n,m,cnt,last[N],fa[N],dep[N],val[N],f[N];
struct edge{int to,next,w;}e[N*2];
struct data{int x,y,w,tag;}a[N*2];
LL X;

int read()
{
    int 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;
}

int find(int x)
{
    if (f[x]==x) return x;
    else return f[x]=find(f[x]);
}

bool cmp(data a,data b)
{
    return a.w<b.w;
}

int ksm(int x,int y)
{
    int ans=1;
    while (y)
    {
        if (y&1) ans=(LL)ans*x%MOD;
        x=(LL)x*x%MOD;y>>=1;
    }
    return ans;
}

void addedge(int u,int v,int w)
{
    e[++cnt].to=v;e[cnt].w=w;e[cnt].next=last[u];last[u]=cnt;
    e[++cnt].to=u;e[cnt].w=w;e[cnt].next=last[v];last[v]=cnt;
}

void dfs(int x)
{
    dep[x]=dep[fa[x]]+1;
    for (int i=last[x];i;i=e[i].next)
    {
        if (e[i].to==fa[x]) continue;
        val[e[i].to]=e[i].w;
        fa[e[i].to]=x;
        dfs(e[i].to);
    }
}

int get_mx(int x,int y)
{
    int mx=0;
    if (dep[x]<dep[y]) swap(x,y);
    while (dep[x]>dep[y]) mx=max(mx,val[x]),x=fa[x];
    while (x!=y) mx=max(mx,max(val[x],val[y])),x=fa[x],y=fa[y];
    return mx;
}

int main()
{
    n=read();m=read();scanf("%lld",&X);
    for (int i=1;i<=m;i++) a[i].x=read(),a[i].y=read(),a[i].w=read();
    for (int i=1;i<=n;i++) f[i]=i;
    sort(a+1,a+m+1,cmp);
    LL tot=0;
    for (int i=1;i<=m;i++)
    {
        int x=find(a[i].x),y=find(a[i].y);
        if (x!=y) f[x]=y,addedge(a[i].x,a[i].y,a[i].w),tot+=a[i].w;
    }
    dfs(1);
    int s1=0,s2=1;
    for (int i=2;i<=m;i++)
    {
        int mx=get_mx(a[i].x,a[i].y);
        if (tot-mx+a[i].w==X) s1++;
        else if (tot-mx+a[i].w>X) s2++;
    }
    printf("%d",(LL)(ksm(2,s1)-1)*ksm(2,s2)%MOD);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值