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:
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:
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
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 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"<>t;
while(t--)
{
solve();
}
return 0;
}