E. Perpetual Motion Machine

Codeforces
IDCF830E
Time2000ms
Memory512MB
Difficulty
constructive algorithmsdpgraphsimplementationmathtrees
English · Original
Chinese · Translation
Formal · Original
Developer Petr thinks that he invented a perpetual motion machine. Namely, he has a lot of _elements_, which work in the following way. Each element has one controller that can be set to any non-negative real value. If a controller is set on some value _x_, then the controller consumes _x_2 energy units per second. At the same time, any two elements connected by a wire produce _y_·_z_ energy units per second, where _y_ and _z_ are the values set on their controllers. Petr has only a limited number of wires, so he has already built some scheme of elements and wires, and is now interested if it's possible to set the controllers in such a way that the system produces **at least as much** power as it consumes, and at least one controller is set on the value different from 0. Help him check this, and if it's possible, find the required **integer** values that should be set. It is guaranteed that if there exist controllers' settings satisfying the above conditions, then there exist required integer values not greater than 106. ## Input There are several (at least one) test cases in the input. The first line contains single integer — the number of test cases. There is an empty line before each test case. The first line of test case contains two integers _n_ and _m_ (1 ≤ _n_ ≤ 105, 0 ≤ _m_ ≤ 105) — the number of elements in the scheme and the number of wires. After that, _m_ lines follow, each of them contains two integers _a_ and _b_ (1 ≤ _a_, _b_ ≤ _n_) — two elements connected by a wire. No element is connected with itself, no two elements are connected by more than one wire. It is guaranteed that the sum of _n_ and the sum of _m_ over all test cases do not exceed 105. **For hacks** you can only use tests with one test case. ## Output Print answer for each test case. For each test case print "_YES_" if it's possible to set the controllers in such a way that the consumed power is not greater than the power produced, and the required values on the next line. The settings should be integers from 0 to 106, inclusive, and at least one value should be different from 0. If there are multiple answers, print any of them. If it's not possible to set the controllers in the required way, print one line "_NO_". [samples] ## Note In the first example it's possible to set the controllers in the required way, for example, in the following way: set 1 on the first element, set 2 on the second and on the third, set 1 on the fourth. The consumed power is then equal to 12 + 22 + 22 + 12 = 10 energy units per second, the produced power is equal to 1·2 + 2·2 + 2·1 + 2·1 = 10 energy units per second. Thus the answer is "_YES_". In the second test case it's not possible to set the controllers in the required way. For example, if we set all controllers to 0.5, then the consumed powers equals 0.75 energy units per second, while produced power equals 0.5 energy units per second.
开发者 Petr 认为他发明了一台永动机。具体来说,他有许多 _元素_,其工作方式如下: 每个元素有一个控制器,可以设置为任意非负实数。如果控制器被设置为值 #cf_span[x],则该控制器每秒消耗 #cf_span[x2] 能量单位。同时,任意两个由导线连接的元素每秒产生 #cf_span[y·z] 能量单位,其中 #cf_span[y] 和 #cf_span[z] 分别是它们控制器上设置的值。 Petr 只有有限数量的导线,因此他已经构建了一些元素和导线的结构,现在他想知道是否可以设置控制器,使得系统产生的功率至少等于消耗的功率,且至少有一个控制器的值不为 #cf_span[0]。请帮助他判断是否可行,如果可行,找出所需的 _整数_ 值。 保证如果存在满足上述条件的控制器设置,则一定存在不超过 #cf_span[106] 的整数解。 输入包含多个(至少一个)测试用例。第一行包含一个整数 —— 测试用例的数量。 每个测试用例前有一个空行。每个测试用例的第一行包含两个整数 #cf_span[n] 和 #cf_span[m](#cf_span[1 ≤ n ≤ 105],#cf_span[0 ≤ m ≤ 105])—— 分别表示方案中的元素数量和导线数量。 接下来 #cf_span[m] 行,每行包含两个整数 #cf_span[a] 和 #cf_span[b](#cf_span[1 ≤ a, b ≤ n])—— 表示由导线连接的两个元素。没有元素与自身相连,任意两个元素之间至多有一条导线。 保证所有测试用例中 #cf_span[n] 的总和以及 #cf_span[m] 的总和均不超过 #cf_span[105]。 *对于 Hack*,你只能使用仅含一个测试用例的测试数据。 请为每个测试用例输出答案。 对每个测试用例,如果可以设置控制器使得消耗功率不超过产生功率,且至少有一个控制器的值非零,则输出 "_YES_",并在下一行输出所设置的值。设置值必须是介于 #cf_span[0] 到 #cf_span[106](含)之间的整数,且至少有一个值不为 #cf_span[0]。如果有多个答案,输出任意一个即可。 如果无法按要求设置控制器,则输出一行 "_NO_"。 在第一个例子中,可以按如下方式设置控制器:第一个元素设为 #cf_span[1],第二个和第三个元素设为 #cf_span[2],第四个元素设为 #cf_span[1]。此时消耗功率为 #cf_span[12 + 22 + 22 + 12 = 10] 能量单位/秒,产生功率为 #cf_span[1·2 + 2·2 + 2·1 + 2·1 = 10] 能量单位/秒。因此答案为 "_YES_"。 在第二个测试用例中,无法按要求设置控制器。例如,若将所有控制器设为 #cf_span[0.5],则消耗功率为 #cf_span[0.75] 能量单位/秒,而产生功率仅为 #cf_span[0.5] 能量单位/秒。 ## Input 输入包含多个(至少一个)测试用例。第一行包含一个整数 —— 测试用例的数量。每个测试用例前有一个空行。每个测试用例的第一行包含两个整数 #cf_span[n] 和 #cf_span[m](#cf_span[1 ≤ n ≤ 105],#cf_span[0 ≤ m ≤ 105])—— 分别表示方案中的元素数量和导线数量。接下来 #cf_span[m] 行,每行包含两个整数 #cf_span[a] 和 #cf_span[b](#cf_span[1 ≤ a, b ≤ n])—— 表示由导线连接的两个元素。没有元素与自身相连,任意两个元素之间至多有一条导线。保证所有测试用例中 #cf_span[n] 的总和以及 #cf_span[m] 的总和均不超过 #cf_span[105]。*对于 Hack*,你只能使用仅含一个测试用例的测试数据。 ## Output 请为每个测试用例输出答案。对每个测试用例,如果可以设置控制器使得消耗功率不超过产生功率,且至少有一个控制器的值非零,则输出 "_YES_",并在下一行输出所设置的值。设置值必须是介于 #cf_span[0] 到 #cf_span[106](含)之间的整数,且至少有一个值不为 #cf_span[0]。如果有多个答案,输出任意一个即可。如果无法按要求设置控制器,则输出一行 "_NO_"。 [samples] ## Note 在第一个例子中,可以按如下方式设置控制器:第一个元素设为 #cf_span[1],第二个和第三个元素设为 #cf_span[2],第四个元素设为 #cf_span[1]。此时消耗功率为 #cf_span[12 + 22 + 22 + 12 = 10] 能量单位/秒,产生功率为 #cf_span[1·2 + 2·2 + 2·1 + 2·1 = 10] 能量单位/秒。因此答案为 "_YES_"。在第二个测试用例中,无法按要求设置控制器。例如,若将所有控制器设为 #cf_span[0.5],则消耗功率为 #cf_span[0.75] 能量单位/秒,而产生功率仅为 #cf_span[0.5] 能量单位/秒。
**Definitions** Let $ n \in \mathbb{Z}^+ $ be the number of elements. Let $ m \in \mathbb{Z}_{\geq 0} $ be the number of wires. Let $ G = (V, E) $ be an undirected graph with $ V = \{1, 2, \dots, n\} $ and $ E \subseteq \binom{V}{2} $, $ |E| = m $. Let $ x_i \in \mathbb{R}_{\geq 0} $ be the controller value assigned to element $ i \in V $. **Constraints** 1. The total energy consumption per second is $ \sum_{i=1}^n x_i^2 $. 2. The total energy production per second is $ \sum_{\{i,j\} \in E} x_i x_j $. 3. We require: $$ \sum_{\{i,j\} \in E} x_i x_j \geq \sum_{i=1}^n x_i^2 $$ 4. Not all $ x_i = 0 $: $ \exists\, i \in V $ such that $ x_i > 0 $. 5. We seek **integer** values $ x_i \in \{0, 1, \dots, 10^6\} $ satisfying the above. **Objective** Determine whether there exists an assignment $ \mathbf{x} = (x_1, \dots, x_n) \in \mathbb{Z}_{\geq 0}^n $, not identically zero, such that: $$ \sum_{\{i,j\} \in E} x_i x_j \geq \sum_{i=1}^n x_i^2 $$ If yes, output any such assignment; otherwise, output "NO".
Samples
Input #1
4
 
4 4
1 2
2 3
3 4
4 2
 
3 2
2 3
3 1
 
4 6
1 2
3 4
4 2
1 4
1 3
3 2
 
10 9
2 1
3 2
5 2
6 2
2 7
2 8
2 9
2 10
4 2
Output #1
YES
1 2 2 1
NO
YES
1 1 1 1
YES
1 5 1 1 1 1 1 1 1 1
API Response (JSON)
{
  "problem": {
    "name": "E. Perpetual Motion Machine",
    "description": {
      "content": "Developer Petr thinks that he invented a perpetual motion machine. Namely, he has a lot of _elements_, which work in the following way. Each element has one controller that can be set to any non-nega",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF830E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Developer Petr thinks that he invented a perpetual motion machine. Namely, he has a lot of _elements_, which work in the following way.\n\nEach element has one controller that can be set to any non-nega...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "开发者 Petr 认为他发明了一台永动机。具体来说,他有许多 _元素_,其工作方式如下:\n\n每个元素有一个控制器,可以设置为任意非负实数。如果控制器被设置为值 #cf_span[x],则该控制器每秒消耗 #cf_span[x2] 能量单位。同时,任意两个由导线连接的元素每秒产生 #cf_span[y·z] 能量单位,其中 #cf_span[y] 和 #cf_span[z] 分别是它们控制器上设置的...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z}^+ $ be the number of elements.  \nLet $ m \\in \\mathbb{Z}_{\\geq 0} $ be the number of wires.  \nLet $ G = (V, E) $ be an undirected graph with $ V = \\{1, 2, \\dots...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments