Robots Gym - 101915G
The Robotics Olympiad teams were competing in a contest.
There was a tree drawn on the floor, consisting of n nodes and n - 1 edges. The nodes are numbered from 1 to n, and each edge has a weight. The tree is rooted at the first node. q teams are participating, and each team is given an integer xi. Their robot should start at node 1, and move in the following way until there are no valid moves left: From all the edges between the current node and it's children, go through the edge with the maximum value less than xi. Note that the robot can't move to the parent, only to children.
However, the teams weren't able to program the robots to return to them after the contest, so they had to manually pick them up. Since the tree can be quite large, they need your help to determine where each robot ended it's movement.
Input
The first line contains a single integer T, the number of test cases.
The first line of each test case contains two space-separated integers n and q. (1 ≤ n, q ≤ 105).
The following n - 1 lines contain 3 integers ui, vi, wi. This means that there is an edge connecting nodes ui and vi, with weight wi. (1 ≤ ui, vi ≤ n) (1 ≤ wi ≤ 109). It's guaranteed that all wi are distinct.
The following line contains q integers xi. (1 ≤ xi ≤ 109).
Output
For each test case, print one line with a single number Si, the sum of numbers of nodes where each robot ends.
Example
1
5 7
1 2 3
1 3 4
3 4 9
3 5 7
1 3 4 9 8 7 10
21
Note
In the sample test case, the robots end in the following nodes: {1, 1, 2, 5, 5, 3, 4}.
Si = 1+1+2+5+5+3+4 = 21.
Large I/O files. Please consider using fast input/output methods.
题意:给一颗顶点数为n的带权无向树,定点编号为1-n,有q个机器人,每个机器人带一个值,要求所有机器人从顶点1出发,机器人权值严格大于边权且只能向孩子节点反向才能移动。问所有机器人终点顶点编号和。
思路:先dfs将所有的点到原点的最大权值记录下来,之后将机器人按从大到小排序,开始边跑边删
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 1e5+;
const int mod = 1e9+; struct edge
{
int v,w;
friend bool operator < (edge a,edge b)
{
return a.w > b.w;
}
};
vector<edge>V[maxn];
void addedeg(int u,int v,int w)
{
V[u].push_back(edge{v,w});
}
int maxx[maxn];
int vis[maxn];
void cal(int x,int fa)
{
if(V[x].size() == || x == -)
return;
for(int i=;i<V[x].size();i++)
{
int to = V[x][i].v;
if(to == fa)
continue;
maxx[to] = max(maxx[x],V[x][i].w);
cal(to,x);
}
}
priority_queue<int,vector<int>,less<int>> pq;
ll ans = ;
void dfs(int x)
{
vis[x] = ;
if(pq.empty() || pq.top() < maxx[x])
return;
for(int i=;i<V[x].size();i++)
{
int to = V[x][i].v;
if(vis[to])
continue;
if(pq.top() > V[x][i].w)
dfs(to);
}
while(pq.size() && pq.top() > maxx[x])
{
ans += x;
pq.pop();
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,q;
memset(vis,,sizeof vis);
memset(maxx,,sizeof maxx);
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
V[i].clear();
for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedeg(u,v,w);
addedeg(v,u,w);
}
cal(,-);
for(int i=;i<=n;i++)
sort(V[i].begin(),V[i].end());
while(q--)
{
int x;
scanf("%d",&x);
pq.push(x);
}
ans = ;
dfs();
printf("%lld\n",ans);
}
}
Robots Gym - 101915G的更多相关文章
- Gym 101915G Robots
G. Robots time limit per test 5.0 s memory limit per test 256 MB input standard input output standar ...
- Gym 101915
Gym - 101915A Printing Books 题意:有一本书,从第X页开始,一共用了n位数字,求此书一共多少页.99就是两位数字,100就是三位数字. 思路:直接模拟即可,我用了一个hi ...
- gym 100971 J Robots at Warehouse
Vitaly works at the warehouse. The warehouse can be represented as a grid of n × m cells, each of wh ...
- 【Gym 100971J】Robots at Warehouse
题意 链接给你一个n*m的地图,'#'代表墙,‘.’代表可走的,1代表1号机器人,2代表2号机器人,机器人可以上下左右移动到非墙的位置,但不能走到另一个机器人身上.问能否交换1和2的位置. 分析 如果 ...
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...
- Gym 100971J-Robots at Warehouse
题目链接:http://codeforces.com/gym/100971/problem/J Vitaly works at the warehouse. The warehouse can be ...
- Gym - 100971J (思维+简单bfs)
题目链接:http://codeforces.com/gym/100971/problem/J J. Robots at Warehouse time limit per test 2.0 s mem ...
- (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest
layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest author: "luow ...
- Open AI Gym简介
介绍 OpenAI Gym是一款用于研发和比较强化学习算法的工具包,它支持训练智能体(agent)做任何事——从行走到玩Pong或围棋之类的游戏都在范围中. OpenAI Gym 是一个用于开发和比较 ...
随机推荐
- intellijidea课程 intellijidea神器使用技巧 4-1 重构
1 重构变量 shift + F6 将选中的变量以及用到该变量的部分全部修改 2 重构方法 Ctrl + F6 重构变量
- windows远程连接Mac、Mac远程连接Mac、Mac连接Windows
最近因为要进行学习交流,需要用到远程连接,所以找了三种不同的方式,记录如下 1.Windows远程连接Mac 1.mac os x电脑设置 系统偏好设置-共享-勾选“远端管理”,然后在电脑设置—VNC ...
- [转]WinForm下Splash(启动画面)制作
本文转自:http://www.smartgz.com/blog/Article/1088.asp 原文如下: 本代码可以依据主程序加载进度来显示Splash. static class Progra ...
- Jetty服务器的使用
Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可以将 ...
- So you want to write a desktop app in Python
So you want to write a desktop app in Python Thomas Kluyver 2014-06-16 23:55 51 Comments Source This ...
- Windows 静态IP脚本
@echo off echo 快速设置IP地址和DNS为“静态” set 连接名称=以太网 set ip地址=192.168.1.80 set 子网掩码=255.255.255.0 set 网关地址= ...
- OpenGL学习 Our First OpenGL Program
This shows you how to create the main window with the book’s application framework and how to render ...
- mysql索引和正确使用方式
一.索引类型 B树索引:大部分都是,因此B树的特性限制了索引如何使用:必须看看索引的正确使用限制(含组合索引的限制)http://blog.csdn.net/lovemdx/article/detai ...
- 【UOJ139】【UER #4】被删除的黑白树(贪心)
点此看题面 大致题意: 请你给一棵树黑白染色,使每一个叶结点到根节点的路径上黑节点个数相同. 贪心 显然,按照贪心的思想,我们要让叶结点到根节点的路径上黑节点的个数尽量大. 我们可以用\(Min_i\ ...
- 定位webpack文件大小
之前发现一个神器,记录一下,可以可视化webpack打包的每个js文件大小,这样对我们优化代码是有帮助的,有目标的 https://www.npmjs.com/package/webpack-bund ...