forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathH.cpp
More file actions
153 lines (137 loc) · 4.17 KB
/
H.cpp
File metadata and controls
153 lines (137 loc) · 4.17 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
const int maxv = 1000;
const int maxe = 50000;
struct EdmondsLawler {
int n, E, start, finish, newRoot, qsize, adj[maxe], next[maxe], last[maxv], mat[maxv], que[maxv], dad[maxv], root[maxv];
bool inque[maxv], inpath[maxv], inblossom[maxv];
void init(int _n) {
n = _n; E = 0;
for(int x=1; x<=n; ++x) { last[x] = -1; mat[x] = 0; }
}
void add(int u, int v) {
adj[E] = v; next[E] = last[u]; last[u] = E++;
}
int lca(int u, int v) {
for(int x=1; x<=n; ++x) inpath[x] = false;
while (true) {
u = root[u];
inpath[u] = true;
if (u == start) break;
u = dad[mat[u]];
}
while (true) {
v = root[v];
if (inpath[v]) break;
v = dad[mat[v]];
}
return v;
}
void trace(int u) {
while (root[u] != newRoot) {
int v = mat[u];
inblossom[root[u]] = true;
inblossom[root[v]] = true;
u = dad[v];
if (root[u] != newRoot) dad[u] = v;
}
}
void blossom(int u, int v) {
for(int x=1; x<=n; ++x) inblossom[x] = false;
newRoot = lca(u, v);
trace(u); trace(v);
if (root[u] != newRoot) dad[u] = v;
if (root[v] != newRoot) dad[v] = u;
for(int x=1; x<=n; ++x) if (inblossom[root[x]]) {
root[x] = newRoot;
if (!inque[x]) {
inque[x] = true;
que[qsize++] = x;
}
}
}
bool bfs() {
for(int x=1; x<=n; ++x){
inque[x] = false;
dad[x] = 0;
root[x] = x;
}
qsize = 0;
que[qsize++] = start;
inque[start] = true;
finish = 0;
for(int i=0; i<qsize; ++i) {
int u = que[i];
for (int e = last[u]; e != -1; e = next[e]) {
int v = adj[e];
if (root[v] != root[u] && v != mat[u]) {
if (v == start || (mat[v] > 0 && dad[mat[v]] > 0)) blossom(u, v);
else if (dad[v] == 0) {
dad[v] = u;
if (mat[v] > 0) que[qsize++] = mat[v];
else {
finish = v;
return true;
}
}
}
}
}
return false;
}
void enlarge() {
int u = finish;
while (u > 0) {
int v = dad[u], x = mat[v];
mat[v] = u;
mat[u] = v;
u = x;
}
}
int maxmat() {
for(int x=1; x<=n; ++x) if (mat[x] == 0) {
start = x;
if (bfs()) enlarge();
}
int ret = 0;
for(int x=1; x<=n; ++x) if (mat[x] > x) ++ret;
return ret;
}
} edmonds;
const int MN = 1011;
int c[MN][MN];
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
freopen("perfect.in", "r", stdin);
freopen("perfect.out", "w", stdout);
int n;
while (cin >> n) {
memset(c, 0, sizeof c);
FOR(i,1,n) {
int k; cin >> k;
while (k--) {
int j; cin >> j;
c[i][j] = 1;
}
}
edmonds.init(n);
FOR(i,1,n) FOR(j,i+1,n)
if (c[i][j] && c[j][i]) {
edmonds.add(i, j);
edmonds.add(j, i);
}
int res = edmonds.maxmat();
if (res == n/2) cout << "YES";
else cout << "NO";
cout << endl;
}
}