【图论杂题】:K.dog [最小生成树巧妙运用]

题目:

团队链接:dog

题面:

有一个长度为n的未知的 01 01 01序列

可以询问一个区间 [ a , b ] [a,b] [a,b]内数的和的奇偶性需要付出代价 C ( a , b ) C(a,b) C(a,b),

P . S . : C ( a , b ) 已知 P.S.:C(a,b)\text{已知} P.S.:C(a,b)已知

给出任意区间的询问代价

求能构解出该 01 01 01序列的最小代价

范围: n &lt; = 2000 n&lt;=2000 n<=2000

样例:

输入:

4
5 7 6 1 9 11 10 4 20 13

输出:

16
题解:

这个题嘛,,,看样子很玄乎,可是,,它就是个板子题,而且是很普通的板子题,,,因为它把所有的查询代价都给你了,所以你,,就可以连边了!!把i和j+1进行边即可,边权就是代价,这时候你就会发现答案就是最小生成树了,,

代码:

#include<bits/stdc++.h>
#define LL long long
using namespace std;
inline LL read()
{
	LL s=0,w=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch<='9'&&ch>='0')s=s*10+ch-'0',ch=getchar();
	return s*w;
}
const int sea=1e8+7;
struct see{int x,ver;LL edge;}e[sea];
int n,tot,cnt,fa[sea];
LL ans;
bool cmp(see x,see y){return x.edge<y.edge;}
void add(int x,int y,LL z){e[++tot].ver=y;e[tot].x=x;e[tot].edge=z;}
int get(int x){if(x==fa[x])return x;else return fa[x]=get(fa[x]);}
int main()
{
	n=read();
	for(int i=1;i<=n+1;i++) fa[i]=i;
	for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) add(i,j+1,read());
	sort(e+1,e+tot+1,cmp);
	for(int i=1;i<=tot;i++)
	{
		int x=get(e[i].x),y=get(e[i].ver);
		if(x==y) continue;
		ans+=e[i].edge;
		fa[x]=y;cnt++;
		if(cnt==n) break;
	}
	printf("%lld\n",ans);
	return 0;
}

Continue……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值