#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int adjMat[500][500];
int dp[500][500][2],dp2[500][500][2];
int m[500];
int main() {
int i;
int N,K,x;
scanf("%d %d",&N,&K);
for (i = 0; i < N; i++) {
while (1) {
scanf("%d",&x),x--;
if (x == -1) break;
else adjMat[i][x] = 1;
}
}
int j,k,l;
for (i = 2; i <= N; i++) {
for (j = 0; j < N; j++) {
k = (j+i-1) % N;
for (l = (j+1) % N; l != k; l++,l %= N) {
if (adjMat[j][l]) dp[j][k][0] = max(dp[j][k][0],max(dp[j][l][1],dp[l][k][0])+1);
if (adjMat[k][l]) dp[j][k][1] = max(dp[j][k][1],max(dp[j][l][1],dp[l][k][0])+1);
}
}
}
for (i = 2; i <= N; i++) {
for (j = 0; j < N; j++) {
k = (j+i-1) % N;
dp2[j][k][0] = dp2[j][k][1] = -9999;
if (adjMat[j][k]) dp2[j][k][0] = 1;
if (adjMat[k][j]) dp2[j][k][1] = 1;
for (l = (j+1) % N; l != k; l++,l %= N) {
if (adjMat[j][l]) dp2[j][k][0] = max(dp2[j][k][0],dp2[l][k][0]+1);
if (adjMat[k][l]) dp2[j][k][1] = max(dp2[j][k][1],dp2[j][l][1]+1);
}
}
}
if (K == 0) {
int M = -1,p = -1;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if ((i != j) && adjMat[i][j]) {
if ((max(dp[i][j][1],dp[j][i][0])+1) > M) M = max(dp[i][j][1],dp[j][i][0])+1,p = i;
}
}
}
printf("%d\n%d\n",M,p+1);
}
else {
int M = -1,p = -1;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if ((i != j) && adjMat[i][j]) {
if ((max(dp[i][j][1],dp[j][i][0])+1) > M) M = max(dp[i][j][1],dp[j][i][0])+1,p = i;
}
}
}
for (i = 0; i < N; i++) {
fill(m,m+N,-9999);
for (j = (i+N-2) % N; j != i; j = (j+N-1) % N) {
for (k = 0; k < N; k++) {
if (adjMat[(j+1) % N][k]) m[k] = max(m[k],dp2[(j+1) % N][i][1]+1);
}
if (adjMat[j][i]) {
for (k = (i+1) % N; k != j; k++,k %= N) {
if ((m[k]+max(dp[k][j][0],dp[i][k][1])+1) > M) M = m[k]+max(dp[k][j][0],dp[i][k][1])+1,p = j;
}
}
}
fill(m,m+N,-9999);
for (j = (i+2) % N; j != i; j++,j %= N) {
for (k = 0; k < N; k++) {
if (adjMat[(j+N-1) % N][k]) m[k] = max(m[k],dp2[i][(j+N-1) % N][0]+1);
}
if (adjMat[j][i]) {
for (k = (i+N-1) % N; k != j; k = (k+N-1) % N) {
if ((m[k]+max(dp[k][i][0],dp[j][k][1])+1) > M) M = m[k]+max(dp[k][i][0],dp[j][k][1])+1,p = j;
}
}
}
}
printf("%d\n%d\n",M,p+1);
}
return 0;
}
Compilation message
race.cpp: In function 'int main()':
race.cpp:64:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&K);
~~~~~^~~~~~~~~~~~~~~
race.cpp:67:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&x),x--;
~~~~~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
636 KB |
Output is correct |
3 |
Correct |
3 ms |
712 KB |
Output is correct |
4 |
Correct |
4 ms |
1012 KB |
Output is correct |
5 |
Correct |
5 ms |
1028 KB |
Output is correct |
6 |
Correct |
10 ms |
1076 KB |
Output is correct |
7 |
Correct |
10 ms |
1220 KB |
Output is correct |
8 |
Correct |
15 ms |
1348 KB |
Output is correct |
9 |
Correct |
19 ms |
1476 KB |
Output is correct |
10 |
Correct |
17 ms |
1488 KB |
Output is correct |
11 |
Correct |
28 ms |
1504 KB |
Output is correct |
12 |
Correct |
198 ms |
2560 KB |
Output is correct |
13 |
Correct |
496 ms |
3556 KB |
Output is correct |
14 |
Correct |
849 ms |
4448 KB |
Output is correct |
15 |
Correct |
2599 ms |
5488 KB |
Output is correct |
16 |
Correct |
2841 ms |
5488 KB |
Output is correct |
17 |
Correct |
2600 ms |
5576 KB |
Output is correct |
18 |
Correct |
1563 ms |
5612 KB |
Output is correct |
19 |
Execution timed out |
3040 ms |
5612 KB |
Time limit exceeded |
20 |
Execution timed out |
3041 ms |
5612 KB |
Time limit exceeded |