#include <bits/stdc++.h>
using namespace std;
int n;
vector< vector<short> > g, tg;
vector< bitset<5001> > mat;
vector<int> was, topoSort, scc;
int cur = 1;
vector<int> as;
vector<int> st;
void reverse_graph() {
for (int u = 1; u < 2 * n + 1; u ++) {
for (auto& v: g[u]) {
tg[v].push_back(u);
}
vector<short>().swap(g[u]);
}
}
void add_edge(int u, int v) {
g[u].push_back(v);
}
void dfs1(int u) {
was[u] = 1;
for (auto& v : g[u]) {
if (!was[v]) {
dfs1(v);
}
}
topoSort.push_back(u);
}
void dfs2(int u) {
scc[u] = cur;
was[u] = 1;
for (auto& v : tg[u]) {
if (!was[v]) {
dfs2(v);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
mat.assign(n + 1, bitset<5001>());
g.resize(2 * n + 1);
tg.resize(2 * n + 1);
for (int i = 1; i <= n; i++) {
int k;
cin >> k;
while (k--) {
int a;
cin >> a;
mat[a][i] = mat[i][a] = 1;
}
}
for (int i = 1; i <= n - 1; i++) {
for (int j = i + 1; j <= n; j++) {
if (mat[i][j]) {
add_edge(i, j + n);
add_edge(j, i + n);
} else {
add_edge(i + n, j);
add_edge(j + n, i);
}
}
}
was.assign(2 * n + 1, 0);
for (int i = 1; i <= 2 * n; i++) {
if (!was[i]) {
dfs1(i);
}
}
reverse(topoSort.begin(), topoSort.end());
reverse_graph();
scc.assign(2 * n + 1, -1);
was.assign(2 * n + 1, 0);
for (auto& u : topoSort) {
if (!was[u]) {
dfs2(u);
cur++;
}
}
as.resize(n + 1);
for (int i = 1; i <= n; i++) {
if (scc[i] == scc[i + n]) {
cout << 0 << '\n';
return 0;
}
as[i] = scc[i] > scc[i + n];
}
int ans = 1;
int sup = 0, con = 0;
for (int i = 1; i <= n; i++) {
sup += 1 - as[i];
con += as[i];
}
if ((sup == 0) || (con == 0)) {
ans = 0;
}
for (int i = 1; i <= n; i++) {
if (as[i]) {
int pos = 1;
if (con == 1) {
pos = 0;
}
for (int j = 1; j <= n; j++) {
if (!as[j] && !mat[i][j]) {
pos = 0;
break;
}
}
ans += pos;
} else {
int pos = 1;
if (sup == 1) {
pos = 0;
}
for (int j = 1; j <= n; j++) {
if (as[j] && mat[i][j]) {
pos = 0;
break;
}
}
ans += pos;
}
}
st.resize(n + 1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == i) {
continue;
}
}
for (int j = 1; j <= n; j++) {
if ((as[i] && !as[j] && !mat[i][j]) || (!as[i] && as[j] && mat[i][j])) {
if (st[i] > 0) {
st[i] = -1;
break;
} else {
st[i] = j;
}
}
}
}
for (int i = 1; i <= n - 1; i++) {
for (int j = i + 1; j <= n; j++) {
if (as[i] != as[j]) {
int x = i, y = j;
if (as[x]) {
swap(x, y);
}
if ((st[x] == 0 || st[x] == y) && (st[y] == 0 || st[y] == x)) {
ans++;
}
}
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
760 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
8312 KB |
Output is correct |
2 |
Correct |
109 ms |
9712 KB |
Output is correct |
3 |
Correct |
313 ms |
12536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
10360 KB |
Output is correct |
2 |
Correct |
178 ms |
13692 KB |
Output is correct |
3 |
Correct |
477 ms |
19216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
26616 KB |
Output is correct |
2 |
Correct |
433 ms |
29452 KB |
Output is correct |
3 |
Correct |
789 ms |
30840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
750 ms |
55036 KB |
Output is correct |
2 |
Correct |
1066 ms |
57520 KB |
Output is correct |
3 |
Correct |
2057 ms |
52936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2150 ms |
94968 KB |
Output is correct |
2 |
Correct |
1843 ms |
97016 KB |
Output is correct |
3 |
Execution timed out |
3012 ms |
94716 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |