第一题: Total Eclipse
题目:
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≤ci≤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.
思路
我们先按照点的大小从大到小进行排序,然后每次就填点,查询它所连的点是否被加入,如果加入了,那么就搜寻一下查询点的祖先是否是这个点,如果不是,就让查询点的祖先的父亲变成这个点,这样就会构成有根树。
然后我们就会发现如果某个点成为最小值,他的父亲已经进行了x次操作,那么我们的答案就是这个点的值减去他父亲节点的值。
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
const int N=1e5+7,M=4e5+8;
int n,m;
int h[N],e[M],ne[M],idx;