제출 #500697

#제출 시각아이디문제언어결과실행 시간메모리
500697blueFirefighting (NOI20_firefighting)C++17
14 / 100
210 ms286804 KiB
#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;


    vvll dist(1+3000, vll(1+3000, INF));

        vvll has_edge(1+3000, vll(1+3000, 0));


            vector< pair<int, ll> > edge[1+3000];

int main()
{
    int N, K;
    cin >> N >> K;

    // for(int i = 1; i <= N; i++) dist[i][i] = 0;


    // vvll edge(1+N, vll(1+N, 0));


    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)
    {
        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);

            // if(N >= 100) while(1);

        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++)
                if(dist[i][v] <= K)
                    covered[i] = 1;
        }

        cout << int(res.size()) << '\n';
        for(int r: res) cout << r << ' ';
        cout << '\n';
    }



}
#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...