CF1909E.Multiple Lamps
提高+/省选-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
⠀
You have n lamps, numbered from 1 to n. Initially, all the lamps are turned off.
You also have n buttons. The i-th button toggles all the lamps whose index is a multiple of i. When a lamp is toggled, if it was off it turns on, and if it was on it turns off.
You have to press some buttons according to the following rules.
- You have to press at least one button.
- You cannot press the same button multiple times.
- You are given m pairs (ui,vi). If you press the button ui, you also have to press the button vi (at any moment, not necessarily after pressing the button ui). Note that, if you press the button vi, you don't need to press the button ui.
You don't want to waste too much electricity. Find a way to press buttons such that at the end at most ⌊n/5⌋ lamps are on, or print −1 if it is impossible.
⠀
你有 n 盏灯,编号从 1 到 n。初始时,所有灯均处于关闭状态。
你还有 n 个按钮。第 i 个按钮会切换所有下标为 i 的倍数的灯的状态:若灯当前为关闭,则变为开启;若为开启,则变为关闭。
你需要按照以下规则按下若干按钮:
- 至少要按下其中一个按钮;
- 同一个按钮不能被多次按下;
- 给定 m 对 (ui,vi)。若你按下了按钮 ui,则你也必须按下按钮 vi(可在任意时刻按下,不一定要在按下 ui 之后立即按下)。注意:若你按下了按钮 vi,则不一定需要按下按钮 ui。
你不希望消耗过多电能。请找出一种按钮按下方案,使得最终最多有 ⌊n/5⌋ 盏灯处于开启状态;若不存在这样的方案,请输出 −1。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers n and m (1≤n≤2⋅105, 0≤m≤2⋅105) — the number of lamps and the number of pairs, respectively.
Each of the next m lines contains two integers ui, vi (1≤ui,vi≤n, ui=vi). If you press the button ui, you also have to press the button vi. It is guaranteed that the pairs (ui,vi) are distinct.
It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 m(1≤n≤2⋅105,0≤m≤2⋅105),分别表示灯的数量和配对数量。
接下来的 m 行中,每行包含两个整数 ui、vi(1≤ui,vi≤n,ui=vi):若按下按钮 ui,则也必须按下按钮 vi。保证所有配对 (ui,vi) 互不相同。
保证所有测试用例的 n 之和以及 m 之和均不超过 2⋅105。
输出格式
For each test case:
- If there is no choice of buttons that makes at most ⌊n/5⌋ lamps on at the end, output a single line containing −1.
- Otherwise, output two lines. The first line should contain an integer k (1≤k≤n) — the number of pressed buttons. The second line should contain k integers b1,b2,…,bk (1≤bi≤n) — the indices of the pressed buttons (in any order). The bi must be distinct, and at the end at most ⌊n/5⌋ lamps must be turned on.
对于每个测试用例:
- 如果不存在一种按钮选择方案,使得最终亮着的灯的数量不超过 ⌊n/5⌋,则输出一行,包含单个整数 −1。
- 否则,输出两行。第一行应包含一个整数 k(1≤k≤n)—— 按下的按钮数量;第二行应包含 k 个整数 b1,b2,…,bk(1≤bi≤n)—— 按下的按钮的编号(顺序任意)。这些 bi 必须互不相同,且最终亮着的灯的数量不得超过 ⌊n/5⌋。
输入输出样例
输入#1
4 4 0 5 2 4 1 5 1 15 9 7 8 8 9 9 10 10 9 11 1 12 2 13 3 14 4 15 5 5 4 1 2 2 3 3 4 4 5
输出#1
-1 4 3 5 1 2 3 8 9 10 1 5
说明/提示
In the first test case, you need to turn at most ⌊4/5⌋ lamps on, which means that no lamp can be turned on. You can show that no choice of at least one button turns 0 lamps on.
In the second test case, you can press buttons 3, 5, 1, 2.
- Initially, all the lamps are off;
- after pressing button 3, the lamps whose index is a multiple of 3 (i.e., 3) are toggled, so lamp 3 is turned on;
- after pressing button 5, the lamps whose index is a multiple of 5 (i.e., 5) are toggled, so lamps 3, 5 are turned on;
- after pressing button 1, the lamps whose index is a multiple of 1 (i.e., 1, 2, 3, 4, 5) are toggled, so lamps 1, 2, 4 are turned on;
- after pressing button 2, the lamps whose index is a multiple of 2 (i.e., 2, 4) are toggled, so lamp 1 is turned on.
This is valid because
- you pressed at least one button;
- you pressed all the buttons at most once;
- you pressed button u2=5, which means that you had to also press button v2=1: in fact, button 1 has been pressed;
- at the end, only lamp 1 is on.
In the third test case, pressing the buttons 8, 9, 10 turns only the lamps 8, 9, 10 on.
在第一个测试用例中,你最多需要打开 ⌊4/5⌋ 盏灯,即不允许打开任何一盏灯。你可以验证:无论选择至少一个按钮进行按压,都无法使恰好 0 盏灯被打开。
在第二个测试用例中,你可以依次按压按钮 3、5、1、2。
- 初始时,所有灯均处于关闭状态;
- 按压按钮 3 后,所有下标为 3 的倍数的灯(即灯 3)被翻转,因此灯 3 被打开;
- 按压按钮 5 后,所有下标为 5 的倍数的灯(即灯 5)被翻转,因此灯 3 和灯 5 处于开启状态;
- 按压按钮 1 后,所有下标为 1 的倍数的灯(即灯 1、2、3、4、5)被翻转,因此灯 1、2、4 被打开;
- 按压按钮 2 后,所有下标为 2 的倍数的灯(即灯 2、4)被翻转,因此最终仅有灯 1 处于开启状态。
该方案是合法的,因为:
- 你按压了至少一个按钮;
- 每个按钮至多被按压一次;
- 你按压了按钮 u2=5,这意味着你必须也按压按钮 v2=1;事实上,按钮 1 确实已被按压;
- 最终,仅灯 1 处于开启状态。
在第三个测试用例中,按压按钮 8、9、10 后,仅有灯 8、9、10 被打开。
输入解题思路,AI测评打分。不知道怎么写?