#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <vector>
#include <set>
#include <stack>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int mxn = 2500;
int n, x, y;
int id[mxn], vis[mxn], in[mxn], ot[mxn], ud[mxn];
vector<int> g[mxn];
set<int> s[mxn];
stack<int> stk;
int k, kx, ky;
void die(){
cout << "detention" << endl;
exit(0);
}
//branch group
void spush(int c){
ud[c] = 1;
stk.push(c);
}
int spop(){
int c = stk.top();
stk.pop();
ud[c] = 0;
return c;
}
bool sol(), solin(), solout();
bool sol(){
if(kx > x || ky > y || kx + ky + stk.size() > x + y) return 0;
if(stk.empty()) return 1;
return solin() || solout();
}
bool solin(){
int c = spop();
int m = 0;
kx++, in[c] = 1;
for(int i : g[c]){
ky += ot[i];
if(in[i] || ot[i] || ud[i]) continue;
m++;
spush(i);
}
bool ret = sol();
kx--, in[c] = 0;
for(int i : g[c]) ky -= ot[i];
while(m--) spop();
spush(c);
if(ret){
vis[c] = 1;
s[k].insert(c);
}
return ret;
}
bool solout(){
int c = spop();
ot[c] = 1;
for(int i : g[c]) ky += in[i];
bool ret = sol();
ot[c] = 0;
for(int i : g[c]) ky -= in[i];
spush(c);
return ret;
}
//end branch group
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> x >> y;
for(int i = 0; i < n; i++){
int m;
cin >> m;
for(int j = 0; j < m; j++){
int v;
cin >> v;
g[i].push_back(v);
}
sort(g[i].begin(), g[i].end());
}
for(int i = 0; i < n; i++){
id[i] = -1;
for(int j : g[i]){
auto it = lower_bound(g[j].begin(), g[j].end(), i);
if(it == g[j].end() || *it != i) die();
}
}
memset(id, -1, sizeof(id));
for(; k < n; k++){
if(vis[k]) continue;
spush(k);
if(!solin()) die();
spop();
loop:
for(int i : s[k]){
int m = id[i];
if(~m && m != k){
vector<int> v;
for(int j : s[k]){
if(id[j] == m){
v.push_back(j);
id[j] = -1;
}
}
int w = 0, z = 0;
for(int j : v)
for(int l : g[j]){
w += id[l] == k;
z += id[l] == m;
}
if(w < z){
for(int j : v){
id[j] = m;
s[k].erase(j);
s[m].insert(j);
}
goto loop;
}else{
for(int j : v){
id[j] = k;
}
}
}else{
id[i] = k;
}
}
}
cout << "home" << endl;
int ret = 0;
for(int i = 0; i < k; i++) ret += !s[i].empty();
cout << ret << endl;
for(int i = 0; i < k; i++){
if(s[i].empty()) continue;
cout << s[i].size();
for(int j : s[i]) cout << " " << j;
cout << endl;
}
return 0;
}
Compilation message
friends.cpp: In function 'bool sol()':
friends.cpp:44:46: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(kx > x || ky > y || kx + ky + stk.size() > x + y) return 0;
~~~~~~~~~~~~~~~~~~~~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
4 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
640 KB |
Output is correct |
11 |
Correct |
6 ms |
640 KB |
Output is correct |
12 |
Correct |
6 ms |
640 KB |
Output is correct |
13 |
Correct |
6 ms |
768 KB |
Output is correct |
14 |
Correct |
6 ms |
640 KB |
Output is correct |
15 |
Correct |
6 ms |
640 KB |
Output is correct |
16 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
640 KB |
Output is correct |
19 |
Correct |
6 ms |
640 KB |
Output is correct |
20 |
Correct |
6 ms |
640 KB |
Output is correct |
21 |
Correct |
6 ms |
768 KB |
Output is correct |
22 |
Correct |
6 ms |
640 KB |
Output is correct |
23 |
Correct |
6 ms |
640 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
25 |
Correct |
4 ms |
512 KB |
Output is correct |
26 |
Incorrect |
7 ms |
768 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |