#include "bits/stdc++.h"
#include "crocodile.h"
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
void dfs(vector<pair<int, int>> tree[], vector<int> &parent, vector<int> &dist, vector<int> &len, int u, int e)
{
for(auto v: tree[u])
{
if(v.fi==e)
continue;
parent[v.fi] = u;
dist[v.fi] = dist[u] + v.se;
len[v.fi] = len[u] + 1;
dfs(tree, parent, dist, len, v.fi, u);
}
}
void remove_subtree(vector<pair<int, int>> tree[], vector<bool> &erased, int u, int e)
{
erased[u] = true;
for(auto v: tree[u])
if(v.fi!=e)
remove_subtree(tree, erased, v.fi, u);
}
int travel_plan(int n, int m, int r[][2], int l[], int k, int p[])
{
vector<pair<int, int>> tree[n];
for(int i = 0; i<m; i++)
{
tree[r[i][0]].pb(mp(r[i][1], l[i]));
tree[r[i][1]].pb(mp(r[i][0], l[i]));
}
vector<int> dist(n, 0);
vector<int> len(n, 0);
vector<int> parent(n, -1);
dfs(tree, parent, dist, len, 0, -1);
vector<pair<pair<int, int>, int>> arr;
for(int i = 0; i<k; i++)
{
arr.pb(mp(mp(dist[p[i]], len[p[i]]), p[i]));
}
sort(arr.begin(), arr.end());
vector<bool> erased(n, false);
int res = 0;
/*for(int i = 0; i<k; i++)
cout << arr[i].fi.fi << ' ' << arr[i].fi.se << ' ' << arr[i].se << endl;*/
sort(p, p+k);
for(int i = 0; i<k; i++)
{
if(erased[arr[i].se])
continue;
if(i+1<arr[i].fi.se)
{
remove_subtree(tree, erased, parent[arr[i].se], parent[parent[arr[i].se]]);
}
else if(i+1==arr[i].fi.se)
{
vector<int> temp;
for(auto u: tree[parent[arr[i].se]])
{
if(binary_search(p, p+k, u.fi))
temp.pb(dist[u.fi]);
}
if(temp.size()>1)
{
sort(temp.begin(), temp.end());
res = temp[1];
break;
}
}
else
{
res = arr[i].fi.fi;
break;
}
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |