#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2500;
int n;
vector <int> adj[MAX_N];
unordered_map <int, bool> isAdj[MAX_N];
set <int> solution, group[MAX_N];
vector <int> frecv[MAX_N];
bool vis[MAX_N];
void findGroup( multiset <int> candidates, set<int> uniqueCandidates, set <int> chosen, int p, int q ) {
if ( uniqueCandidates.size() > p + q )
return;
if ( candidates.size() == 0 ) {
if ( chosen.size() > solution.size() )
solution = chosen;
return;
}
int v = *candidates.begin();
if ( p > 0 ) {
multiset <int> newCandidates = candidates;
set<int> newUniqueCandidates = uniqueCandidates;
set <int> newChosen = chosen;
newCandidates.erase( v );
newUniqueCandidates.erase( v );
newChosen.insert( v );
for ( int u: adj[v] ) {
if ( chosen.find( u ) == chosen.end() ) {
newCandidates.insert( u );
newUniqueCandidates.insert( u );
}
}
findGroup( newCandidates, newUniqueCandidates, newChosen, p - 1, q );
}
if ( q > 0 ) {
multiset <int> newCandidates = candidates;
set<int> newUniqueCandidates = uniqueCandidates;
set <int> newChosen = chosen;
newCandidates.erase( newCandidates.lower_bound( v ) );
if ( newCandidates.find( v ) == newCandidates.end() )
newUniqueCandidates.erase( v );
findGroup( newCandidates, newUniqueCandidates, newChosen, p, q - 1 );
}
}
bool verif() {
for ( int v = 0; v < n; v++ ) {
if ( frecv[v].size() > 1 )
return false;
}
return true;
}
bool check( set <int> a, int q ) {
for ( int v: a ) {
for ( int u: adj[v] ) {
if ( a.find( u ) == a.end() )
q--;
}
}
return (q >= 0);
}
int main() {
int p, q;
cin >> n >> p >> q;
for ( int u = 0; u < n; u++ ) {
int k;
cin >> k;
for ( int i = 0; i < k; i++ ) {
int v;
cin >> v;
adj[u].push_back( v );
isAdj[u][v] = true;
}
}
for ( int u = 0; u < n; u++ ) {
if ( adj[u].size() > p + q - 1 ) {
cout << "detention\n";
return 0;
}
for ( int v: adj[u] ) {
if ( (isAdj[u][v] ^ isAdj[v][u]) == 1 ) {
cout << "detention\n";
return 0;
}
}
}
for ( int u = 0; u < n; u++ ) {
if ( vis[u] )
continue;
multiset <int> candidates;
set<int> uniqueCandidates;
set <int> chosen;
candidates.insert( u );
uniqueCandidates.insert( u );
solution.clear();
findGroup( candidates, uniqueCandidates, chosen, p, q );
if ( solution.size() == 0 ) {
cout << "detention\n";
return 0;
}
for ( int v: solution ) {
vis[v] = true;
frecv[v].push_back( u );
group[u].insert( v );
}
}
while ( !verif() ) {
int x, y;
for ( int v = 0; v < n; v++ ) {
if ( frecv[v].size() > 1 ) {
x = frecv[v][0], y = frecv[v][1];
break;
}
}
set <int> a = group[x], b = group[y];
set <int> a1 = a, b1 = b;
for ( int v: b1 )
a1.erase( v );
set <int> a2 = a, b2 = b;
for ( int v: a2 )
b2.erase( v );
if ( check( a1, q ) ) {
group[x] = a1;
group[y] = b1;
} else if ( check( b2, q ) ) {
group[x] = a2;
group[y] = b2;
}
for ( int v = 0; v < n; v++ )
frecv[v].clear();
for ( int u = 0; u < n; u++ ) {
for ( int v: group[u] )
frecv[v].push_back( u );
}
}
int ans = 0;
for ( int u = 0; u < n; u++ ) {
if ( group[u].size() > 0 )
ans++;
}
cout << "home\n";
cout << ans << "\n";
for ( int u = 0; u < n; u++ ) {
if ( group[u].size() == 0 )
continue;
cout << group[u].size() << " ";
for ( int v: group[u] )
cout << v << " ";
cout << "\n";
}
return 0;
}
Compilation message
friends.cpp: In function 'void findGroup(std::multiset<int>, std::set<int>, std::set<int>, int, int)':
friends.cpp:14:34: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
14 | if ( uniqueCandidates.size() > p + q )
| ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
friends.cpp: In function 'int main()':
friends.cpp:84:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
84 | if ( adj[u].size() > p + q - 1 ) {
| ~~~~~~~~~~~~~~^~~~~~~~~~~
friends.cpp:119:16: warning: 'y' may be used uninitialized in this function [-Wmaybe-uninitialized]
119 | int x, y;
| ^
friends.cpp:119:13: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
119 | int x, y;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
3 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
3 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
5 ms |
1372 KB |
Output is correct |
11 |
Correct |
9 ms |
1376 KB |
Output is correct |
12 |
Correct |
19 ms |
1432 KB |
Output is correct |
13 |
Correct |
17 ms |
1372 KB |
Output is correct |
14 |
Correct |
4 ms |
1372 KB |
Output is correct |
15 |
Correct |
3 ms |
1364 KB |
Output is correct |
16 |
Correct |
4 ms |
1112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
1112 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
5 ms |
1372 KB |
Output is correct |
19 |
Correct |
9 ms |
1376 KB |
Output is correct |
20 |
Correct |
19 ms |
1432 KB |
Output is correct |
21 |
Correct |
17 ms |
1372 KB |
Output is correct |
22 |
Correct |
4 ms |
1372 KB |
Output is correct |
23 |
Correct |
3 ms |
1364 KB |
Output is correct |
24 |
Correct |
4 ms |
1112 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
56 ms |
1468 KB |
Output is correct |
27 |
Correct |
40 ms |
1636 KB |
Output is correct |
28 |
Correct |
50 ms |
1568 KB |
Output is correct |
29 |
Correct |
25 ms |
1116 KB |
Output is correct |
30 |
Correct |
50 ms |
1364 KB |
Output is correct |
31 |
Correct |
5 ms |
1372 KB |
Output is correct |
32 |
Correct |
3 ms |
1372 KB |
Output is correct |
33 |
Correct |
290 ms |
2132 KB |
Output is correct |
34 |
Correct |
59 ms |
1868 KB |
Output is correct |