zoukankan      html  css  js  c++  java
  • Robots Gym

    传送门

    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

    Input
    1
    5 7
    1 2 3
    1 3 4
    3 4 9
    3 5 7
    1 3 4 9 8 7 10
    Output
    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);
        }
    }
  • 相关阅读:
    使用jetty部署配置solr服务
    solr 与 MySQL(二)
    学习solr(一)
    FormData 上传文件
    node.js cannot find module 'mysql'
    select2 ajax 无法选中
    jenkins持续集成文件冲突的问题
    Inno Setup 实现每次jenkins自动构建时版本号自动+1
    jenkins 配置slave节点(win10系统)
    Jenkins+Gradle实现android开发持续集成和打包
  • 原文地址:https://www.cnblogs.com/smallhester/p/10386914.html
Copyright © 2011-2022 走看看