#include <bits/stdc++.h>
using namespace std;
int n, p, q;
bool f[3000], m[3000];
vector<int> e[3000];
void mark(vector<int> & g, bool v) {
for (int i : g)
m[i] = v;
}
bool anyMarked(vector<int> & g) {
for (int i : g)
if (m[i])
return true;
return false;
}
bool valid(vector<int> & g) {
mark(g, true);
int cq = 0;
for (int i : g)
for (int j : e[i])
cq += !m[j];
return cq <= q;
}
vector<int> withoutMarked(vector<int> & g) {
vector<int> h;
for (int i : g)
if (!m[i])
h.push_back(i);
return h;
}
int main() {
cin >> n >> p >> q;
set<pair<int, int>> ed;
for (int i = 0; i < n; i++) {
int m, j;
cin >> m;
while (m--) {
cin >> j;
e[i].push_back(j);
ed.emplace(i, j);
}
}
for (auto [i, j] : ed)
if (ed.find({j, i}) == ed.end()) {
cout << "detention\n";
return 0;
}
vector<vector<int>> par;
for (int i = 0; i < n; i++) {
if (f[i])
continue;
vector<int> g = {i};
for (int b = 0; b < (1 << (p + q - 1)) && !f[i];) {
int gi = 0, gj = 0, cp = 1, cq = 0, k = p + q - 1;
m[i] = true;
while (gi < (int) g.size()) {
if (gj == (int) e[g[gi]].size())
gi++, gj = 0;
else if (m[e[g[gi]][gj]])
gj++;
else if (k == 0)
break;
else {
bool ig = (b >> --k) & 1;
if (ig && cp == p || !ig && cq == q)
break;
if (ig)
cp++, m[e[g[gi]][gj]] = true, g.push_back(e[g[gi]][gj]);
else
cq++;
gj++;
}
}
if (gi < (int) g.size()) {
mark(g, false);
g.resize(1);
b &= ~((1 << k) - 1);
b += 1 << k;
} else {
for (int j : g)
f[j] = true;
break;
}
}
if (!f[i]) {
cout << "detention\n";
return 0;
}
for (vector<int> & h : par) {
if (!anyMarked(h))
continue;
vector<int> hw = withoutMarked(h);
mark(g, false);
if (valid(hw))
mark(h, false), h = hw;
else
g = withoutMarked(g), mark(h, false);
mark(g, true);
}
mark(g, false);
par.push_back(g);
}
vector<vector<int>> res;
for (vector<int> & g : par)
if (!g.empty())
res.push_back(g);
cout << "home\n" << res.size() << "\n";
for (vector<int> & g : res) {
cout << g.size();
for (int i : g)
cout << " " << i;
cout << "\n";
}
}
Compilation message
friends.cpp: In function 'int main()':
friends.cpp:72:18: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
72 | if (ig && cp == p || !ig && cq == q)
| ~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
376 KB |
Output is correct |
7 |
Correct |
0 ms |
368 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
372 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
372 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
500 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
8 ms |
844 KB |
Output is correct |
11 |
Correct |
5 ms |
892 KB |
Output is correct |
12 |
Correct |
8 ms |
852 KB |
Output is correct |
13 |
Correct |
8 ms |
788 KB |
Output is correct |
14 |
Correct |
5 ms |
896 KB |
Output is correct |
15 |
Correct |
5 ms |
828 KB |
Output is correct |
16 |
Correct |
4 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
376 KB |
Output is correct |
7 |
Correct |
0 ms |
368 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
372 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
500 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
8 ms |
844 KB |
Output is correct |
19 |
Correct |
5 ms |
892 KB |
Output is correct |
20 |
Correct |
8 ms |
852 KB |
Output is correct |
21 |
Correct |
8 ms |
788 KB |
Output is correct |
22 |
Correct |
5 ms |
896 KB |
Output is correct |
23 |
Correct |
5 ms |
828 KB |
Output is correct |
24 |
Correct |
4 ms |
768 KB |
Output is correct |
25 |
Correct |
1 ms |
368 KB |
Output is correct |
26 |
Correct |
23 ms |
980 KB |
Output is correct |
27 |
Correct |
14 ms |
932 KB |
Output is correct |
28 |
Correct |
19 ms |
892 KB |
Output is correct |
29 |
Correct |
28 ms |
724 KB |
Output is correct |
30 |
Correct |
26 ms |
888 KB |
Output is correct |
31 |
Correct |
7 ms |
852 KB |
Output is correct |
32 |
Correct |
4 ms |
828 KB |
Output is correct |
33 |
Correct |
284 ms |
1492 KB |
Output is correct |
34 |
Correct |
57 ms |
1368 KB |
Output is correct |