#include "longesttrip.h"
#include <algorithm>
#include<bits/stdc++.h>
#include <cassert>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define ln endl
vector<vector<ll>> A;
ll n, d;
void dfs(ll u, vector<bool> &vis, vector<int> &trip){
trip.push_back(u);
vis[u]=1;
for (auto v:A[u]){
if (!vis[v]){
dfs(v, vis, trip);
}
}
}
std::vector<int> longest_trip(int N, int D)
{
n=N; d=D;
A.clear();
A.resize(N);
for (ll i=0; i<n; i++){
for (ll j=i+1; j<n; j++){
if (are_connected({(int)i}, {(int)j})){
A[i].push_back(j);
A[j].push_back(i);
}
}
}
vector<ll> semi;
for (ll i=0; i<n; i++){
if (A[i].size()<n-1){
semi.push_back(i);
}
}
vector<pair<ll, ll>> vert;
for (ll i=0; i<n; i++){
vert.push_back({A[i].size(), i});
sort(A[i].begin(), A[i].end(), [](ll op1, ll op2)->bool{return A[op1].size()<A[op2].size();});
}
sort(vert.begin(), vert.end());
// assert(semi.size()>=0 and semi.size()<=2);
vector<int> ans;
vector<bool> vis(n);
for (auto [_, i]:vert){
if (vis[i]==0){
vector<int> trip;
dfs(i, vis, trip);
if (ans.size()<trip.size()){
ans=trip;
}
}
}
return ans;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:36:24: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
36 | if (A[i].size()<n-1){
| ~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
166 ms |
2320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
24 ms |
344 KB |
Output is correct |
3 |
Correct |
114 ms |
1112 KB |
Output is correct |
4 |
Correct |
355 ms |
1112 KB |
Output is correct |
5 |
Correct |
809 ms |
1860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
344 KB |
Output is correct |
2 |
Correct |
23 ms |
344 KB |
Output is correct |
3 |
Correct |
116 ms |
852 KB |
Output is correct |
4 |
Correct |
353 ms |
1260 KB |
Output is correct |
5 |
Correct |
738 ms |
2216 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Incorrect |
5 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
21 ms |
344 KB |
Output is correct |
3 |
Correct |
127 ms |
852 KB |
Output is correct |
4 |
Correct |
361 ms |
1012 KB |
Output is correct |
5 |
Correct |
723 ms |
2184 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Incorrect |
5 ms |
364 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
20 ms |
344 KB |
Output is correct |
3 |
Partially correct |
140 ms |
1204 KB |
Output is partially correct |
4 |
Partially correct |
373 ms |
1124 KB |
Output is partially correct |
5 |
Partially correct |
808 ms |
1900 KB |
Output is partially correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Incorrect |
5 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |