AT_arc232_b.Two Flips

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are NN cards numbered 11 to NN. Card ii has an integer AiA_i written on its front side and an integer BiB_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).

You can perform the following operation zero or more times.

  • Choose two different cards such that the integers written on their currently facing-up sides are equal, and flip both of them simultaneously.

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

You are given TT test cases; solve each of them.

有 NN 张编号为 11 至 NN 的卡片。第 ii 张卡片正面写有一个整数 AiA_i,背面写有一个整数 BiB_i。每张卡片的正面与背面所写的整数互不相同。初始时,所有卡片均正面朝上(即正面朝上)。

你可以执行以下操作零次或多次:

  • 选择两张不同的卡片,使得它们当前朝上的那一面所写的整数相等,并同时将这两张卡片翻转。

判断是否能够达到如下状态:卡片 11 和卡片 22 均背面朝上(即背面朝上),其余所有卡片均正面朝上。若可以达到该状态,求所需的最少操作次数。

你将得到 TT 组测试数据,请对每组数据分别求解。

输入格式

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

TT
case1\mathrm{case}_1
case2\mathrm{case}_2
⋮\vdots
caseT\mathrm{case}_T

Each test case is given in the following format:

NN
A1A_1 B1B_1
A2A_2 B2B_2
⋮\vdots
ANA_N BNB_N

输入从标准输入中按以下格式给出:

TT
case1\mathrm{case}_1
case2\mathrm{case}_2
⋮\vdots
caseT\mathrm{case}_T

每个测试用例按以下格式给出:

NN
A1A_1 B1B_1
A2A_2 B2B_2
⋮\vdots
ANA_N BNB_N

输出格式

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

输出 TT 行。第 ii 行应在第 ii 个测试用例中无法到达目标状态时输出 -1,否则输出所需的最少操作次数。

输入输出样例

  • 输入#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

    输出#1

    3
    4
    1
    -1
    2
    5
    7
    -1

说明/提示

Sample 1 Explanation:
In the first test case, choosing the pairs of cards (1,3),(2,4),(3,4)(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)(1,3),(1,2),(1,4),(3,4) in this order reaches the target state in four operations. Card 11 may be flipped multiple times along the way.

Constraints

  • 1≤T≤1250001 \le T \le 125000
  • 2≤N≤2500002 \le N \le 250000
  • 1≤Ai,Bi≤2N1 \le A_i,B_i \le 2N (1≤i≤N)(1 \le i \le N)
  • Ai≠BiA_i \ne B_i (1≤i≤N)(1 \le i \le N)
  • The sum of NN over all test cases in a single input is at most 250000250000.
  • All input values are integers.

样例 1 解释:
在第一个测试用例中,按顺序选择卡片对 (1,3)(1,3)、(2,4)(2,4)、(3,4)(3,4),可在三次操作内达到目标状态。无法在两次或更少的操作内达到目标状态。

在第二个测试用例中,按顺序选择卡片对 (1,3)(1,3)、(1,2)(1,2)、(1,4)(1,4)、(3,4)(3,4),可在四次操作内达到目标状态。在此过程中,卡片 11 可能被多次翻转。

约束条件

  • 1≤T≤1250001 \le T \le 125000
  • 2≤N≤2500002 \le N \le 250000
  • 1≤Ai,Bi≤2N1 \le A_i,B_i \le 2N (1≤i≤N)(1 \le i \le N)
  • Ai≠BiA_i \ne B_i (1≤i≤N)(1 \le i \le N)
  • 单个输入中所有测试用例的 NN 值之和不超过 250000250000。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页