#include <bits/stdc++.h>
#include <random>
#include <chrono>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> void _do(T x){cerr<<x<<"\n";}
template<typename T,typename ...U> void _do(T x,U ...y){cerr<<x<<", ";_do(y...);}
#define dbg(...) cerr<<#__VA_ARGS__<<" = ";_do(__VA_ARGS__);
const int MOD1=1e9+7;
const int MOD2=998244353;
const ll INF=3e18;
ll fpow(ll a,ll b,ll m)
{
if(!b) return 1;
ll tmp=1;
for(ll cur=a;b;b>>=1,cur=cur*cur%m) if(b&1) tmp=tmp*cur%m;
return tmp;
}
ll inv(ll a,ll m) {return fpow(a,m-2,m);}
#define MottoHayaku ios::sync_with_stdio(false);cin.tie(0);
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define F first
#define S second
#define pb push_back
#define uni(c) c.resize(distance(c.begin(),unique(c.begin(),c.end())))
#define unisort(c) sort(c.begin(),c.end()),uni(c)
#include "longesttrip.h"
int has[300][300];
vector<int> ed[300];
bool vis[300];
int par[300];
int deg[300],cnt[300];
int n;
vector<int> longest_trip(int N,int D)
{
n=N;
rep(i,n)
{
rep(j,n)
{
has[i][j]=-1;
}
ed[i].clear();
vis[i]=0;
par[i]=-1;
deg[i]=cnt[i]=0;
}
vector<int> ret;
if(D==3)
{
rep(i,n) ret.pb(i);
return ret;
}
vector<int> v1={0},v2;
bool has=1;
rep1(i,n-1)
{
if(are_connected({v1.back()},{i}))
{
v1.pb(i);
has=1;
}
else if(has==0)
{
v2.pb(i);
}
else
{
if(v2.empty()||are_connected({v2.back()},{i}))
{
v2.pb(i);
has=0;
}
else
{
reverse(v2.begin(),v2.end());
for(int x:v2) v1.pb(x);
v2={i};
has=1;
}
}
}
if(v1.size()<v2.size()) swap(v1,v2);
if(v2.empty()) return v1;
if(!are_connected(v1,v2)) return v1;
if(are_connected({v1[0]},{v2[0]}))
{
reverse(v2.begin(),v2.end());
for(int x:v1) v2.pb(x);
return v2;
}
if(are_connected({v1.back()},{v2[0]}))
{
for(int x:v2) v1.pb(x);
return v1;
}
int l=0,r=(int)v1.size()-1;
while(l<r)
{
int mid=l+r>>1;
vector<int> tmp;
rep(i,mid+1) tmp.pb(v1[i]);
if(are_connected(tmp,{v2[0]})) r=mid;
else l=mid+1;
}
reverse(v2.begin(),v2.end());
for(int i=l;i<v1.size();i++) v2.pb(v1[i]);
rep(i,l) v2.pb(v1[i]);
return v2;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:117:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
117 | int mid=l+r>>1;
| ~^~
longesttrip.cpp:124:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for(int i=l;i<v1.size();i++) v2.pb(v1[i]);
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
276 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
208 KB |
Output is correct |
2 |
Correct |
7 ms |
208 KB |
Output is correct |
3 |
Correct |
9 ms |
336 KB |
Output is correct |
4 |
Correct |
8 ms |
336 KB |
Output is correct |
5 |
Correct |
7 ms |
592 KB |
Output is correct |
6 |
Correct |
9 ms |
208 KB |
Output is correct |
7 |
Correct |
9 ms |
208 KB |
Output is correct |
8 |
Correct |
11 ms |
336 KB |
Output is correct |
9 |
Correct |
9 ms |
440 KB |
Output is correct |
10 |
Correct |
9 ms |
592 KB |
Output is correct |
11 |
Correct |
7 ms |
592 KB |
Output is correct |
12 |
Correct |
10 ms |
592 KB |
Output is correct |
13 |
Correct |
8 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
208 KB |
Output is correct |
2 |
Correct |
9 ms |
208 KB |
Output is correct |
3 |
Correct |
8 ms |
336 KB |
Output is correct |
4 |
Correct |
10 ms |
464 KB |
Output is correct |
5 |
Correct |
6 ms |
592 KB |
Output is correct |
6 |
Correct |
7 ms |
208 KB |
Output is correct |
7 |
Correct |
7 ms |
208 KB |
Output is correct |
8 |
Correct |
6 ms |
336 KB |
Output is correct |
9 |
Correct |
8 ms |
336 KB |
Output is correct |
10 |
Correct |
7 ms |
592 KB |
Output is correct |
11 |
Correct |
8 ms |
564 KB |
Output is correct |
12 |
Correct |
7 ms |
592 KB |
Output is correct |
13 |
Correct |
10 ms |
620 KB |
Output is correct |
14 |
Correct |
10 ms |
208 KB |
Output is correct |
15 |
Correct |
12 ms |
208 KB |
Output is correct |
16 |
Correct |
12 ms |
336 KB |
Output is correct |
17 |
Correct |
5 ms |
336 KB |
Output is correct |
18 |
Correct |
10 ms |
336 KB |
Output is correct |
19 |
Correct |
10 ms |
420 KB |
Output is correct |
20 |
Correct |
13 ms |
336 KB |
Output is correct |
21 |
Correct |
11 ms |
592 KB |
Output is correct |
22 |
Correct |
11 ms |
628 KB |
Output is correct |
23 |
Correct |
8 ms |
624 KB |
Output is correct |
24 |
Correct |
11 ms |
592 KB |
Output is correct |
25 |
Correct |
13 ms |
208 KB |
Output is correct |
26 |
Correct |
10 ms |
208 KB |
Output is correct |
27 |
Correct |
11 ms |
208 KB |
Output is correct |
28 |
Correct |
10 ms |
208 KB |
Output is correct |
29 |
Correct |
9 ms |
208 KB |
Output is correct |
30 |
Correct |
7 ms |
336 KB |
Output is correct |
31 |
Correct |
7 ms |
336 KB |
Output is correct |
32 |
Correct |
12 ms |
336 KB |
Output is correct |
33 |
Correct |
4 ms |
336 KB |
Output is correct |
34 |
Correct |
6 ms |
336 KB |
Output is correct |
35 |
Correct |
10 ms |
336 KB |
Output is correct |
36 |
Correct |
9 ms |
592 KB |
Output is correct |
37 |
Correct |
9 ms |
624 KB |
Output is correct |
38 |
Correct |
10 ms |
592 KB |
Output is correct |
39 |
Correct |
10 ms |
592 KB |
Output is correct |
40 |
Correct |
6 ms |
572 KB |
Output is correct |
41 |
Correct |
11 ms |
704 KB |
Output is correct |
42 |
Correct |
9 ms |
592 KB |
Output is correct |
43 |
Correct |
11 ms |
592 KB |
Output is correct |
44 |
Correct |
7 ms |
592 KB |
Output is correct |
45 |
Correct |
13 ms |
208 KB |
Output is correct |
46 |
Incorrect |
0 ms |
208 KB |
Incorrect |
47 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
7 ms |
208 KB |
Output is correct |
3 |
Correct |
8 ms |
336 KB |
Output is correct |
4 |
Correct |
11 ms |
464 KB |
Output is correct |
5 |
Correct |
11 ms |
592 KB |
Output is correct |
6 |
Correct |
14 ms |
208 KB |
Output is correct |
7 |
Correct |
8 ms |
208 KB |
Output is correct |
8 |
Correct |
8 ms |
336 KB |
Output is correct |
9 |
Correct |
7 ms |
336 KB |
Output is correct |
10 |
Correct |
9 ms |
592 KB |
Output is correct |
11 |
Correct |
9 ms |
592 KB |
Output is correct |
12 |
Correct |
10 ms |
592 KB |
Output is correct |
13 |
Correct |
8 ms |
592 KB |
Output is correct |
14 |
Correct |
12 ms |
208 KB |
Output is correct |
15 |
Correct |
8 ms |
208 KB |
Output is correct |
16 |
Correct |
9 ms |
336 KB |
Output is correct |
17 |
Correct |
12 ms |
336 KB |
Output is correct |
18 |
Correct |
9 ms |
336 KB |
Output is correct |
19 |
Correct |
9 ms |
336 KB |
Output is correct |
20 |
Correct |
9 ms |
440 KB |
Output is correct |
21 |
Correct |
11 ms |
208 KB |
Output is correct |
22 |
Correct |
11 ms |
208 KB |
Output is correct |
23 |
Correct |
10 ms |
208 KB |
Output is correct |
24 |
Correct |
6 ms |
208 KB |
Output is correct |
25 |
Correct |
8 ms |
208 KB |
Output is correct |
26 |
Correct |
8 ms |
336 KB |
Output is correct |
27 |
Correct |
12 ms |
336 KB |
Output is correct |
28 |
Correct |
12 ms |
336 KB |
Output is correct |
29 |
Correct |
9 ms |
336 KB |
Output is correct |
30 |
Correct |
10 ms |
336 KB |
Output is correct |
31 |
Correct |
9 ms |
336 KB |
Output is correct |
32 |
Correct |
10 ms |
208 KB |
Output is correct |
33 |
Incorrect |
1 ms |
208 KB |
Incorrect |
34 |
Halted |
0 ms |
0 KB |
- |