#include "bits/stdc++.h"
//#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
using namespace std;
#include "longesttrip.h"
vector<int> v[260];
vector<int> vis(260,0),vis2(260,0),depth(260,-1),topo;
int ord[260],max_c,max_ans,max_a,max_b;
vector<int> aa,bb;
void topo_sort(int c){
if(vis[c]) return;
vis[c]=1;
for(int x:v[c]) topo_sort(x);
topo.push_back(c);
}
void dfs(int c,int p){
if(vis[c]) return;
vis[c]=1;
sort(all(v[c]),[&](int a,int b){
return ord[a]<ord[b];
});
vector<array<int,2>> takla;
for(int x:v[c]){
if(x==p) continue;
if(!vis[x]){
dfs(x,c);
depth[c]=max(depth[c],depth[x]);
takla.push_back({depth[x],x});
}
}
depth[c]++;
sort(all(takla));
reverse(all(takla));
if(sz(takla)<=1) return;
if(takla[0][0]+takla[1][0]+1>max_ans){
max_ans=takla[0][0]+takla[1][0]+1;
max_c=c;
max_a=takla[0][1];
max_b=takla[1][1];
}
}
void go_leaf(int c,int t){
if(vis[c]) return;
vis[c]=1;
int kid=-1,maxi=-1;
for(int x:v[c]){
if(depth[x]>=depth[c]) continue;
if(depth[x]>maxi){
maxi=depth[x];
kid=x;
}
}
if(kid!=-1) go_leaf(kid,t);
if(t) aa.push_back(c);
else bb.push_back(c);
}
void mark(int c){
if(vis2[c]) return;
vis2[c]=1;
for(int x:v[c]) mark(x);
}
vector<int> construct(int c){
vector<int> res;
topo.clear();aa.clear();bb.clear();
vis.assign(260,0);
depth.assign(260,-1);
topo_sort(c);
reverse(all(topo));
for(int i=0;i<sz(topo);i++) ord[topo[i]]=i;
vis.assign(260,0);
max_a=max_b=max_ans=max_c=-1;
dfs(c,-1);
vis.assign(260,0);
if(max_a!=-1) go_leaf(max_a,1);
vis.assign(260,0);
if(max_b!=-1) go_leaf(max_b,0);
if(max_a!=-1){
aa.push_back(max_c);
reverse(all(bb));
for(int uu:bb) aa.push_back(uu);
if(sz(aa)>sz(res)) res=aa;
}
aa.clear();
go_leaf(c,1);
if(sz(aa)>sz(res)) res=aa;
return res;
}
vector<int> longest_trip(int n, int d){
if(d==3){
vector<int> res(n,0);
iota(all(res),0);
return res;
}
for(int i=0;i<n;i++) v[i].clear();
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(are_connected({i},{j})){
v[i].push_back(j);
v[j].push_back(i);
}
}
}
vector<int> ans;
vis2.assign(260,0);
for(int i=0;i<n;i++){
if(vis2[i]) continue;
vector<int> x=construct(i);
if(sz(x)>sz(ans)) ans=x;
mark(i);
}
return ans;
}
/*void _(){
int n,m;
cin >> n >> m;
for(int i=0;i<n;i++) v[i].clear();
for(int i=0;i<m;i++){
int a,b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
vector<int> ans;
for(int i=0;i<n;i++){
vector<int> x=construct(i);
if(sz(x)>sz(ans)) ans=x;
}
for(int x:ans) cout << x << ' ';
cout << '\n';
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int tc=1;//cin >> tc;
while(tc--) _();
return 0;
}*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
183 ms |
976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
21 ms |
344 KB |
Output is correct |
3 |
Correct |
159 ms |
600 KB |
Output is correct |
4 |
Correct |
414 ms |
476 KB |
Output is correct |
5 |
Correct |
832 ms |
828 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Incorrect |
5 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
17 ms |
344 KB |
Output is correct |
3 |
Correct |
157 ms |
464 KB |
Output is correct |
4 |
Correct |
408 ms |
500 KB |
Output is correct |
5 |
Correct |
762 ms |
1028 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
19 ms |
344 KB |
Output is correct |
8 |
Correct |
163 ms |
600 KB |
Output is correct |
9 |
Correct |
315 ms |
848 KB |
Output is correct |
10 |
Correct |
798 ms |
1124 KB |
Output is correct |
11 |
Correct |
824 ms |
884 KB |
Output is correct |
12 |
Correct |
823 ms |
1236 KB |
Output is correct |
13 |
Correct |
801 ms |
848 KB |
Output is correct |
14 |
Correct |
5 ms |
344 KB |
Output is correct |
15 |
Correct |
11 ms |
344 KB |
Output is correct |
16 |
Correct |
34 ms |
344 KB |
Output is correct |
17 |
Correct |
91 ms |
600 KB |
Output is correct |
18 |
Correct |
148 ms |
344 KB |
Output is correct |
19 |
Correct |
306 ms |
344 KB |
Output is correct |
20 |
Correct |
303 ms |
488 KB |
Output is correct |
21 |
Correct |
867 ms |
1156 KB |
Output is correct |
22 |
Correct |
841 ms |
888 KB |
Output is correct |
23 |
Correct |
815 ms |
864 KB |
Output is correct |
24 |
Correct |
874 ms |
880 KB |
Output is correct |
25 |
Correct |
6 ms |
344 KB |
Output is correct |
26 |
Correct |
10 ms |
344 KB |
Output is correct |
27 |
Correct |
22 ms |
344 KB |
Output is correct |
28 |
Correct |
22 ms |
344 KB |
Output is correct |
29 |
Correct |
26 ms |
340 KB |
Output is correct |
30 |
Correct |
183 ms |
344 KB |
Output is correct |
31 |
Correct |
202 ms |
344 KB |
Output is correct |
32 |
Correct |
183 ms |
344 KB |
Output is correct |
33 |
Correct |
311 ms |
620 KB |
Output is correct |
34 |
Correct |
305 ms |
856 KB |
Output is correct |
35 |
Correct |
279 ms |
856 KB |
Output is correct |
36 |
Correct |
786 ms |
872 KB |
Output is correct |
37 |
Correct |
800 ms |
1056 KB |
Output is correct |
38 |
Correct |
841 ms |
1224 KB |
Output is correct |
39 |
Correct |
845 ms |
956 KB |
Output is correct |
40 |
Correct |
824 ms |
1068 KB |
Output is correct |
41 |
Correct |
776 ms |
708 KB |
Output is correct |
42 |
Correct |
830 ms |
1104 KB |
Output is correct |
43 |
Correct |
813 ms |
1284 KB |
Output is correct |
44 |
Correct |
817 ms |
1432 KB |
Output is correct |
45 |
Correct |
10 ms |
344 KB |
Output is correct |
46 |
Correct |
7 ms |
344 KB |
Output is correct |
47 |
Correct |
27 ms |
344 KB |
Output is correct |
48 |
Correct |
30 ms |
340 KB |
Output is correct |
49 |
Correct |
25 ms |
344 KB |
Output is correct |
50 |
Correct |
205 ms |
344 KB |
Output is correct |
51 |
Correct |
202 ms |
344 KB |
Output is correct |
52 |
Correct |
211 ms |
444 KB |
Output is correct |
53 |
Correct |
292 ms |
344 KB |
Output is correct |
54 |
Correct |
295 ms |
344 KB |
Output is correct |
55 |
Correct |
328 ms |
724 KB |
Output is correct |
56 |
Correct |
817 ms |
848 KB |
Output is correct |
57 |
Correct |
837 ms |
992 KB |
Output is correct |
58 |
Correct |
790 ms |
1280 KB |
Output is correct |
59 |
Correct |
805 ms |
944 KB |
Output is correct |
60 |
Correct |
757 ms |
1520 KB |
Output is correct |
61 |
Correct |
787 ms |
780 KB |
Output is correct |
62 |
Correct |
739 ms |
956 KB |
Output is correct |
63 |
Correct |
775 ms |
1236 KB |
Output is correct |
64 |
Correct |
815 ms |
1132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
26 ms |
344 KB |
Output is correct |
3 |
Partially correct |
140 ms |
344 KB |
Output is partially correct |
4 |
Partially correct |
363 ms |
740 KB |
Output is partially correct |
5 |
Partially correct |
786 ms |
1048 KB |
Output is partially correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Incorrect |
5 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |