Robots Gym - 101915G
Posted smallhester
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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+10; const int mod = 1e9+7; 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() == 0 || x == -1) return; for(int i=0;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 = 0; void dfs(int x) { vis[x] = 1; if(pq.empty() || pq.top() < maxx[x]) return; for(int i=0;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,0,sizeof vis); memset(maxx,0,sizeof maxx); scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) V[i].clear(); for(int i=1;i<n;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); addedeg(u,v,w); addedeg(v,u,w); } cal(1,-1); for(int i=1;i<=n;i++) sort(V[i].begin(),V[i].end()); while(q--) { int x; scanf("%d",&x); pq.push(x); } ans = 0; dfs(1); printf("%lld ",ans); } }
以上是关于Robots Gym - 101915G的主要内容,如果未能解决你的问题,请参考以下文章
Python robotsparser 模块不会加载“robots.txt”
php Yoast SEO机器人或WP robots.txt的过滤器和示例代码
解决使用Monitor出现gym.error.DependencyNotInstalled: Found neither the ffmpeg nor avconv executables的问题(代码