QOJ.ac

QOJ

Time Limit: 0.7 s Memory Limit: 256 MB Total points: 100

#5705. 图

Statistics

给定一个无向图,每条边有两种颜色之一:黑色或红色。 你的任务是为每个节点分配一个实数,使得:

  • 对于每条黑色边,其端点上的数值之和为 $1$;
  • 对于每条红色边,其端点上的数值之和为 $2$;
  • 所有分配数值的绝对值之和尽可能小。

如果无法做到,请报告不存在可行的数值分配方案。

输入格式

第一行包含两个整数 $N$ ($1 \le N \le 100\,000$) 和 $M$ ($0 \le M \le 200\,000$),分别表示节点数和边数。节点编号为连续整数:$1, 2, \dots, N$。

接下来的 $M$ 行描述边。每行包含三个整数 $a, b$ 和 $c$,表示节点 $a$ 和 $b$ ($1 \le a, b \le N$) 之间存在一条颜色为 $c$ 的边($1$ 表示黑色,$2$ 表示红色)。

输出格式

如果存在解,第一行应包含单词 “YES”,第二行应包含 $N$ 个用空格分隔的数字。对于每个 $i$ ($1 \le i \le N$),第 $i$ 个数字应为分配给节点 $i$ 的数值。

输出应满足:

  • 每条边端点上的数值之和与精确值之差小于 $10^{-6}$;
  • 所有分配数值的绝对值之和与最小值之差小于 $10^{-6}$。

如果有多个有效解,输出其中任意一个即可。

如果不存在解,唯一的一行应包含单词 “NO”。

样例

样例输入 1

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

样例输出 1

YES
0.5 0.5 1.5 -0.5

样例输入 2

2 1
1 2 1

样例输出 2

YES
0.3 0.7

说明 2

注意解不唯一。

样例输入 3

3 2
1 2 2
2 3 2

样例输出 3

YES
0 2 0

样例输入 4

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

样例输出 4

NO

子任务

  1. ($5$ 分) $N \le 5, M \le 14$
  2. ($12$ 分) $N \le 100$
  3. ($17$ 分) $N \le 1000$
  4. ($24$ 分) $N \le 10\,000$
  5. ($42$ 分) 无额外限制

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.