odds

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 250    Accepted Submission(s): 72

Problem Description
度度熊有一棵 N 个节点 (node) 的有根树 (rooted tree),树上的每条边 (edge) 都有一个整数的权重,对于每一个非叶的节点 (non-leaf node),通往子节点 (child) 的所有边的权重总和为 2×105。

考虑以下在树上行走的随机过程:

1. 起始位置在根节点。
2. 如果现在位置在任何一个叶节点 (leaf node) 上,则结束。
3. 令现在所在的节点通往子节点i的边为 ei,其权重为 vi。
4. 定义 P(ei) 为选择到边 ei 的概率,以 P(ei)=vi/(2×105) 的概率分布随机选择一条边,移动至对应的子节点。
5. 跳至第 2. 步骤。

接下来我们定义以下的操作:

1. 选定一个非叶节点
2. 将这个节点所有通往子节点的边的权重随意重新排序,亦即可以无限次地交换彼此之间的权重,但不能改变权重的大小。

给定一个固定的 X 值,请对于每个叶节点分开考虑以下问题:

如果能进行至多 X 次的操作,那到达这个叶节点的概率最大可以多大呢?

 
Input
输入的第一行有一个正整数 T,代表接下来有几组测试数据。

对于每组测试数据:
第一行有两个整数 N, X。
接下来的 N−1 行中,每行有三个整数 a, b 以及 v,代表这颗树上节点 a 至节点 b 有一条权重为 v 的边,同时,保证 b 为 a 的子节点。
节点的编号由 0 至 N−1,节点 0 为树根。

* 2≤N≤105
* 0≤X≤N
* 0≤a,b<N
* 0≤v≤ 2×105
* 节点 b 为 a 的子节点
* 对于非叶节点,其所有通往子节点的边的权重总合為 2×105
* 1≤T≤21
* 至多 2 组测试数据中的 N>2000

 
Output
对于每一组测试数据,对于每一个叶节点,请依序在一行中输出一个整数 V,如果这个节点的答案的最简有理数为 P/Q,则 V=P×Q−1 mod 109+7。
请按照节点编号的顺序由小至大输出。
两组测试数据间请不要输出多余的空白行。

在本题中,对于正整数 Q,定义Q−1 的值为一正整数 Q′ 滿足 Q′<109+7 且 Q×Q′mod 109+7 = 1。

 
Sample Input
1
11 2
0 1 100
0 2 199700
0 3 200
1 4 120000
1 5 80000
3 6 100000
3 7 100000
7 8 90000
7 9 90000
7 10 20000
 
Sample Output
714500006
628700005
628700005
357250003
110762501
110762501
110762501
 
Source
 
Recommend
chendu   |   We have carefully selected several similar problems for you:  6408 6407 6406 6405 6404 
 

Statistic | Submit | Discuss | Note

析:这个题目,我差不多是暴力加了点剪枝过去的,但是速度挺快的,在比赛时才用了100ms多,说一下我的理解,如果某一个叶子的最大概率,相信很简单就能求出来,只要把从根结点到该叶子结点的所有的结点的值和最大值保存下来,然后按比值排个序就能够知道哪 x 个要重排,这是一个叶子的求法,但是如果很多呢,我们可以进行计算,叶子多了那么树的深度就会降低,也就是从根结点到叶子的结点数量会减少,但是肯定有一个最大值,两者都比较大,这个值不知道后台是什么数据。然后如果对每个结点都进行回溯的话,不幸的是,这样肯定超时,我们可以考虑记录一条路径,也可以维护一个栈,这样到每个叶子以前的结点都记录下来了,只要在叶子结点再重新赋值,再排序一次就好了,但是还是超时了。我考虑到可以把当前和重排后的最大值相等的结点去掉,这个去掉我一开始只是没有加入排序,这样的话,每次排序会少一些结点,并且如果某个叶结点的深度不超过 x ,那么这个值就可以直接选每个结点的最大值。这样还是超时了。最后一个优化就是把当前和重排后的最大值相等的结点在前面就计算出来,不是留到最后再计算,放到后面再计算虽然不用排序,但是还是算,同时更新不是树的深度小于等于 x 就加最大值,而是根据路径里的结点数量来确定。就这样我就 A 掉这个题目,我觉得这肯定不是正解,官方题解好多繁体字就没有看。。。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int maxm = 1e6 + 10;
const LL mod = 1000000007LL;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x; scanf("%d", &x); return x; } const LL inv = 714285005LL; LL fast_pow(LL a, int n){
LL res = 1;
while(n){
if(n&1) res = res * a % mod;
n >>= 1;
a = a * a % mod;
}
return res;
} vector<P> G[maxn];
int val[maxn];
int ans[maxn];
int pd[maxn]; struct Node{
int a, b;
Node() {}
Node(int a_, int b_) : a(a_), b(b_) { }
bool operator < (const Node &rhs) const{
return (LL)b * rhs.a > (LL)a * rhs.b;
}
}; vector<Node> path(maxn);
vector<Node> res; void dfs(int u, int d, int len, LL all){
if(G[u].empty()){ // the leaf node
LL sum = all;
if(m >= len){
for(int i = 0; i < len; ++i) sum = sum * path[i].b % mod;
ans[u] = (int)(sum * pd[d] % mod);
return ;
}
res.cl;
res.assign(path.be, path.be+len);
sort(res.be, res.ed);
for(int i = 0; i < m; ++i) sum = sum * res[i].b % mod;
for(int i = m; i < len; ++i) sum = sum * res[i].a % mod;
ans[u] = (int)(sum * pd[d] % mod);
return ;
}
for(int i = 0; i < G[u].sz; ++i){
if(G[u][i].se == val[u])
dfs(G[u][i].fi, d + 1, len, all * val[u] % mod);
else{
path[len] = Node(G[u][i].se, val[u]);
dfs(G[u][i].fi, d + 1, len + 1, all);
} }
} int main(){
pd[0] = 1;
for(int i = 1; i < maxn; ++i) pd[i] = (LL)pd[i-1] * inv % mod;
int T; cin >> T;
while(T--){
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i) G[i].cl, val[i] = ans[i] = -1;
int a, b, c;
for(int i = 1; i < n; ++i){
scanf("%d %d %d", &a, &b, &c);
G[a].pb(P(b, c));
val[a] = max(val[a], c);
}
dfs(0, 0, 0, 1);
for(int i = 0; i < n; ++i) if(ans[i] != -1) printf("%d\n", ans[i]);
}
return 0;
}

  

HDU 6382 odds (暴力 + 剪枝优化)的更多相关文章

  1. 【OpenJudge1814】 恼人的青蛙 暴力+剪枝优化

    此题poj1054上也有 #include<cstdio> #include<cstring> #include<algorithm> using namespac ...

  2. poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】

    题目:  http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...

  3. 搜索(剪枝优化):HDU 5113 Black And White

    Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...

  4. hdu 4109 dfs+剪枝优化

    求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己添加的)出发,0到1~n个节点之间的距离为1.mt[i]表示从0点到第i个节点眼下所得的最长路径 #include<iost ...

  5. hdu 5077 NAND(暴力打表)

    题目链接:hdu 5077 NAND 题目大意:Xiaoqiang要写一个编码程序,然后依据x1,x2,x3的值构造出8个字符.如今给定要求生成的8个字符.问 说Xiaoqiang最少要写多少行代码. ...

  6. HDU2433 最短路 + 剪枝优化

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2433 ,最短路(SPFA或优化过的Dijstra) + 剪枝优化 这道题关键还是在几个剪枝上面,没有剪 ...

  7. 图解Leetcode组合总和系列——回溯(剪枝优化)+动态规划

    Leetcode组合总和系列--回溯(剪枝优化)+动态规划 组合总和 I 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 ...

  8. HDU 3507 单调队列 斜率优化

    斜率优化的模板题 给出n个数以及M,你可以将这些数划分成几个区间,每个区间的值是里面数的和的平方+M,问所有区间值总和最小是多少. 如果不考虑平方,那么我们显然可以使用队列维护单调性,优化DP的线性方 ...

  9. TensorFlow官方发布剪枝优化工具:参数减少80%,精度几乎不变

    去年TensorFlow官方推出了模型优化工具,最多能将模型尺寸减小4倍,运行速度提高3倍. 最近现又有一款新工具加入模型优化"豪华套餐",这就是基于Keras的剪枝优化工具. 训 ...

随机推荐

  1. Java属性中指定Json的属性名称

    只需要使用注解"@JsonProperty(value = "pwd")" import com.fasterxml.jackson.annotation.Js ...

  2. js解决下拉列表框互斥选项的问题

    如图不区分选项与其他选项是互斥的关系,当选择了不区分时,其他选项就要去除,当有其他选项存在时,就不能有不区分 解决办法:定义change事件,若列表发生改变,首先判断点击的是否是不区分,若是,则将其他 ...

  3. mui-H5获取当前手机通讯录

    mui.plusReady(function() { // 扩展API加载完毕,现在可以正常调用扩展API plus.contacts.getAddressBook(plus.contacts.ADD ...

  4. Vue项目碰到"‘webpack-dev-server’不是内部或外部命令,也不是可运行的程序或批处理文件"报错

    解决办法: 最后将项目里的“node_modules”文件夹删除,然后在cmd中cd到项目目录,依次运行命令:npm install和npm run build,最后运行npm run dev后项目成 ...

  5. Linux Apache虚拟主机配置方法

    apache 虚拟主机配置 注意: 虚拟主机可以开很多个 虚拟主机配置之后,原来的默认/etc/httpd/httpd.conf中的默认网站就不会生效了 练习: 主机server0 ip:172.25 ...

  6. appium 版本更新后的方法变化更新收集 ---持续更新

    在高版本的android手机(例如android 7.0 , 8.0等等),必须使用高版本的appium, 以及对应的selenium版本,那么很多的appium或者selenium方法会变得无法直接 ...

  7. python脚本处理下载的b站学习视频

    作为常年在b站学习的我,一直以来看到有兴趣的视频,从来都是点赞收藏下载三连,但是苦于我那小钢炮iphone se屏幕大小有限,看起视频实在费劲,决定一定要找个下载电脑上下载b站视频的方法,以前用过硕鼠 ...

  8. Java中关于quartz定时任务时间设置

    每天凌晨2点 0 0 2 * * ?和每天隔一小时 0 * */1 * * ? 例1:每隔5秒执行一次:*/5 * * * * ? 例2:每隔5分执行一次:0 */5 * * * ?在26分.29分. ...

  9. 自动化测试框架对比(UIAutomator、Appium)

    在Android端,appium基于WebDriver协议,利用Bootstrap.jar,最后通过调⽤用UiAutomator的命令,实现App的自动化测试. UiAutomator测试框架是And ...

  10. 4-20arget 属性和hover

    1.target 属性 定义和用法 target 属性规定在何处打开 action URL. 值 描述 _blank 在新窗口中打开. _self 默认.在相同的框架中打开. _parent 在父框架 ...