codeforces Split Into Two Sets

Polycarp was recently given a set of n (number n — even) dominoes. Each domino contains two integers from 11 to n.

Can he divide all the dominoes into two sets so that all the numbers on the dominoes of each set are different? Each domino must go into exactly one of the two sets.

For example, if he has 44 dominoes: {1,4}{1,4}, {1,3}{1,3}, {3,2}{3,2} and {4,2}{4,2}, then Polycarp will be able to divide them into two sets in the required way. The first set can include the first and third dominoes ({1,4}{1,4} and {3,2}{3,2}), and the second set — the second and fourth ones ({1,3}{1,3} and {4,2}{4,2}).

Input

The first line contains a single integer t (1≤t≤104) — the number of test cases.

The descriptions of the test cases follow.

The first line of each test case contains a single even integer n(2≤n≤2⋅105) — the number of dominoes.

The next n lines contain pairs of numbers ai and bi (1≤ai,bi≤n) describing the numbers on the i-th domino.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case print:

  • YES, if it is possible to divide n dominoes into two sets so that the numbers on the dominoes of each set are different;
  • NO if this is not possible.

You can print YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer).

Example

input

6

4

1 2

4 3

2 1

3 4

6

1 2

4 5

1 3

4 6

2 3

5 6

2

1 1

2 2

2

1 2

2 1

8

2 1

1 2

4 3

4 3

5 6

5 7

8 6

7 8

8

1 2

2 1

4 3

5 3

5 4

6 7

8 6

7 8

output

YES
NO
NO
YES
YES
NO

Note

In the first test case, the dominoes can be divided as follows:

  • First set of dominoes: [{1,2},{4,3}][{1,2},{4,3}]
  • Second set of dominoes: [{2,1},{3,4}][{2,1},{3,4}]

In other words, in the first set we take dominoes with numbers 11 and 22, and in the second set we take dominoes with numbers 33 and 44.

In the second test case, there's no way to divide dominoes into 22 sets, at least one of them will contain repeated number.

刚通过囚犯那个题学了种类并查集,以为自己理解了,但是完全不知道为什么这个题是种类并查集,难道把每个牌上的两个数字想象成敌对关系?????

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=4e4+10;
const int N=4e5+10;
int fa[N];
int n;
int find(int x)
{
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int a,int b)
{
	int x=find(a);
	int y=find(b);
	if(x!=y)
	fa[x]=y;
}
void solve()
{
	cin>>n;
	map<int,int> mp;
	for(int i=1;i<=2*n;i++) fa[i]=i;
	bool flag=1;
	for(int i=1;i<=n;i++)
	{
		int a,b;
		cin>>a>>b;
		mp[a]++;mp[b]++;
		if(mp[a]>2||mp[b]>2) flag=0;
		if(a==b) flag=0;
		if(find(a)==find(b)) flag=0;
		else{
			merge(a,b+n);
			merge(b,a+n);
		}
	}
	if(flag) cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值