이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vi = vector<int>;
const ll INF = 1'000'000'000'000'000'000LL;
int main()
{
int N, K;
cin >> N >> K;
vvll dist(1+N, vll(1+N, INF));
// for(int i = 1; i <= N; i++) dist[i][i] = 0;
vvll has_edge(1+N, vll(1+N, 0));
// vvll edge(1+N, vll(1+N, 0));
vector< pair<int, ll> > edge[1+N];
for(int e = 1; e <= N-1; e++)
{
int a, b, d;
cin >> a >> b >> d;
has_edge[a][b] = has_edge[b][a] = 1;
// dist[a][b] = dist[b][a] = d;
// edge[a][b] = edge[b][a] = 1;
edge[a].push_back({b, d});
edge[b].push_back({a, d});
}
for(int i = 1; i <= N; i++)
{
queue<int> tbv;
tbv.push(i);
dist[i][i] = 0;
while(!tbv.empty())
{
int u = tbv.front();
tbv.pop();
for(auto q: edge[u])
{
int v = q.first;
ll w = q.second;
if(dist[i][v] < INF) continue;
dist[i][v] = dist[i][u] + w;
tbv.push(v);
}
}
}
// for(int k = 1; k <= N; k++)
// for(int i = 1; i <= N; i++)
// for(int j = 1; j <= N; j++)
// dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
vi par(1+N, -1);
for(int i = 2; i <= N; i++)
{
for(int j = 1; j <= N; j++)
{
if(j == i) continue;
if(dist[1][i] == dist[1][j] + dist[j][i] && has_edge[i][j] == 1)
{
par[i] = j;
break;
}
}
}
if(N >= 100) while(1);
vector<pair<ll, int> > V;
for(int i = 1; i <= N; i++) V.push_back({dist[1][i], i});
sort(V.begin(), V.end());
vi res;
reverse(V.begin(), V.end());
vi covered(1+N, 0);
for(auto q: V)
{
int u = q.second;
if(covered[u]) continue;
int v = u;
while(v != 1 && dist[par[v]][u] <= K) v = par[v];
res.push_back(v);
for(int i = 1; i <= N; i++)
covered[i] |= (dist[v][i] <= K);
}
cout << int(res.size()) << '\n';
for(int r: res) cout << r << ' ';
cout << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |