CF1787G.Colorful Tree Again

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

An edge-weighted tree of nn nodes is given with each edge colored in some color. Each node of this tree can be blocked or unblocked, all nodes are unblocked initially.

A simple path is a path in a graph that does not have repeating nodes. The length of a path is defined as the sum of weights of all edges on the path.

A path is good when it is a simple path consisting of edges of the same color cc, all edges of color cc are on this path, and every node on the path is unblocked.

You need to operate 22 kinds of queries:

  1. block a node,
  2. unblock a node.

After each query, print the maximum length among all good paths. If there are no good paths, print 00.

给定一棵含 nn 个节点的边带权树,每条边被染成某种颜色。该树的每个节点可以处于“阻塞”或“未阻塞”状态,初始时所有节点均为未阻塞。

简单路径(simple path)指图中不包含重复节点的路径。路径的长度定义为该路径上所有边的权重之和。

当一条路径满足以下条件时,称为好路径(good path):

  • 它是一条简单路径;
  • 它仅由同一种颜色 cc 的边构成;
  • 该颜色 cc 的所有边都出现在此路径上;
  • 路径上的每个节点均处于未阻塞状态。

你需要处理两类操作查询:

  1. 阻塞一个节点;
  2. 解除一个节点的阻塞状态。

每次查询后,请输出当前所有好路径中的最大长度;若不存在任何好路径,则输出 00。

输入格式

The first line contains two integers nn, qq (1≤n,q≤2⋅1051 \leq n,q \leq 2\cdot 10^5) — the number of nodes and the number of queries.

Then n−1n-1 lines follow, each containing four integers uu, vv, ww and cc (1≤u,v,w,c≤n1 \leq u,v,w,c \leq n; u≠vu \not = v), denoting a weighted edge connecting node uu and node vv with weight ww and color cc. It is guaranteed that these edges form a tree.

Then qq lines follow, each containing two integers pp and xx (p=0p = 0 or p=1p = 1, 1≤x≤n1\leq x\leq n), denoting a query:

  1. if p=0p = 0, block the node xx. It's guaranteed that it's not blocked at this time;
  2. if p=1p = 1, unblock the node xx. It's guaranteed that it's blocked at this time.

第一行包含两个整数 nn、qq(1≤n,q≤2⋅1051 \leq n,q \leq 2\cdot 10^5),分别表示节点数和查询数。

接下来 n−1n-1 行,每行包含四个整数 uu、vv、ww 和 cc(1≤u,v,w,c≤n1 \leq u,v,w,c \leq n;u≠vu \neq v),表示一条连接节点 uu 和节点 vv 的带权边,其权重为 ww,颜色为 cc。保证这些边构成一棵树。

接下来 qq 行,每行包含两个整数 pp 和 xx(p=0p = 0 或 p=1p = 1,1≤x≤n1\leq x\leq n),表示一个查询:

  1. 若 p=0p = 0,则封锁节点 xx;保证此时该节点尚未被封锁;
  2. 若 p=1p = 1,则解除对节点 xx 的封锁;保证此时该节点已被封锁。

输出格式

For each query, print the maximum length of a good path. If there are no good paths, print 00.

对于每个查询,输出一条好路径的最大长度。如果没有好路径,则输出 00。

输入输出样例

  • 输入#1

    5 4
    4 1 3 4
    5 2 4 4
    3 1 3 2
    1 2 5 1
    0 4
    0 3
    0 2
    1 3

    输出#1

    5
    5
    0
    3
  • 输入#2

    5 5
    4 1 4 4
    4 5 2 2
    3 1 2 4
    3 2 3 1
    0 3
    0 4
    1 3
    1 4
    0 1

    输出#2

    2
    0
    3
    6
    3
  • 输入#3

    6 9
    3 2 2 3
    2 4 4 2
    3 1 5 5
    6 4 3 2
    5 3 1 3
    0 2
    0 4
    0 5
    0 6
    1 2
    1 4
    1 5
    0 3
    1 6

    输出#3

    5
    5
    5
    5
    5
    5
    5
    0
    7
  • 输入#4

    1 2
    0 1
    1 1

    输出#4

    0
    0

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

首页