#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; }
#define BitCount(x) __builtin_popcount(x)
using namespace std;
typedef long long LL;
typedef pair <int, int> II;
const int N = 5e2 + 10;
int n, request;
int dp[N][N][2], f[N][N][2];
vector <int> adj[N], rev[N];
int CCW(int i, int j, int k) {
if (i == k || j == k) return -1;
if (i < j) {
return (j < k && k < n) || (k < i);
} else return (j < k && k < i);
}
int DP(int l, int r, int k) {
if (l == r) return dp[l][r][k] = 0;
int &ans = dp[l][r][k];
if (ans != -1) return ans;
ans = 0;
REP(i, 0, adj[r].size()) {
int v = adj[r][i];
int d = CCW(l, r, v); if (d == -1) continue;
if (d == k) {
ans = max(ans, 1 + DP(l, v, k));
ans = max(ans, 1 + DP(r, v, k ^ 1));
}
}
return ans;
}
int Compute(int u, int v, int k, int dir) {
if (u == v) return f[u][v][dir] = 0;
int &ans = f[u][v][dir]; if (ans != -1) return ans;
ans = 0;
REP(i, 0, adj[v].size()) {
int nxt = adj[v][i];
int d1 = CCW(u, v, nxt), d2 = CCW(u, k, nxt);
if (d1 == dir) ans = max(ans, 1 + Compute(u, nxt, k, dir));
if (d1 == -1 || d2 == -1) continue;
if (d1 != dir && d2 != dir) {
int add = 0;
REP(p, 0, adj[nxt].size()) {
int x = adj[nxt][p]; if (CCW(u, k, x) == -1) continue;
if (CCW(u, k, x) != dir) add = max(add, 1 + DP(nxt, x, dir ^ 1));
}
ans = max(ans, 1 + add);
}
}
return ans;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // LOCAL
scanf("%d%d", &n, &request);
REP(u, 0, n) {
int v;
while (scanf("%d", &v) == 1) {
if (v == 0) break;
adj[u].push_back(v - 1);
rev[v - 1].push_back(u);
}
}
if (request == 0) {
memset(dp, -1, sizeof dp);
int ans = 0, num = -1;
REP(u, 0, n) {
REP(k, 0, adj[u].size()) {
int v = adj[u][k];
int can = DP(u, v, 0) + 1;
if (can > ans) {
ans = can;
num = u;
}
can = DP(u, v, 1) + 1;
if (can > ans) {
ans = can;
num = u;
}
}
}
printf("%d\n%d", ans, num + 1);
} else {
int ans = 0, num = -1;
// dir == 0
memset(f, -1, sizeof f);
REP(k, 0, n) {
memset(f, -1, sizeof f);
REP(i, 0, rev[k].size()) {
int u = rev[k][i];
int can = 1 + Compute(u, k, k, 0);
if (can > ans) {
ans = can;
num = u;
}
}
}
// dir == 1
REP(k, 0, n) {
memset(f, -1, sizeof f);
REP(i, 0, rev[k].size()) {
int u = rev[k][i];
int can = 1 + Compute(u, k, k, 1);
if (can > ans) {
ans = can;
num = u;
}
}
}
assert(num != -1);
printf("%d\n%d", ans, num + 1);
}
return 0;
}
Compilation message
race.cpp: In function 'int DP(int, int, int)':
race.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)
^
race.cpp:35:5: note: in expansion of macro 'REP'
REP(i, 0, adj[r].size()) {
^
race.cpp: In function 'int Compute(int, int, int, int)':
race.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)
^
race.cpp:50:5: note: in expansion of macro 'REP'
REP(i, 0, adj[v].size()) {
^
race.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)
^
race.cpp:57:13: note: in expansion of macro 'REP'
REP(p, 0, adj[nxt].size()) {
^
race.cpp: In function 'int main()':
race.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)
^
race.cpp:85:13: note: in expansion of macro 'REP'
REP(k, 0, adj[u].size()) {
^
race.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)
^
race.cpp:106:13: note: in expansion of macro 'REP'
REP(i, 0, rev[k].size()) {
^
race.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)
^
race.cpp:118:13: note: in expansion of macro 'REP'
REP(i, 0, rev[k].size()) {
^
race.cpp:72:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &request);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
6108 KB |
Output is correct |
2 |
Incorrect |
13 ms |
6108 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
6108 KB |
Output isn't correct |
4 |
Incorrect |
9 ms |
6108 KB |
Output isn't correct |
5 |
Correct |
3 ms |
6108 KB |
Output is correct |
6 |
Incorrect |
49 ms |
6108 KB |
Output isn't correct |
7 |
Correct |
6 ms |
6240 KB |
Output is correct |
8 |
Incorrect |
49 ms |
6108 KB |
Output isn't correct |
9 |
Correct |
9 ms |
6240 KB |
Output is correct |
10 |
Correct |
33 ms |
6240 KB |
Output is correct |
11 |
Correct |
13 ms |
6240 KB |
Output is correct |
12 |
Execution timed out |
3000 ms |
6240 KB |
Execution timed out |
13 |
Execution timed out |
3000 ms |
6240 KB |
Execution timed out |
14 |
Correct |
83 ms |
6240 KB |
Output is correct |
15 |
Execution timed out |
3000 ms |
6504 KB |
Execution timed out |
16 |
Execution timed out |
3000 ms |
6636 KB |
Execution timed out |
17 |
Execution timed out |
3000 ms |
6504 KB |
Execution timed out |
18 |
Correct |
106 ms |
6240 KB |
Output is correct |
19 |
Execution timed out |
3000 ms |
6636 KB |
Execution timed out |
20 |
Execution timed out |
3000 ms |
6636 KB |
Execution timed out |