CF1061E.Politics

省选/NOI-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There are nn cities in the country.

Two candidates are fighting for the post of the President. The elections are set in the future, and both candidates have already planned how they are going to connect the cities with roads. Both plans will connect all cities using n−1n - 1 roads only. That is, each plan can be viewed as a tree. Both of the candidates had also specified their choice of the capital among nn cities (xx for the first candidate and yy for the second candidate), which may or may not be same.

Each city has a potential of building a port (one city can have at most one port). Building a port in ii-th city brings aia_i amount of money. However, each candidate has his specific demands. The demands are of the form:

  • kk xx, which means that the candidate wants to build exactly xx ports in the subtree of the kk-th city of his tree (the tree is rooted at the capital of his choice).

Find out the maximum revenue that can be gained while fulfilling all demands of both candidates, or print -1 if it is not possible to do.

It is additionally guaranteed, that each candidate has specified the port demands for the capital of his choice.

该国共有 nn 座城市。

两名候选人正在竞选总统职位。选举将在未来举行,且两名候选人均已规划好如何用道路连接所有城市。两种方案均仅使用 n−1n - 1 条道路将全部城市连通,即每种方案均可视为一棵树。此外,两名候选人均已在 nn 座城市中各自选定首都(第一位候选人选的是城市 xx,第二位候选人选的是城市 yy),二者可能相同,也可能不同。

每座城市均具备建设港口的潜力(每座城市最多建设一座港口)。在第 ii 座城市建设港口可带来 aia_i 的收益。然而,每名候选人均有其特定的要求,形式如下:

  • kk xx:表示该候选人要求在其树结构中以第 kk 座城市为根的子树内恰好建设 xx 座港口(该树以该候选人所选的首都为根)。

请计算在同时满足两名候选人所有要求的前提下,所能获得的最大总收益;若无法同时满足所有要求,则输出 -1。

额外保证:每名候选人所提出的所有港口建设要求中,必包含对其所选首都城市对应子树的要求。

输入格式

The first line contains integers nn, xx and yy (1≤n≤5001 \le n \le 500, 1≤x,y≤n1 \le x, y \le n) — the number of cities, the capital of the first candidate and the capital of the second candidate respectively.

Next line contains integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤100 0001 \le a_i \le 100\,000) — the revenue gained if the port is constructed in the corresponding city.

Each of the next n−1n - 1 lines contains integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i), denoting edges between cities in the tree of the first candidate.

Each of the next n−1n - 1 lines contains integers ui′u'_i and vi′v'_i (1≤ui′,vi′≤n1 \le u'_i, v'_i \le n, ui′≠vi′u'_i \ne v'_i), denoting edges between cities in the tree of the second candidate.

Next line contains an integer q1q_1 (1≤q1≤n1 \le q_1 \le n), denoting the number of demands of the first candidate.

Each of the next q1q_1 lines contains two integers kk and xx (1≤k≤n1 \le k \le n, 1≤x≤n1 \le x \le n) — the city number and the number of ports in its subtree.

Next line contains an integer q2q_2 (1≤q2≤n1 \le q_2 \le n), denoting the number of demands of the second candidate.

Each of the next q2q_2 lines contain two integers kk and xx (1≤k≤n1 \le k \le n, 1≤x≤n1 \le x \le n) — the city number and the number of ports in its subtree.

It is guaranteed, that given edges correspond to valid trees, each candidate has given demand about each city at most once and that each candidate has specified the port demands for the capital of his choice. That is, the city xx is always given in demands of the first candidate and city yy is always given in the demands of the second candidate.

第一行包含三个整数 nn、xx 和 yy(1≤n≤5001 \le n \le 500,1≤x,y≤n1 \le x, y \le n),分别表示城市数量、第一位候选人的首都以及第二位候选人的首都。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤100 0001 \le a_i \le 100\,000),表示在对应城市建造港口所能获得的收益。

接下来的 n−1n - 1 行每行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \le u_i, v_i \le n,ui≠viu_i \ne v_i),表示第一位候选人所对应的树中城市之间的边。

再接下来的 n−1n - 1 行每行包含两个整数 ui′u'_i 和 vi′v'_i(1≤ui′,vi′≤n1 \le u'_i, v'_i \le n,ui′≠vi′u'_i \ne v'_i),表示第二位候选人所对应的树中城市之间的边。

下一行包含一个整数 q1q_1(1≤q1≤n1 \le q_1 \le n),表示第一位候选人的需求条数。

接下来的 q1q_1 行每行包含两个整数 kk 和 xx(1≤k≤n1 \le k \le n,1≤x≤n1 \le x \le n),表示城市编号 kk 及其子树中需建造的港口数量。

下一行包含一个整数 q2q_2(1≤q2≤n1 \le q_2 \le n),表示第二位候选人的需求条数。

接下来的 q2q_2 行每行包含两个整数 kk 和 xx(1≤k≤n1 \le k \le n,1≤x≤n1 \le x \le n),表示城市编号 kk 及其子树中需建造的港口数量。

保证所给的边构成合法的树;每位候选人对每个城市的需求数至多出现一次;且每位候选人均已为其所选首都指定港口需求。即:城市 xx 总是出现在第一位候选人的需求中,城市 yy 总是出现在第二位候选人的需求中。

输出格式

Print exactly one integer — the maximum possible revenue that can be gained, while satisfying demands of both candidates, or -1 if it is not possible to satisfy all of the demands.

输出一个整数——在满足两位候选人需求的前提下所能获得的最大收益;如果无法满足所有需求,则输出 −1-1。

输入输出样例

  • 输入#1

    4 1 2
    1 2 3 4
    1 2
    1 3
    3 4
    1 2
    2 3
    1 4
    2
    1 3
    4 1
    1
    2 3

    输出#1

    9
  • 输入#2

    5 1 1
    3 99 99 100 2
    1 2
    1 3
    3 4
    3 5
    1 3
    1 2
    2 4
    2 5
    2
    1 2
    3 1
    2
    1 2
    2 1

    输出#2

    198
  • 输入#3

    4 1 2
    1 2 3 4
    1 2
    1 3
    3 4
    2 1
    2 4
    4 3
    1
    1 4
    2
    4 1
    2 4

    输出#3

    -1

说明/提示

In the first example, it is optimal to build ports in cities 22, 33 and 44, which fulfills all demands of both candidates and gives revenue equal to 2+3+4=92 + 3 + 4 = 9.

In the second example, it is optimal to build ports in cities 22 and 33, which fulfills all demands of both candidates and gives revenue equal to 99+99=19899 + 99 = 198.

In the third example, it is not possible to build ports in such way, that all demands of both candidates are specified, hence the answer is -1.

在第一个例子中,最优方案是在城市 22、33 和 44 建设港口,这能够满足两位候选人的全部需求,且获得的收益为 2+3+4=92 + 3 + 4 = 9。

在第二个例子中,最优方案是在城市 22 和 33 建设港口,这能够满足两位候选人的全部需求,且获得的收益为 99+99=19899 + 99 = 198。

在第三个例子中,不存在一种建设港口的方式,使得两位候选人的全部需求均被满足,因此答案为 −1-1。

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

首页