-
Notifications
You must be signed in to change notification settings - Fork 0
/
E.cpp
109 lines (106 loc) · 2.09 KB
/
E.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//Took help
#include<bits/stdc++.h>
#include<string>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
long long vis[200005],globalFlag;
set<long long> adj[200005];
void DFS(long long v,long long pr)
{
if(pr==-1)
vis[v]=1;
else
{
long long col=vis[pr];
if(vis[v]!=-1)
{
if(vis[v]==col)
globalFlag=1;
return;
}
else
{
if(col==1)
vis[v]=2;
else
vis[v]=1;
}
}
for(auto nxt:adj[v])
{
if(nxt==pr)
continue;
if(vis[nxt]!=-1)
{
long long childCol=vis[nxt];
if(childCol==vis[v])
{
globalFlag=1;
continue;
}
}
DFS(nxt,v);
}
}
int main()
{
fastio;
long long t,n,i,p,a,b;
cin>>t;
while(t--)
{
globalFlag=0;
cin>>n;
for(i=0;i<=n;i++)
{
adj[i].clear();
vis[i]=-1;
}
map<long long,long long> mp;
map<long long,long long> prevMp;
p=0;
for(i=0;i<n;i++)
{
cin>>a>>b;
if(a==b)
{
p=1;
}
mp[a]++;
mp[b]++;
if(mp[a]==1)
prevMp[a]=i+1;
else if(mp[a]==2)
{
adj[prevMp[a]].insert(i+1);
adj[i+1].insert(prevMp[a]);
}
else
p=1;
if(mp[b]==1)
prevMp[b]=i+1;
else if(mp[b]==2)
{
adj[prevMp[b]].insert(i+1);
adj[i+1].insert(prevMp[b]);
}
else
p=1;
}
if(p==1)
{
cout<<"NO\n";
continue;
}
for(i=1;i<=n;i++)
{
if(vis[i]==-1)
DFS(i,-1);
}
if(globalFlag==1)
cout<<"NO\n";
else
cout<<"YES\n";
}
return 0;
}