#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vii;
typedef pair<ll,ll> pii;
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int M=1010;
const ll inf=1e18;
struct dsu{
ll sz[M],pa[M];
void init(ll x){
for(int i=0;i<=x;i++)
pa[i]=i,sz[i]=1;
}
ll get(ll x){
if(x==pa[x])
return x;
return pa[x]=get(pa[x]);
}
void unite(ll x,ll y){
x=get(x),y=get(y);
if(x==y)
return;
if(sz[x]<sz[y])
swap(x,y);
pa[y]=x;
sz[x]+=sz[y];
}
bool same(ll x,ll y){
return (get(x)==get(y));
}
}d;
ll n;
bool edge[M][M],vis[M];
bool used[M];
vector<int>ans,adj[M];
void dfs(ll x){
if(vis[x])
return;
vis[x]=1;
for(auto it:adj[x]){
dfs(it);
}
}
bool cchk(ll i,ll j){
if(!edge[i][j])
return 0;
bool c1[260];
memset(c1,0,sizeof c1);
for(auto it:adj[i]){
if(it!=j)
c1[it]=1;
}
for(auto it:adj[j]){
if(it!=i&&!c1[it])
return 1;
}
return 0;
}
deque<int>dp[260][260];
vector<int> solve(ll x,ll y){
vii v;
v.pb(x),v.pb(y);
for(int i=0;i<n;i++){
if(i==x||i==y)
continue;
v.pb(i);
}
for(int i=2;i<v.size();i++){
bool c=0;
for(int j=i-2;j>=0;j--){
if(edge[v[i]][v[i-1]]&&dp[j][i-1].size()>0){
dp[j][i]=dp[j][i-1];
dp[j][i].pb(v[i]);
dp[i][j]=dp[i-1][j];
dp[i][j].push_front(v[i]);
}
if(edge[v[i]][v[j]]&&dp[j][i-1].size()>0&&!c){
dp[i-1][i]=dp[i-1][j];
dp[i-1][i].pb(v[i]);
dp[i][i-1]=dp[j][i-1];
dp[i][i-1].push_front(v[i]);
c=1;
}
}
}
vector<int>res;
for(int i=0;i<n-1;i++){
if(dp[i][n-1].size()>0){
for(auto it:dp[i][n-1])
res.pb(it);
return res;
}
}
return res;
}
std::vector<int> longest_trip(int N, int D)
{
memset(vis,0,sizeof vis);
n=N;
for(int i=0;i<n;i++)
adj[i].clear();
for(int i=0;i<n;i++){
used[i]=0;
for(int j=0;j<n;j++)
dp[i][j].clear();
}
d.init(n);
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
edge[i][j]=edge[j][i]=are_connected({i},{j});
if(edge[i][j])
adj[i].pb(j),adj[j].pb(i);
}
}
dfs(0);
bool chk=0;
for(int i=0;i<n;i++){
if(!vis[i])
chk=1;
}
if(chk){
vector<int>v1,v2;
v1.pb(0);
for(int i=1;i<n;i++){
if(edge[i][0])
v1.pb(i);
else
v2.pb(i);
}
if(v1.size()<v2.size())
swap(v1,v2);
return v1;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j)
continue;
if(cchk(i,j)){
dp[0][1].pb(i),dp[0][1].pb(j);
dp[1][0].pb(j),dp[1][0].pb(i);
return solve(i,j);
}
}
}
if(ans.empty()){
for(int i=0;i<n;i++)
ans.pb(i);
}
return ans;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> solve(ll, ll)':
longesttrip.cpp:73:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i=2;i<v.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
47188 KB |
Output is correct |
2 |
Correct |
207 ms |
49456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
46936 KB |
Output is correct |
2 |
Incorrect |
40 ms |
46848 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
46936 KB |
Output is correct |
2 |
Incorrect |
42 ms |
46936 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
46936 KB |
Output is correct |
2 |
Incorrect |
43 ms |
46936 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
46936 KB |
Output is correct |
2 |
Incorrect |
44 ms |
46936 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |