#include<bits/stdc++.h>
using namespace std;
const int maxn = 500 + 5;
const int inf = 1e6;
int n,k;
int way[maxn][maxn];
vector<int> yaw[maxn];
int dp[maxn][maxn][2];
int dp2[maxn][maxn][2];
int mx[maxn];
int add(int x, int y) {
return ((x+y)%n + n)%n;
}
int f(int u, int v, int t) {
if(dp[u][v][t]==-1) {
dp[u][v][t] = 0;
int wow = (t==0 ? 1:-1);
for(int x=add(u,wow);x!=v;x=add(x,wow)) {
if(!way[u][x]) continue;
//if(u==4 && v==1) printf("%d %d %d -> %d\n",u,v,t,x);
dp[u][v][t] = max(dp[u][v][t], max(f(x,u,!t), f(x,v,t)) + 1);
}
}
return dp[u][v][t];
}
int g(int u, int v, int t) {
if(dp2[u][v][t]==-1) {
dp2[u][v][t] = -inf;
int wow = (t==0 ? 1:-1);
if(way[u][v]) dp2[u][v][t] = 1;
for(int x=add(u,wow);x!=v;x=add(x,wow)) {
if(!way[u][x]) continue;
// if(u==4 && v==2) printf("%d %d %d -> %d\n",u,v,t,x);
dp2[u][v][t] = max(dp2[u][v][t], g(x,v,t) + 1);
}
}
return dp2[u][v][t];
}
int main() {
scanf("%d%d",&n,&k);
for(int u=0;u<n;u++) {
int v;
while(scanf("%d",&v) && v) {
--v;
way[u][v] = 1;
yaw[v].push_back(u);
}
}
memset(dp,-1,sizeof(dp));
memset(dp2,-1,sizeof(dp2));
pair<int,int> res = {-1,-1};
for(int u=0;u<n;u++) res = max(res, {f(u,u,0), u});
return 0;
if(k) {
for(int u=0;u<n;u++) {
//case 1
for(int v=0;v<n;v++) mx[v] = 0;
for(int v=add(u,1);v!=u;v=add(v,1)) {
if(way[u][v]) {
for(int x=add(v,1);x!=u;x=add(x,1)) {
// printf("case 1 : u = %d v = %d x = %d => 1 + %d + %d\n",u,v,x,mx[x],g(v,x,0));
res = max(res, {1 + mx[x] + g(v,x,0), u});
}
}
for(auto x : yaw[v]) mx[x] = max(mx[x], f(v,u,1) + 1);
}
//case 2
for(int v=0;v<n;v++) mx[v] = 0;
for(int v=add(u,-1);v!=u;v=add(v,-1)) {
if(way[u][v]) {
for(int x=add(v,-1);x!=u;x=add(x,-1)) {
// printf("case 2 : u = %d v = %d x = %d => 1 + %d + %d\n",u,v,x,mx[x],g(v,x,1));
res = max(res, {1 + mx[x] + g(v,x,1), u});
}
}
for(auto x : yaw[v]) mx[x] = max(mx[x], f(v,u,0) + 1);
}
}
for(int v=0;v<n;v++) {
//case 3
for(int u=0;u<n;u++) mx[u] = 0;
for(int u=add(v,-1);u!=v;u=add(u,-1)) {
if(way[u][v]) {
for(int x=add(v,1);x!=u;x=add(x,1)) {
// printf("case 2 : u = %d v = %d x = %d => 1 + %d + %d\n",u,v,x,mx[x],g(v,x,0));
res = max(res, {1 + mx[x] + g(v,x,0), u});
}
}
for(auto x : yaw[u]) mx[x] = max(mx[x], f(u,v,0) + 1);
}
//case 4
for(int u=0;u<n;u++) mx[u] = 0;
for(int u=add(v,1);u!=v;u=add(u,1)) {
if(way[u][v]) {
for(int x=add(v,-1);x!=u;x=add(x,-1)) {
// printf("case 2 : u = %d v = %d x = %d => 1 + %d + %d\n",u,v,x,mx[x],g(v,x,1));
res = max(res, {1 + mx[x] + g(v,x,1), u});
}
}
for(auto x : yaw[u]) mx[x] = max(mx[x], f(u,v,1) + 1);
}
}
}
printf("%d\n%d",res.first,res.second+1);
}
Compilation message
race.cpp: In function 'int main()':
race.cpp:42:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&k);
~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
4344 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
6 ms |
4528 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
5 ms |
4528 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
7 ms |
4528 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
10 ms |
4580 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
11 ms |
4708 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
16 ms |
4708 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
16 ms |
4708 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
27 ms |
4836 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
39 ms |
4836 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
35 ms |
4920 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
203 ms |
4948 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
557 ms |
5204 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
1295 ms |
5356 KB |
Unexpected end of file - int32 expected |
15 |
Execution timed out |
3055 ms |
5740 KB |
Time limit exceeded |
16 |
Execution timed out |
3043 ms |
5832 KB |
Time limit exceeded |
17 |
Execution timed out |
3050 ms |
5832 KB |
Time limit exceeded |
18 |
Incorrect |
2209 ms |
5832 KB |
Unexpected end of file - int32 expected |
19 |
Execution timed out |
3050 ms |
5884 KB |
Time limit exceeded |
20 |
Execution timed out |
3049 ms |
5884 KB |
Time limit exceeded |