#include <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <assert.h>
#include <limits>
#include <cstdio>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
#define mp make_pair
#define f first
#define s second
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e5+5;
const ll INF = 1e18;
const ld PI = 4*atan((ld)1);
const int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
namespace io {
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
// cin.exceptions(cin.failbit); // ex. throws exception when you try to read letter into int
if (sz(s)) {
setIn(s+".in");
setOut(s+".out");
} // for USACO
}
}
using namespace io;
namespace input {
template<class T> void re(complex<T>& x);
template<class T1, class T2> void re(pair<T1,T2>& p);
template<class T> void re(vector<T>& a);
template<class T, size_t SZ> void re(array<T,SZ>& a);
template<class T> void re(T& x) { cin >> x; }
void re(double& x) { string t; re(t); x = stod(t); }
void re(ld& x) { string t; re(t); x = stold(t); }
template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
re(first); re(rest...);
}
template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}
namespace output {
template<class T1, class T2> void pr(const pair<T1,T2>& x);
template<class T, size_t SZ> void pr(const array<T,SZ>& x);
template<class T> void pr(const vector<T>& x);
template<class T> void pr(const set<T>& x);
template<class T1, class T2> void pr(const map<T1,T2>& x);
template<class T> void pr(const T& x) { cout << x; }
template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
pr(first); pr(rest...);
}
template<class T1, class T2> void pr(const pair<T1,T2>& x) {
pr("{",x.f,", ",x.s,"}");
}
template<class T> void prContain(const T& x) {
pr("{");
bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool>
pr("}");
}
template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
template<class T> void pr(const vector<T>& x) { prContain(x); }
template<class T> void pr(const set<T>& x) { prContain(x); }
template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
void ps() { pr("\n"); }
template<class Arg> void ps(const Arg& first) {
pr(first); ps(); // no space at end of line
}
template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
pr(first," "); ps(rest...); // print w/ spaces
}
}
using namespace output;
using namespace input;
ll add(ll a, ll b) {
a += b;
if(a >= MOD) {
a -= MOD;
}
return a;
}
ll sub(ll a, ll b) {
a -= b;
if(a < 0) {
a += MOD;
}
return a;
}
ll mul(ll a, ll b) {
return (a * b)%MOD;
}
void add_self(ll& a, ll b) {
a = add(a, b);
}
void sub_self(ll& a, ll b) {
a = sub(a, b);
}
void mul_self(ll& a, ll b) {
a = mul(a, b);
}
#define pb push_back
void ckmx(int&x,int y){x=max(x,y);}
const int N=505;
vector<int> E[N];
int dp[N][N][2],n,k,tmp[N];
bool was[N][N][2];
int on(int l,int r,int v){
return (l<=r&&l<=v&&r>=v)||(l>r&&(v<=r||v>=l));
}
int DP(int l,int r,int side){
if(was[l][r][side])return dp[l][r][side];
int u=side==0?l:r;
was[l][r][side]=1;
int L=side==0?(l==n?1:l+1):l;
int R=side==1?(r==1?n:r-1):r;
for(int v:E[u]){
if(on(l,r,v)){
ckmx(dp[l][r][side],1+max(DP(L,v,1),DP(v,R,0)));
}
}
return dp[l][r][side];
}
int go[N][N][2],has[N][N];
int main(){
scanf("%i %i",&n,&k);
for(int i=1;i<=n;i++){
while(1){
int j;
scanf("%i",&j);
if(j==0)break;
E[i].pb(j);
has[i][j]=1;
}
}
auto nxt=[&](int x){return x==n?1:x+1;};
auto pre=[&](int x){return x==1?n:x-1;};
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)tmp[j]=-N;
tmp[i]=0;
for(int j=i;;j=nxt(j)){
go[i][j][0]=tmp[j];
for(int r:E[j]){
ckmx(tmp[r],tmp[j]+1);
}
if(j==pre(i))break;
}
for(int j=1;j<=n;j++)tmp[j]=-N;
tmp[i]=0;
for(int j=i;;j=pre(j)){
go[i][j][1]=tmp[j];
for(int r:E[j]){
ckmx(tmp[r],tmp[j]+1);
}
if(j==nxt(i))break;
}
}
int ans=-1,idx=0;
for(int i=1;i<=n;i++){
int now=DP(i,i==1?n:i-1,0);
if(now>ans)ans=now,idx=i;
}
if(k==1){
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i!=j){
if(go[i][j][0]!=0){
int r=-1;
for(int l=nxt(j);l!=i;l=nxt(l))if(has[l][i]){r=l;break;}
if(r!=-1){
for(int z=nxt(r);z!=i;z=nxt(z))if(has[j][z]){
int now=1+go[i][j][0]+1+max(DP(nxt(r),z,1),DP(z,pre(i),0));
if(ans<now)ans=now,idx=r;
}
}
}
if(go[i][j][1]!=0){
int r=-1;
for(int l=pre(j);l!=i;l=pre(l))if(has[l][i]){r=l;break;}
if(r!=-1){
for(int z=pre(r);z!=i;z=pre(z))if(has[j][z]){
int now=1+go[i][j][1]+1+max(DP(z,pre(r),0),DP(nxt(i),z,1));
if(ans<now)ans=now,idx=r;
}
}
}
}
}
printf("%i\n%i\n",ans,idx);
return 0;
}
Compilation message
race.cpp: In function 'void io::setIn(std::string)':
race.cpp:67:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
67 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'void io::setOut(std::string)':
race.cpp:68:36: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
68 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'int main()':
race.cpp:191:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
191 | scanf("%i %i",&n,&k);
| ~~~~~^~~~~~~~~~~~~~~
race.cpp:195:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
195 | scanf("%i",&j);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
2 ms |
768 KB |
Output is correct |
5 |
Correct |
2 ms |
896 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
6 ms |
1152 KB |
Output is correct |
8 |
Correct |
5 ms |
1280 KB |
Output is correct |
9 |
Correct |
9 ms |
1408 KB |
Output is correct |
10 |
Correct |
22 ms |
1536 KB |
Output is correct |
11 |
Correct |
11 ms |
1536 KB |
Output is correct |
12 |
Correct |
62 ms |
2560 KB |
Output is correct |
13 |
Correct |
133 ms |
3704 KB |
Output is correct |
14 |
Correct |
91 ms |
4728 KB |
Output is correct |
15 |
Correct |
721 ms |
6136 KB |
Output is correct |
16 |
Correct |
891 ms |
6264 KB |
Output is correct |
17 |
Correct |
734 ms |
6136 KB |
Output is correct |
18 |
Correct |
118 ms |
5880 KB |
Output is correct |
19 |
Correct |
1022 ms |
6392 KB |
Output is correct |
20 |
Correct |
1049 ms |
6520 KB |
Output is correct |