#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];
std::vector<int> longest_trip(int N, int D)
{
n=N;
d.init(n);
vector<int>ans;
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});
}
}
for(int i=0;i<n;i++){
vii v;
for(int j=0;j<n;j++){
if(j==i)
continue;
else if(!edge[i][j]){
v.pb(j);
}
}
for(int j=0;j<v.size()-1;j++)
d.unite(v[j],v[j+1]);
for(auto it:v)
used[it]=1;
}
bool chk=1;
for(int i=0;i<n;i++){
if(used[i])
chk=0;
}
if(chk){
for(int i=0;i<n;i++){
ans.pb(i);
}
return ans;
}
ll last=-1;
vii v1,v2;
for(int i=0;i<n;i++){
if(used[i]){
if(last==-1){
last=i;
v1.pb(i);
}
else if(d.same(i,last))
v1.pb(i);
else
v2.pb(i);
}
else
v2.pb(i);
}
bool c=0;
for(auto it:v1){
for(auto itt:v2){
if(edge[it][itt]){
vis[it]=vis[itt]=1;
c=1;
break;
}
}
if(c)
break;
}
if(!c){
if(v1.size()<v2.size())
swap(v1,v2);
for(auto it:v1)
ans.pb(it);
return ans;
}
ll x=0;
for(auto it:v1){
if(!vis[it])
ans.pb(it);
else
x=it;
}
ans.pb(x);
for(auto it:v2){
if(vis[it])
ans.pb(it);
}
for(auto it:v2){
if(!vis[it])
ans.pb(it);
}
return ans;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:59:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int j=0;j<v.size()-1;j++)
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
183 ms |
692 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
0 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
0 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |