传送门
文章目录
- 题意:
- 思路:
题意:
给你nnn个点,mmm条边,限制xxx,每个点都有沥青aia_iai,定义合并两个点即两点之间有边且au+av≥xa_u+a_v\ge xau+av≥x,合并之后的沥青为au+av−xa_u+a_v-xau+av−x,现在让你输出一种选选边方式,选n−1n-1n−1条边使得nnn个点联通。
n,m<=3e5,n−1≤m,1≤x,ai≤1e9n,m<=3e5,n-1\le m,1\le x,a_i \le1e9n,m<=3e5,n−1≤m,1≤x,ai≤1e9
思路:
首先当(n−1)∗x>suma(n-1)*x>sum_a(n−1)∗x>suma的时候,显然无解,否则一定能构造出一组解。
证明:
(1)(1)(1)当有一个点ai≥xa_i\ge xai≥x的时候,显然可以将他与任意一个点合并,让后转换成n−1n-1n−1个点的子问题。
(2)(2)(2)当所有点ai<xa_i<xai<x的时候,那么一定有两个点au+av≥xa_u+a_v\ge xau+av≥x,否则如果所有点au+av<xa_u+a_v<xau+av<x,那么a1+2∗(a2+...+an−1)+an<(n−1)∗xa_1+2*(a_2+...+a_{n-1})+a_n<(n-1)*xa1+2∗(a2+...+an−1)+an<(n−1)∗x,所以sum<(n−1)∗xsum<(n-1)*xsum<(n−1)∗x。
所以我们每次都选最大的aia_iai,之后将他与跟他相连的且不在他集合中的点合并,用优先队列 + 并查集可以轻松实现。
// 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;
}
/**/