第三届 AtCoder Japan Open -预选-(第 232 场 AtCoder Regular Contest++)
B
两次翻转
Two Flips
AtCoder
⌀ 2656
原题
← AC →

两次翻转AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

There are $N$ cards numbered $1$ to $N$. Card $i$ has an integer $A_i$ written on its front side and an integer $B_i$ written on its back side. For every card, the integers written on the front and back sides are different. Initially, all cards are placed face up (front side up).

有 $N$ 张编号为 $1$ 至 $N$ 的卡片。第 $i$ 张卡片的正面写有整数 $A_i$,背面写有整数 $B_i$。对于每张卡片,正面和背面写有的整数均不相同。初始时,所有卡片均正面朝上。AI (CI翻译)

You can perform the following operation zero or more times.

可以进行以下操作零次或多次。AI (CI翻译)

  • Choose two different cards such that the integers written on their currently facing-up sides are equal, and flip both of them simultaneously.
  • 选择两张不同的卡片,要求它们当前朝上的一面所写的整数相等,然后同时翻转这两张卡片。
AI (CI翻译)

Determine whether it is possible to reach the state where cards $1$ and $2$ are face down (back side up) and all other cards are face up. If it is possible, find the minimum number of operations required.

判断是否可以达到这样的状态:卡片 $1$ 和 $2$ 背面朝上,而其余所有卡片正面朝上。若可以,求所需的最少操作次数。AI (CI翻译)

You are given $T$ test cases; solve each of them.

给定 $T$ 个测试用例,请分别求解。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \le T \le 125000$
  • $2 \le N \le 250000$
  • $1 \le A_i,B_i \le 2N$ $(1 \le i \le N)$
  • $A_i \ne B_i$ $(1 \le i \le N)$
  • The sum of $N$ over all test cases in a single input is at most $250000$.
  • All input values are integers.
  • $1 \le T \le 125000$
  • $2 \le N \le 250000$
  • $1 \le A_i,B_i \le 2N$ $(1 \le i \le N)$
  • $A_i \ne B_i$ $(1 \le i \le N)$
  • 单个输入中所有测试用例的 $N$ 之和不超过 $250000$。
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入给出,格式如下:AI (CI翻译)

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
AI (CI翻译)

Each test case is given in the following format:

每个测试用例的格式如下:AI (CI翻译)

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output $T$ lines. The $i$-th line should contain -1 if it is impossible to reach the target state in the $i$-th test case, and otherwise the minimum number of operations required.

输出 $T$ 行。第 $i$ 行中,如果无法在第 $i$ 个测试用例中达到目标状态,则输出 -1;否则输出所需的最少操作次数。AI (CI翻译)

样例

Input #1
8
4
1 4
2 5
1 3
2 3
4
1 2
2 1
1 3
1 3
2
1 2
1 2
2
1 2
2 1
3
1 2
3 4
1 3
5
1 4
4 5
1 2
2 3
3 1
8
1 9
7 10
1 2
3 2
3 4
5 4
5 6
7 6
3
1 2
3 4
2 3
Output #1
3
4
1
-1
2
5
7
-1
In the first test case, choosing the pairs of cards $(1,3),(2,4),(3,4)$ in this order reaches the target state in three operations. It is impossible to reach the target state in two or fewer operations. In the second test case, choosing the pairs of cards $(1,3),(1,2),(1,4),(3,4)$ in this order reaches the target state in four operations. Card $1$ may be flipped multiple times along the way.
题目信息
题号
arc232_b
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。