#include <bits/stdc++.h>
#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
#define REP(x, a, b) for (int x = a; x < b; ++x)
#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(_, 0, n) cout << A[_] << " "; cout << endl; }
using namespace std;
typedef long long LL;
typedef pair <int, int> II;
const int N = 5e3 + 10;
const int INF = 0x3f3f3f3f;
int n, first, last;
int f[N], p[N], Q[5 * N];
vector <int> boss[N], adj[N];
void DFS(int u) {
f[u] = 1;
REP(k, 0, adj[u].size()) {
int v = adj[u][k];
DFS(v);
f[u] += f[v];
}
}
int Compute(int u) {
FOR(i, 1, n) {
adj[i].clear();
p[i] = -1;
}
p[u] = 0;
first = 1; last = 0;
Q[++last] = u;
while (first <= last) {
int u = Q[first++];
REP(k, 0, boss[u].size()) {
int v = boss[u][k];
if (p[v] == -1) {
adj[u].push_back(v);
p[v] = u;
Q[++last] = v;
}
}
}
int num = 0;
FOR(i, 1, n) num += (p[i] != -1);
if (num == n) {
DFS(u);
int ans = 0;
FOR(i, 1, n) ans += f[i];
return ans;
} else return INF;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // LOCAL
scanf("%d", &n);
FOR(i, 1, n) {
int m; scanf("%d", &m);
FOR(j, 1, m) {
int x; scanf("%d", &x);
boss[x].push_back(i);
}
}
int ans = INF;
FOR(i, 1, n) ans = min(ans, Compute(i));
printf("%d\n", ans);
return 0;
}
Compilation message
bosses.cpp: In function 'void DFS(int)':
bosses.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define REP(x, a, b) for (int x = a; x < b; ++x)
^
bosses.cpp:25:5: note: in expansion of macro 'REP'
REP(k, 0, adj[u].size()) {
^
bosses.cpp: In function 'int Compute(int)':
bosses.cpp:5:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define REP(x, a, b) for (int x = a; x < b; ++x)
^
bosses.cpp:42:9: note: in expansion of macro 'REP'
REP(k, 0, boss[u].size()) {
^
bosses.cpp: In function 'int main()':
bosses.cpp:66:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
^
bosses.cpp:68:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int m; scanf("%d", &m);
^
bosses.cpp:70:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x; scanf("%d", &x);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2388 KB |
Output is correct |
2 |
Correct |
0 ms |
2388 KB |
Output is correct |
3 |
Correct |
0 ms |
2388 KB |
Output is correct |
4 |
Correct |
0 ms |
2388 KB |
Output is correct |
5 |
Correct |
0 ms |
2388 KB |
Output is correct |
6 |
Correct |
0 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2388 KB |
Output is correct |
2 |
Correct |
0 ms |
2388 KB |
Output is correct |
3 |
Correct |
0 ms |
2388 KB |
Output is correct |
4 |
Correct |
0 ms |
2388 KB |
Output is correct |
5 |
Correct |
0 ms |
2388 KB |
Output is correct |
6 |
Correct |
0 ms |
2388 KB |
Output is correct |
7 |
Correct |
0 ms |
2388 KB |
Output is correct |
8 |
Correct |
0 ms |
2388 KB |
Output is correct |
9 |
Correct |
0 ms |
2388 KB |
Output is correct |
10 |
Correct |
0 ms |
2388 KB |
Output is correct |
11 |
Correct |
0 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2388 KB |
Output is correct |
2 |
Correct |
0 ms |
2388 KB |
Output is correct |
3 |
Correct |
0 ms |
2388 KB |
Output is correct |
4 |
Correct |
0 ms |
2388 KB |
Output is correct |
5 |
Correct |
0 ms |
2388 KB |
Output is correct |
6 |
Correct |
0 ms |
2388 KB |
Output is correct |
7 |
Correct |
0 ms |
2388 KB |
Output is correct |
8 |
Correct |
0 ms |
2388 KB |
Output is correct |
9 |
Correct |
0 ms |
2388 KB |
Output is correct |
10 |
Correct |
0 ms |
2388 KB |
Output is correct |
11 |
Correct |
0 ms |
2388 KB |
Output is correct |
12 |
Correct |
6 ms |
2520 KB |
Output is correct |
13 |
Correct |
3 ms |
2520 KB |
Output is correct |
14 |
Correct |
236 ms |
2520 KB |
Output is correct |
15 |
Correct |
63 ms |
2520 KB |
Output is correct |
16 |
Correct |
889 ms |
2652 KB |
Output is correct |
17 |
Execution timed out |
1500 ms |
2784 KB |
Execution timed out |
18 |
Halted |
0 ms |
0 KB |
- |