CF1909E.Multiple Lamps
普及/提高-
通过率:0%
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.
输入格式
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 .
输出格式
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.
输入输出样例
输入#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.