Codeforces Global Round 14 F. Phoenix and Earthquake 思维 + 并查集

93 篇文章 0 订阅
77 篇文章 0 订阅

传送门

文章目录

题意:

给你 n n n个点, m m m条边,限制 x x x,每个点都有沥青 a i a_i ai,定义合并两个点即两点之间有边且 a u + a v ≥ x a_u+a_v\ge x au+avx,合并之后的沥青为 a u + a v − x a_u+a_v-x au+avx,现在让你输出一种选选边方式,选 n − 1 n-1 n1条边使得 n n n个点联通。
n , m < = 3 e 5 , n − 1 ≤ m , 1 ≤ x , a i ≤ 1 e 9 n,m<=3e5,n-1\le m,1\le x,a_i \le1e9 n,m<=3e5,n1m,1x,ai1e9

思路:

首先当 ( n − 1 ) ∗ x > s u m a (n-1)*x>sum_a (n1)x>suma的时候,显然无解,否则一定能构造出一组解。
证明:
( 1 ) (1) (1)当有一个点 a i ≥ x a_i\ge x aix的时候,显然可以将他与任意一个点合并,让后转换成 n − 1 n-1 n1个点的子问题。
( 2 ) (2) (2)当所有点 a i < x a_i<x ai<x的时候,那么一定有两个点 a u + a v ≥ x a_u+a_v\ge x au+avx,否则如果所有点 a u + a v < x a_u+a_v<x au+av<x,那么 a 1 + 2 ∗ ( a 2 + . . . + a n − 1 ) + a n < ( n − 1 ) ∗ x a_1+2*(a_2+...+a_{n-1})+a_n<(n-1)*x a1+2(a2+...+an1)+an<(n1)x,所以 s u m < ( n − 1 ) ∗ x sum<(n-1)*x sum<(n1)x
所以我们每次都选最大的 a i a_i ai,之后将他与跟他相连的且不在他集合中的点合并,用优先队列 + 并查集可以轻松实现。

// Problem: F. Phoenix and Earthquake
// Contest: Codeforces - Codeforces Global Round 14
// URL: https://codeforces.com/contest/1515/problem/F
// Memory Limit: 256 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,m,x;
LL p[N],a[N];
vector<vector<PII>>v(N);

int find(int x)
{
	return x==p[x]? x:p[x]=find(p[x]);
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);

	LL sum=0;
	scanf("%d%d%d",&n,&m,&x);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]),sum+=a[i],p[i]=i;
	for(int i=1;i<=m;i++)
	{
		int a,b; scanf("%d%d",&a,&b);
		v[a].emplace_back(b,i); v[b].emplace_back(a,i);
	}
	if(sum<1ll*(n-1)*x) { puts("NO"); return 0; }
	priority_queue<PII>q;
	for(int i=1;i<=n;i++) q.push({a[i],i});
	puts("YES");
	for(int i=1;i<=n-1;i++)
	{
		PII t=q.top(); q.pop();
		int u=t.Y;
		while(u!=find(u))
		{
			t=q.top(); q.pop();
			u=t.Y;
		}
		while(u==find(v[u].back().X)&&v[u].size()) v[u].pop_back();
		printf("%d\n",v[u].back().Y);
		int vv=find(v[u].back().X);
		a[u]+=a[vv]-x;
		q.push({a[u],u});
		p[vv]=u;
		if(v[u].size()<v[vv].size()) {
			swap(v[u],v[vv]);
		}
		v[u].insert(v[u].end(),v[vv].begin(),v[vv].end());
		v[vv].clear();
	}


	return 0;
}
/*

*/




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值