# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
523944 | qwerasdfzxcl | Firefighting (NOI20_firefighting) | C++14 | 3073 ms | 49048 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<pair<int, ll>> adj[300300];
pair<int, ll> par[300300];
ll dep[300300];
int deg[300300], visited[300300];
priority_queue<pair<ll, int>> pq;
void dfs(int s, int pa = -1){
deg[s] = adj[s].size();
for (auto &v:adj[s]) if (v.first!=pa){
par[v.first] = {s, v.second};
dep[v.first] = dep[s] + v.second;
dfs(v.first, s);
}
}
int cnt;
void dfs2(int s, ll K){
visited[s] = cnt;
for (auto &v:adj[s]) if (visited[v.first]!=cnt){
if (v.second<=K) dfs2(v.first, K-v.second);
else{
deg[v.first]--;
if (deg[v.first]==1 && !visited[v.first]) pq.emplace(dep[v.first], v.first);
}
}
}
int main(){
int n;
ll D;
scanf("%d %lld", &n, &D);
for (int i=0;i<n-1;i++){
int x, y;
ll z;
scanf("%d %d %lld", &x, &y, &z);
adj[x].emplace_back(y, z);
adj[y].emplace_back(x, z);
}
if (n==1) {printf("1\n1\n"); return 0;}
dfs(1);
for (int i=1;i<=n;i++) if (deg[i]==1) pq.emplace(dep[i], i);
vector<int> ans;
while(!pq.empty()){
auto cur = pq.top(); pq.pop();
if (visited[cur.second]) continue;
int s = cur.second;
ll rem = D;
while(par[s].first && par[s].second<=rem){
rem -= par[s].second;
s = par[s].first;
}
ans.push_back(s);
cnt++;
dfs2(s, D);
}
printf("%d\n", (int)ans.size());
for (auto &x:ans) printf("%d ", x);
return 0;
}
Compilation message (stderr)
# | 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... |