CodeForces 348B Apple Tree

这题的解法有点神啊。。

参考题解

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6
 7 typedef long long LL;
 8 const LL INF = (1LL << 60);
 9
10 const int maxn = 200000 + 10;
11 int n;
12 LL val[maxn];
13 vector<int> G[maxn];
14
15 LL gcd(LL a, LL b) { return !b ? a : gcd(b, a % b); }
16 LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }
17
18 LL L = INF, M = 1, sum;
19
20 void dfs(int u, int fa, LL d)
21 {
22     M = lcm(M, d);
23     if(d > sum || d < 0) { printf("%I64d\n", sum); exit(0); }
24     if(u > 1 && G[u].size() == 1) L = min(L, val[u] * d);
25     for(int v : G[u]) if(v != fa)
26         dfs(v, u, d * (G[u].size() - (u == 1 ? 0 : 1)));
27 }
28
29 int main()
30 {
31     scanf("%d", &n);
32     for(int i = 1; i <= n; i++) { scanf("%I64d", val + i); sum += val[i]; }
33     for(int i = 1; i < n; i++)
34     {
35         int u, v; scanf("%d%d", &u, &v);
36         G[u].push_back(v); G[v].push_back(u);
37     }
38
39     dfs(1, 0, 1);
40     printf("%I64d\n", sum - L / M * M);
41
42     return 0;
43 }

代码君

时间: 2024-07-29 08:32:36

CodeForces 348B Apple Tree的相关文章

Codeforces Round #417 (Div. 2) E. Sagheer and Apple Tree(树上Nim)

题目链接:Codeforces Round #417 (Div. 2) E. Sagheer and Apple Tree 题意: 给你一棵树,每个节点有a[i]个苹果,有两个人要在这个树上玩游戏. 两个人轮流操作,谁不能操作谁就输了. 这个树有一个特性:叶子到根的距离的奇偶性相同. 每次操作可以选一个节点i,和一个数x,x小于当前节点i的苹果数. 对于节点i,如果是叶子节点,就将这x个苹果吃掉. 如果是非叶子节点,就将这x个苹果移向节点i的任意儿子节点. 现在第二个操作的人要交换两个节点的苹果

(博弈\sg) Codeforces Round #417 (Div. 2) E Sagheer and Apple Tree

Sagheer is playing a game with his best friend Soliman. He brought a tree with n nodes numbered from 1 to n and rooted at node 1. The i-th node has ai apples. This tree has a special property: the lengths of all paths from the root to any leaf have t

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

http://codeforces.com/contest/348/problem/B B. Apple Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a rooted tree with n vertices. In each leaf vertex there's a single

POJ3321 Apple Tree

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26989   Accepted: 8004 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been

timus 1018. Binary Apple Tree

1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enu

poj 2486 a apple tree

题意:n节点的树,从1开始走,总共v步,每个点都有一个价值,求可以获得的最大价值 分析:这个显然可以走回来,那么就加一维表示是否走回祖先 dp[u][i][j]表示从u为根节点的子树,往下走i步,j=0表示不走回来,j=1表示走回来 那么可以得到状态转移方程,不走回来的可能会影响走回来的,如果先算不会来的,那么一个节点在不走回来算一次 在走回来又算一次,所以先算走回来的就可以避免 dp[u][i][0]=max(dp[u][i][0],dp[u][i-j][1]+dp[v][j-1][0]);

ural 1018 Binary Apple Tree

1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enu

2014 Multi-University Training Contest 6 Apple Tree(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 Apple Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 176    Accepted Submission(s): 120 Problem Description I've bought an orchard an

HDU 4925 Apple Tree(推理)

HDU 4925 Apple Tree 题目链接 题意:给一个m*n矩阵种树,每个位置可以选择种树或者施肥,如果种上去的位置就不能施肥,如果施肥则能让周围果树产量乘2,问最大收益 思路:推理得到肯定是果树和肥料交叉种好,类似国际象棋棋盘,黑的种,白的施肥,由于格子数不多,直接去枚举每个位置即可.如果题目格子数多的话,其实也可以推出公式一步得到答案 代码: #include <cstdio> #include <cstring> const int d[4][2] = {{0, 1}