CF838B.Diverging Directions

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a directed weighted graph with n nodes and 2_n_ - 2 edges. The nodes are labeled from 1 to n, while the edges are labeled from 1 to 2_n_ - 2. The graph's edges can be split into two parts.

  • The first n - 1 edges will form a rooted spanning tree, with node 1 as the root. All these edges will point away from the root.
  • The last n - 1 edges will be from node i to node 1, for all 2 ≤ i ≤ n.

You are given q queries. There are two types of queries

  • 1 i w: Change the weight of the i-th edge to w
  • 2 u v: Print the length of the shortest path between nodes u to v

Given these queries, print the shortest path lengths.

给你一个包含 nn 个节点和 2n−22n-2 条有向带权边的图。节点编号为 11 到 nn,边编号为 11 到 2n−22n-2。该图的边可分为两部分:

  • 前 n−1n-1 条边构成一棵以节点 11 为根的有向生成树,且所有这些边均从根节点向外指向(即方向远离根节点)。
  • 后 n−1n-1 条边则分别为从节点 ii 指向节点 11 的边,其中 2≤i≤n2 \leq i \leq n。

你将收到 qq 个查询,查询分为两类:

  • 1 i w:将第 ii 条边的权重修改为 ww;
  • 2 u v:输出节点 uu 到节点 vv 的最短路径长度。

请根据这些查询,依次输出每次类型 2 查询所对应的最短路径长度。

输入格式

The first line of input will contain two integers n, q (2 ≤ n, q ≤ 200 000), the number of nodes, and the number of queries, respectively.

The next 2_n_ - 2 integers will contain 3 integers a__i, b__i, c__i, denoting a directed edge from node a__i to node b__i with weight c__i.

The first n - 1 of these lines will describe a rooted spanning tree pointing away from node 1, while the last n - 1 of these lines will have b__i = 1.

More specifically,

  • The edges (_a_1, _b_1), (_a_2, _b_2), ... (a__n - 1, b__n - 1) will describe a rooted spanning tree pointing away from node 1.
  • b__j = 1 for n ≤ j ≤ 2_n_ - 2.
  • a__n, a__n + 1, ..., a_2_n - 2 will be distinct and between 2 and n.

The next q lines will contain 3 integers, describing a query in the format described in the statement.

All edge weights will be between 1 and 106.

输入的第一行包含两个整数 nn 和 qq(2≤n,q≤200 0002 \leq n, q \leq 200\,000),分别表示节点数和查询数。

接下来的 2n−22n - 2 个整数将包含 33 个整数 ai, bi, cia_i,\, b_i,\, c_i,表示一条从节点 aia_i 指向节点 bib_i、权重为 cic_i 的有向边。

其中前 n−1n-1 行描述一棵以节点 11 为根、边方向远离根节点的有根生成树;后 n−1n-1 行则满足 bi=1b_i = 1。

更具体地:

  • 边 (a1, b1), (a2, b2), …, (an−1, bn−1)(a_1,\, b_1),\, (a_2,\, b_2),\, \dots,\, (a_{n-1},\, b_{n-1}) 描述一棵以节点 11 为根、边方向远离根节点的有根生成树;
  • 对于 n≤j≤2n−2n \leq j \leq 2n - 2,均有 bj=1b_j = 1;
  • an, an+1, …, a2n−2a_n,\, a_{n+1},\, \dots,\, a_{2n-2} 互不相同,且均在 22 到 nn 之间(含端点)。

接下来的 qq 行每行包含 33 个整数,描述一个如题面所述格式的查询。

所有边的权重均在 11 到 10610^6 之间。

输出格式

For each type 2 query, print the length of the shortest path in its own line.

对于每个类型 2 的查询,在单独一行中输出最短路径的长度。

输入输出样例

  • 输入#1

    5 9
    1 3 1
    3 2 2
    1 4 3
    3 5 4
    5 1 5
    3 1 6
    2 1 7
    4 1 8
    2 1 1
    2 1 3
    2 3 5
    2 5 2
    1 1 100
    2 1 3
    1 8 30
    2 4 2
    2 2 4

    输出#1

    0
    1
    4
    8
    100
    132
    10

输入解题思路,AI测评打分。不知道怎么写?

首页