Submission #525091

#TimeUsernameProblemLanguageResultExecution timeMemory
525091benedict0724Firefighting (NOI20_firefighting)C++17
0 / 100
294 ms25872 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, ll> pi;
int n; ll k;

vector<pi> adj[300002];
bool chk[300002];
int ans = 0;

ll dfs(int x, int p, ll d)
{
    vector<ll> v;
    int cnt = 0;
    for(auto u : adj[x])
    {
        int i = u.first;
        if(i == p) continue;
        ll c = u.second;
        ll P = dfs(i, x, c);
        v.push_back(P);
    }

    sort(v.begin(), v.end());

    if(v.empty())
    {
        if(d > k)
        {
            chk[x] = 1; ans++;
            return 2*k+2;
        }
        return -(d+k+1);
    }

    else
    {
        ll D = v[0];
        int t = 1;
        if(D < 0) { t = -1; D = -D; }
        if(D > k && D + d > 2*k+1)
        {
            chk[x] = 1; ans++;
            return t*min(d, k+1);
        }
        else if(D > k) return t*(D+d);
        if(D + d > k) return t*(k+1);
        else return t*(D+d);
    }

}

int main()
{
    ios::sync_with_stdio(false); cin.tie(NULL);
    cin >> n >> k;
    for(int i=1;i<n;i++)
    {
        int a, b; ll D; cin >> a >> b >> D;
        adj[a].push_back({b, D});
        adj[b].push_back({a, D});
    }

    ll P = dfs(1, 0, 0);
    if(P < 0) P = -P;
    if(P == 0) { chk[1] = 1; ans++; }
    if(P > k) { chk[1] = 1; ans++; }

    cout << ans << "\n";

    for(int i=1;i<=n;i++) if(chk[i]) cout << i << " ";
}

Compilation message (stderr)

Firefighting.cpp: In function 'll dfs(int, int, ll)':
Firefighting.cpp:15:9: warning: unused variable 'cnt' [-Wunused-variable]
   15 |     int cnt = 0;
      |         ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...