A . Total Eclipse 并查集 (逆向加点)

A . Total Eclipse [ 问题 3187 ] [ 讨论 ]
Description
There are n cities and m bidirectional roads in Byteland. These cities are labeled by 1,2,…,n, the brightness of the i-th city is bi.
Magician Sunset wants to play a joke on Byteland by making a total eclipse such that the brightness of every city becomes zero. Sunset can do the following operations for arbitrary number of times:

Select an integer k (1≤k≤n).
Select k distinct cities c1,c2,…,ck (1≤c≤n) such that they are connected with each other. In other words, for every pair of distinct selected cities ci and cj (1≤i<j≤k), if you are at city ci, you can reach city cj without visiting cities not in {c1,c2,…,ck}.
For every selected city ci (1≤i≤k), decrease bci by 1.
Note that Sunset will always choose k with the maximum possible value. Now Sunset is wondering what is the minimum number of operations he needs to do, please write a program to help him.

Input
The first line of the input contains a single integer T (1≤T≤10), the number of test cases.
For each case, the first line of the input contains two integers n and m (1≤n≤100000, 1≤m≤200000), denoting the number of cities and the number of roads.
The second line of the input contains n integers b1,b2,…,bn (1≤bi≤109), denoting the brightness of each city.
Each of the following m lines contains two integers ui and vi (1≤ui,vi≤n,ui≠vi), denoting an bidirectional road between the ui-th city and the vi-th city. Note that there may be multiple roads between the same pair of cities.

Output
For each test case, output a single line containing an integer, the minimum number of operations.

Samples
Input 复制
1
3 2
3 2 3
1 2
2 3
Output
4

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
struct node{
	int id,b;
}city[N];
vector<int>v[N];
bool cmp(node a,node b)
{
	return a.b>b.b;
}
int vis[N];
int p[N];
int get(int x)
{
	if(x==p[x])
	return p[x];
	return p[x]=get(p[x]);
}
int main()
{
	int t,n,m;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		{
			cin>>city[i].b;
			city[i].id=i;
			p[i]=i;
			vis[i]=0;
			v[i].clear();
		}
		sort(city+1,city+1+n,cmp);
		for(int i=1;i<=m;i++)
		{
			int u,s;
			cin>>u>>s;
			v[u].push_back(s);
			v[s].push_back(u);
		}
		vis[city[1].id]=1;
		ll cnt=1,sum=0;
		for(int i=2;i<=n;i++)
		{
			sum+=cnt*(city[i-1].b-city[i].b);
			cnt++;
			for(auto it:v[city[i].id])
			{
				if(!vis[it])continue;
				int x=get(it),y=get(city[i].id);
				if(x!=y)
				{
					p[x]=y;
					cnt--;
				}
			}
			vis[city[i].id]=1;
		}
		sum+=cnt*city[n].b;
		cout<<sum<<endl;
	}
	return 0;
}

为什么需要每个点的贡献乘目前的连通块的数目:
我是这么想的,因为我们是从大到小加点构造出这个图,所以其他连通块的最小点的大小是大于当前判断的点的,而在这这个点的贡献乘连通块(假设联通块数为2)的数目,那么除了本点的贡献他还处理了另一连通块中比本点大的第一个点的贡献。
大佬的图,,,帮助理解,实在是tql在这里插入图片描述
像是加完了7此时7的贡献是分成了好及部分来计算的(7-5)*1是一部分(5-4)*2中的一个是一部分(4-4)*3中也有一部分。所以这就是加起来就是完整的贡献。

关于逆向加点

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值