欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[BZOJ3872][Poi2014]Ant colony

程序员文章站 2022-07-13 12:41:39
...

[BZOJ3872][Poi2014]Ant colony

试题描述

There is an entrance to the ant hill in every chamber with only one corridor leading into (or out of) it. At each entry, there are g groups of m1,m2,...,mg ants respectively. These groups will enter the ant hill one after another, each successive group entering once there are no ants inside. Inside the hill, the ants explore it in the following way:
Upon entering a chamber with d outgoing corridors yet unexplored by the group, the group divides into d groups of equal size. Each newly created group follows one of the d corridors. If d=0, then the group exits the ant hill.
If the ants cannot divide into equal groups, then the stronger ants eat the weaker until a perfect division is possible. Note that such a division is always possible since eventually the number of ants drops down to zero. Nothing can stop the ants from allowing divisibility - in particular, an ant can eat itself, and the last one remaining will do so if the group is smaller than d.
The following figure depicts m ants upon entering a chamber with three outgoing unexplored corridors, dividing themselves into three (equal) groups of floor(m/3) ants each.
A hungry anteater dug into one of the corridors and can now eat all the ants passing through it. However, just like the ants, the anteater is very picky when it comes to numbers. It will devour a passing group if and only if it consists of exactly k ants. We want to know how many ants the anteater will eat.
给定一棵有n个节点的树。在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁。这些蚂蚁会相继进入树中,而且要保证每一时刻每个节点最多只有一群蚂蚁。这些蚂蚁会按以下方式前进:
·在即将离开某个度数为d+1的点时,该群蚂蚁有d个方向还没有走过,这群蚂蚁就会分裂成d群,每群数量都相等。如果d=0,那么蚂蚁会离开这棵树。
·如果蚂蚁不能等分,那么蚂蚁之间会互相吞噬,直到可以等分为止,即一群蚂蚁有m只,要分成d组,每组将会有floor(m/d)只,如下图。
 
[BZOJ3872][Poi2014]Ant colony
一只饥饿的食蚁兽埋伏在一条边上,如果有一群蚂蚁通过这条边,并且数量恰为k只,它就会吞掉这群蚂蚁。请计算一共有多少只蚂蚁会被吞掉。

输入

The first line of the standard input contains three integers n, g, k (2<=n,g<=1000000, 1<=k<=10^9), separated by single spaces. These specify the number of chambers, the number of ant groups and the number of ants the anteater devours at once. The chambers are numbered from 1 to n.
The second line contains g integers m[1],m[2],...,m[g](1<=m[i]<=10^9), separated by single spaces, where m[i] gives the number of ants in the i-th group at every entrance to the ant hill. The n-1 lines that follow describe the corridors within the ant hill; the i-th such line contains two integers a[i],b[i] (1<=a[i],b[i]<=n), separated by a single space, that indicate that the chambers no.a[i] and b[i] are linked by a corridor. The anteater has dug into the corridor that appears first on input.
第一行包含三个整数n,g,k,表示点数、蚂蚁群数以及k。
第二行包含g个整数m[1],m[2],...,m[g],表示每群蚂蚁中蚂蚁的数量。
接下来n-1行每行两个整数,表示一条边,食蚁兽埋伏在输入的第一条边上。

输出

Your program should print to the standard output a single line containing a single integer: the number of ants eaten by the anteater.
一个整数,即食蚁兽能吃掉的蚂蚁的数量。

输入示例

7 5 3
3 4 1 9 11
1 2
1 4
4 3
4 5
4 6
6 7

输出示例

21

数据规模及约定

见“输入

题解

因为只有一只食蚁兽,所以可以从它所在的边开始 BFS,求出每条边经过的蚂蚁上限和下限,扩展到叶节点后二分找有几群蚂蚁在下界和上界范围内累计答案即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;

const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
	if(Head == Tail) {
		int l = fread(buffer, 1, BufferSize, stdin);
		Tail = (Head = buffer) + l;
	}
	return *Head++;
}
int read() {
	int x = 0, f = 1; char c = Getchar();
	while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
	while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
	return x * f;
}

#define maxn 1000010
#define maxm 2000010
#define LL long long
int n, g, k, m, head[maxn], next[maxm], to[maxm], ind[maxn];
LL ants[maxn], mxa;

void AddEdge(int a, int b) {
	to[++m] = b; next[m] = head[a]; head[a] = m;
	swap(a, b);
	to[++m] = b; next[m] = head[a]; head[a] = m;
	return ;
}

bool vis[maxn];
int Q[maxn], hd, tl;
LL low[maxn], upp[maxn];
void BFS(int s) {
	hd = tl = 0; Q[++tl] = s;
	while(hd < tl) {
		int u = Q[++hd];
//		printf("%d %lld %lld\n", u, low[u], upp[u]);
		for(int e = head[u]; e; e = next[e]) if(!vis[to[e]]) {
			Q[++tl] = to[e]; vis[to[e]] = 1;
			if(low[u] * ((LL)ind[u] - 1) > mxa) low[to[e]] = mxa + 1;
			else low[to[e]] = low[u] * ((LL)ind[u] - 1);
			if(upp[u] * ((LL)ind[u] - 1) + ind[u] - 2 > mxa) upp[to[e]] = mxa + 1;
			else upp[to[e]] = upp[u] * ((LL)ind[u] - 1) + ind[u] - 2;
		}
	}
	return ;
}

int main() {
//	freopen("data.in", "r", stdin);
//	freopen("data.out", "w", stdout);
	n = read(); g = read(); k = read();
	for(int i = 1; i <= g; i++) ants[i] = read(), mxa = max(mxa, ants[i]);
	
	int sta, stb;
	for(int i = 1; i < n; i++) {
		int a = read(), b = read();
		AddEdge(a, b);
		ind[a]++; ind[b]++;
		if(i == 1) sta = a, stb = b;
	}
	vis[sta] = vis[stb] = 1;
	low[sta] = low[stb] = upp[sta] = upp[stb] = (LL)k;
	BFS(sta);
	BFS(stb);
	
	sort(ants + 1, ants + g + 1);
	LL ans = 0;
//	for(int i = 1; i <= n; i++) if(ind[i] == 1) printf("%d: %lld %lld\n", i, low[i], upp[i]);
	for(int i = 1; i <= n; i++) if(ind[i] == 1) {
		int l = lower_bound(ants + 1, ants + g + 1, low[i]) - ants;
		int r = upper_bound(ants + 1, ants + g + 1, upp[i]) - ants;
		if(r > g || ants[r] > upp[i]) r--;
		ans += (LL)(r - l + 1);
//		printf("leaves: %d %d %d\n", i, l, r);
	}
	ans *= (LL)k;
	printf("%lld", ans);
	
	return 0;
}