zoukankan      html  css  js  c++  java
  • hdu4010 Query On The Trees

    Problem Description
    We have met so many problems on the tree, so today we will have a query problem on a set of trees.
    There are N nodes, each node will have a unique weight Wi. We will have four kinds of operations on it and you should solve them efficiently. Wish you have fun!
     
    Input
    There are multiple test cases in our dataset.
    For each case, the first line contains only one integer N.(1 ≤ N ≤ 300000) The next N‐1 lines each contains two integers x, y which means there is an edge between them. It also means we will give you one tree initially.
    The next line will contains N integers which means the weight Wi of each node. (0 ≤ Wi ≤ 3000)
    The next line will contains an integer Q. (1 ≤ Q ≤ 300000) The next Q lines will start with an integer 1, 2, 3 or 4 means the kind of this operation.
    1. Given two integer x, y, you should make a new edge between these two node x and y. So after this operation, two trees will be connected to a new one.
    2. Given two integer x, y, you should find the tree in the tree set who contain node x, and you should make the node x be the root of this tree, and then you should cut the edge between node y and its parent. So after this operation, a tree will be separate into two parts.
    3. Given three integer w, x, y, for the x, y and all nodes between the path from x to y, you should increase their weight by w.
    4. Given two integer x, y, you should check the node weights on the path between x and y, and you should output the maximum weight on it.
     
    Output
    For each query you should output the correct answer of it. If you find this query is an illegal operation, you should output ‐1.
    You should output a blank line after each test case.
     
    Sample Input
    5 1 2 2 4 2 5 1 3 1 2 3 4 5 6 4 2 3 2 1 2 4 2 3 1 3 5 3 2 1 4 4 1 4
     
    Sample Output
    3 -1 7
    Hint

    We define the illegal situation of different operations: In first operation: if node x and y belong to a same tree, we think it’s illegal. In second operation: if x = y or x and y not belong to a same tree, we think it’s illegal. In third operation: if x and y not belong to a same tree, we think it’s illegal. In fourth operation: if x and y not belong to a same tree, we think it’s illegal.

     
    lct裸题
    妈蛋 题看错了,以为操作2就是断掉x,y之间的边
    对着黄学长的代码改了好久才发现这个问题
    草草草草草草草草草草草草草草草草草草草草
  • 相关阅读:
    教你做一个牛逼的DBA(在大数据下)
    分区扫描执行计划分析简介
    远程桌面无法复制粘贴
    测试链接服务器sql 语句
    webservice 测试窗体只能用于来自本地计算机的请求
    win2003浏览器提示是否需要将当前访问的网站添加到自己信任的站点中去
    如何在excel中把汉字转换成拼音
    关于update set from where
    添加链接服务器的代码
    「android」as过滤svn文件
  • 原文地址:https://www.cnblogs.com/showson/p/4660688.html
Copyright © 2011-2022 走看看