#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
int dp1[500][500][2], dp2[500][500][2], dp3[500][500][2];
bool adj[500][500];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, t;
cin >> n >> t;
FOR(i, 0, n) FOR(j, 0, n) FOR(k, 0, 2) dp2[i][j][k] = -n;
FOR(i, 0, n) {
int v;
cin >> v;
while (v) {
adj[i][v - 1] = true;
dp2[i][v - 1][0] = dp2[v - 1][i][1] = 1;
cin >> v;
}
}
FOR(k, 1, n + 1) FOR(i, 0, n) {
int j = (i + k) % n;
FOR(v, 0, n) {
if ((v > i && v < i + k) || (v > i - n && v < i + k - n)) {
if (adj[i][v]) dp1[i][j][0] = max(dp1[i][j][0], max(dp1[v][j][0], dp1[i][v][1]) + 1);
if (adj[j][v]) dp1[i][j][1] = max(dp1[i][j][1], max(dp1[v][j][0], dp1[i][v][1]) + 1);
dp2[i][j][0] = max(dp2[i][j][0], dp2[i][v][0] + dp2[v][j][0]);
dp2[i][j][1] = max(dp2[i][j][1], dp2[i][v][1] + dp2[v][j][1]);
}
}
dp3[i][j][0] = max(dp3[(i + 1) % n][j][0], dp1[i][j][0]);
dp3[i][j][1] = max(dp3[i][(j - 1 + n) % n][1], dp1[i][j][1]);
}
pair<int, int> ans = {0, -1};
FOR(i, 0, n) ans = max(ans, {max(dp1[i][i][0], dp1[i][i][1]), i});
if (t) {
FOR(k, 1, n + 1) FOR(i, 0, n) {
int j = (i + k) % n;
FOR(v, i + k + 1, i + n) if (adj[v % n][i]) {
FOR(x, v + 1, i + n) if (adj[j][x % n])
ans = max(ans, {dp2[i][j][0] + max(dp3[v % n][x % n][1], dp3[x % n][i][0]) + 2, v % n});
break;
}
for (int v = i + n - 1; v > i + k; v--) if (adj[v % n][j]) {
for (int x = v - 1; x > i + k; x--) if (adj[i][x % n])
ans = max(ans, {dp2[i][j][1] + max(dp3[x % n][v % n][0], dp3[j][x % n][1]) + 2, v % n});
break;
}
}
}
cout << ans.first << '\n' << ans.second + 1 << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
4 |
Correct |
6 ms |
896 KB |
Output is correct |
5 |
Correct |
6 ms |
1024 KB |
Output is correct |
6 |
Correct |
8 ms |
1024 KB |
Output is correct |
7 |
Correct |
8 ms |
1152 KB |
Output is correct |
8 |
Correct |
11 ms |
1280 KB |
Output is correct |
9 |
Correct |
11 ms |
1408 KB |
Output is correct |
10 |
Correct |
10 ms |
1536 KB |
Output is correct |
11 |
Correct |
13 ms |
1408 KB |
Output is correct |
12 |
Correct |
102 ms |
2816 KB |
Output is correct |
13 |
Correct |
296 ms |
4096 KB |
Output is correct |
14 |
Correct |
239 ms |
5248 KB |
Output is correct |
15 |
Correct |
1381 ms |
6520 KB |
Output is correct |
16 |
Correct |
1446 ms |
6520 KB |
Output is correct |
17 |
Correct |
1352 ms |
6520 KB |
Output is correct |
18 |
Correct |
446 ms |
6400 KB |
Output is correct |
19 |
Correct |
1539 ms |
6520 KB |
Output is correct |
20 |
Correct |
1557 ms |
6520 KB |
Output is correct |