http://codeforces.com/problemset/problem/348/B

注意到如果顶点的数值确定了,那么它分下去的个数也就确定了,那么可以暴力枚举顶点的数值。

顶点的数值是和LCM相隔的,LCM就是,比如1有三个子节点,那么1的数值起码都是3的倍数,不然不能整除。

同理,1有三个儿子2、3、4、,如果3有三个儿子,那么1就要起码是9的倍数了,因为需要分给3的时候至少是3.

所以算出整颗树的LCM,叶子节点LCM是1,其他的LCM = lcm(所有子节点) * son[cur]

算出这个后,就可以暴力枚举顶点1的值了,每次枚举都dfs一次,有点暴力1700ms,学学正解先。

注意一点的就是LCM会爆LL,这个时候直接输出sum即可

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e5 + ;
int a[maxn];
LL sum;
struct Node {
int u, v, tonext;
}e[maxn * ];
int first[maxn], num;
void addEdge(int u, int v) {
++num;
e[num].u = u, e[num].v = v, e[num].tonext = first[u];
first[u] = num;
}
int vis[maxn], DFN = ;
int son[maxn];
void findSon(int cur) {
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (vis[v] == DFN) continue;
vis[v] = DFN;
son[cur]++;
findSon(v);
}
}
LL ans = 1e18L, tans;
LL LCM;
LL lcm(LL a, LL b) {
return a / __gcd(a, b) * b;
}
bool flag;
void dfs(int cur, LL val) {
if (flag) return;
if (son[cur] == ) {
if (val > a[cur]) {
flag = true;
return;
}
tans += a[cur] - val;
return;
}
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (vis[v] == DFN) continue;
vis[v] = DFN;
if (val % son[cur] != ) {
flag = true;
return;
}
dfs(v, val / son[cur]);
}
}
LL calc(int cur) {
if (son[cur] == ) return ;
LL thisLCM = ;
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (vis[v] == DFN) continue;
vis[v] = DFN;
thisLCM = lcm(thisLCM, calc(v));
}
if (thisLCM > sum / son[cur]) {
printf("%I64d\n", sum);
exit();
}
return son[cur] * thisLCM;
}
void work() {
int n;
scanf("%d", &n);
LCM = ;
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
sum += a[i];
}
for (int i = ; i <= n - ; ++i) {
int u, v;
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
vis[] = DFN;
findSon();
++DFN;
vis[] = DFN;
LCM = calc();
// cout << LCM << endl;
ans = sum;
sum /= LCM;
sum *= LCM;
for (LL i = sum; i >= LCM; i -= LCM) {
++DFN, tans = ;
flag = false;
// dfs(1, 24);
vis[] = DFN;
dfs(, i);
if (flag) continue;
printf("%I64d\n", ans - i);
return;
}
printf("%I64d\n", ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

因为已经知道根节点的值是LCM、2 * LCM、3 * LCM、..... sum中合法的最大的哪一个,那么可以二分答案。

一开始想把她们全部存入vector再二分,但是发现不用,直接二分id就行,就是二分LCM上面的那个倍数就可以了。

500ms

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e5 + ;
int a[maxn];
LL sum;
struct Node {
int u, v, tonext;
}e[maxn * ];
int first[maxn], num;
void addEdge(int u, int v) {
++num;
e[num].u = u, e[num].v = v, e[num].tonext = first[u];
first[u] = num;
}
int vis[maxn], DFN = ;
int son[maxn];
void findSon(int cur) {
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (vis[v] == DFN) continue;
vis[v] = DFN;
son[cur]++;
findSon(v);
}
}
LL ans = 1e18L, tans;
LL LCM;
LL lcm(LL a, LL b) {
return a / __gcd(a, b) * b;
}
bool flag;
void dfs(int cur, LL val) {
if (flag) return;
if (son[cur] == ) {
if (val > a[cur]) {
flag = true;
return;
}
tans += a[cur] - val;
return;
}
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (vis[v] == DFN) continue;
vis[v] = DFN;
if (val % son[cur] != ) {
flag = true;
return;
}
dfs(v, val / son[cur]);
}
}
LL calc(int cur) {
if (son[cur] == ) return ;
LL thisLCM = ;
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (vis[v] == DFN) continue;
vis[v] = DFN;
thisLCM = lcm(thisLCM, calc(v));
}
if (thisLCM > sum / son[cur]) {
printf("%I64d\n", sum);
exit();
}
return son[cur] * thisLCM;
}
void work() {
int n;
scanf("%d", &n);
LCM = ;
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
sum += a[i];
}
for (int i = ; i <= n - ; ++i) {
int u, v;
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
vis[] = DFN;
findSon();
++DFN;
vis[] = DFN;
LCM = calc();
// cout << LCM << endl;
ans = sum;
sum /= LCM;
sum *= LCM;
LL be = , en = sum / LCM;
while (be <= en) {
LL mid = ((be + en) >> );
++DFN, flag = false;
vis[] = DFN;
dfs(, mid * LCM);
if (flag) {
en = mid - ;
} else be = mid + ;
}
cout << ans - LCM * en << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

B. Apple Tree 暴力 + 数学的更多相关文章

  1. cf202-div 1-B - Apple Tree:搜索,数论,树的遍历

      http://codeforces.com/contest/348/problem/B   B. Apple Tree time limit per test 2 seconds memory l ...

  2. POJ 2486 Apple Tree

    好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...

  3. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  4. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  5. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  6. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  7. 【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C Description I’ve bought an or ...

  8. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  9. URAL 1018 Binary Apple Tree(树DP)

    Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a bina ...

随机推荐

  1. Android studio 添加assets文件夹

    我们知道Eclipse创建的工程默认是有个assets文件夹的,但是Android studio默认没有帮我们创建,那么我们就自己创建一个就好啦. (1)手动创建 在项目的顶部有个下拉,默认选择的是A ...

  2. phpcms 内容模块PC标签调用

    PHPcms 调用命令的基本格式: 开始:{pc:content action="模块操作名" catid="调用栏目ID" num="数据调用数量& ...

  3. html5--3.10 input元素(9)

    html5--3.10 input元素(9) 学习要点 input元素及其属性 input元素 用来设置表单中的内容项,比如输入内容的文本框,按钮等 不仅可以布置在表单中,也可以在表单之外的元素使用 ...

  4. tcpdump 探测器分析

    注:默认情况下,tcpdump临听它遇见的第一个网络接口,如果它选择了错误的接口,可以-i标志强行指定接口,如果DNS不能用,或者只是不希望tcpdump进行名字查找,请使用-n选项,这个选项(-n) ...

  5. EF之贪婪加载和延迟加载

    这篇文章将讨论查询结果的控制 在使用EF(Entity Framework)的过程中,很多时候我们会进行查询的操作,因此知道哪些数据会被加载到内存当中就至关重要.在多多的情况下,你可能并并不需要加载全 ...

  6. json : json数据解析(一)

    在项目中经常用到json格式的数据转换与解析,先前写过一些小例子,现在整理下,以备后用和帮助后来者. 言归正传: 使用到的jar包 :json-lib-2.4-jdk15.jar,当然你也可以用自己版 ...

  7. Android开发技巧--引用另一个工程

    现在已经有了一个Android工程A.我们想扩展A的功能,但是不想在A的基础上做开发,于是新建了另外一个Android工程B,想在B中引用A. 1:把工程A做成纯Jar包,这样其他的工程就可以直接引用 ...

  8. httpd基础

    hpptd http服务器应用 http服务器程序 httpd apache nginx lighttpd 应用程序服务器 IIS .asp tomcat .jsp jetty 开源的servlet容 ...

  9. Win10的Hyper-V虚拟机上安装Ubuntu后显示分辨率问题

    分辨率问题 Hyper-V中安装好Ubuntu后,虚拟机显示无法全屏,即使最大化窗口,也只能显示固定大小.即使你尝试更改虚拟机内的屏幕分辨率你也只会发现分辨率选项就只有一个. 解决方法 1.在虚拟机U ...

  10. PYTHON实现DFS算法

    class Vertice: def __init__(self,index): self.no = index self.color = 0 # 0:white 1 gray 2 black sel ...