CF901D.Weighting a Tree

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a connected undirected graph with n vertices and m edges. The vertices are enumerated from 1 to n.

You are given n integers _c_1, _c_2, ..., c__n, each of them is between  - n and n, inclusive. It is also guaranteed that the parity of c__v equals the parity of degree of vertex v. The degree of a vertex is the number of edges connected to it.

You are to write a weight between  - 2·_n_2 and 2·_n_2 (inclusive) on each edge in such a way, that for each vertex v the sum of weights on edges connected to this vertex is equal to c__v, or determine that this is impossible.

给你一个包含 nn 个顶点和 mm 条边的连通无向图。顶点编号为 11 到 nn。

给你 nn 个整数 c1, c2, …, cnc_1,\,c_2,\,\dots,\,c_n,每个整数均在 −n-n 到 nn(含端点)之间。同时保证:对每个顶点 vv,cvc_v 的奇偶性与顶点 vv 的度数的奇偶性相同。顶点的度数是指与其相连的边的数量。

你需要给每条边赋予一个权值,该权值需在 −2⋅n2-2\cdot n^2 到 2⋅n22\cdot n^2(含端点)之间,使得对每个顶点 vv,与其相连的所有边上的权值之和恰好等于 cvc_v;若无法做到,则判定为不可能。

输入格式

The first line contains two integers n and m (2 ≤ n ≤ 105, n - 1 ≤ m ≤ 105) — the number of vertices and the number of edges.

The next line contains n integers _c_1, _c_2, ..., c__n ( - n ≤ c__i ≤ n), where c__i is the required sum of weights of edges connected to vertex i. It is guaranteed that the parity of c__i equals the parity of degree of vertex i.

The next m lines describe edges of the graph. The i-th of these lines contains two integers a__i and b__i (1 ≤ a__i, b__i ≤ n; a__i ≠ b__i), meaning that the i-th edge connects vertices a__i and b__i.

It is guaranteed that the given graph is connected and does not contain loops and multiple edges.

第一行包含两个整数 nn 和 mm(2≤n≤1052 \leq n \leq 10^5,n−1≤m≤105n-1 \leq m \leq 10^5)—— 分别表示顶点数和边数。

第二行包含 nn 个整数 c1, c2, …, cnc_1,\,c_2,\,\dots,\,c_n(−n≤ci≤n-n \leq c_i \leq n),其中 cic_i 表示与顶点 ii 相连的所有边的权重之和。保证每个 cic_i 的奇偶性与顶点 ii 的度数的奇偶性相同。

接下来的 mm 行描述图中的边。其中第 ii 行包含两个整数 aia_i 和 bib_i(1≤ai, bi≤n1 \leq a_i,\,b_i \leq n;ai≠bia_i \neq b_i),表示第 ii 条边连接顶点 aia_i 和 bib_i。

保证所给图是连通的,且不含自环和重边。

输出格式

If there is no solution, print "NO".

Otherwise print "YES" and then m lines, the i-th of them is the weight of the i-th edge w__i ( - 2·_n_2 ≤ w__i ≤ 2·_n_2).

如果无解,输出 "NO"。

否则输出 "YES",然后输出 m 行,其中第 i 行为第 i 条边的权值 w__i(满足 - 2·_n_² ≤ w__i ≤ 2·_n_²)。

输入输出样例

  • 输入#1

    3 3
    2 2 2
    1 2
    2 3
    1 3

    输出#1

    YES
    1
    1
    1
  • 输入#2

    4 3
    -1 0 2 1
    1 2
    2 3
    3 4

    输出#2

    YES
    -1
    1
    1
  • 输入#3

    6 6
    3 5 5 5 1 5
    1 4
    3 2
    4 3
    4 5
    3 5
    5 6

    输出#3

    YES
    3
    5
    3
    -1
    -3
    5
  • 输入#4

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

    输出#4

    NO

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

首页