Submission #1245464

#TimeUsernameProblemLanguageResultExecution timeMemory
1245464ducdevPastiri (COI20_pastiri)C++20
8 / 100
123 ms33496 KiB
// Author: 4uckd3v - Nguyen Cao Duc
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAX_N = 5e5;
const int MOD = 1e9 + 7;
const int INF = 1e9;

template <class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x <= y) return false;
    x = y;
    return true;
};

int n, k;
int sheepIdx[MAX_N + 5];
vector<int> adj[MAX_N + 5];

namespace SUBTASK_1 {
    const int N = 5e5;

    int dp[N + 5];

    bool checkSubtask() {
        for (int u = 1; u <= n; u++)
            if (adj[u].size() > 2) return false;
        return true;
    };

    void trace(int i) {
        vector<int> t;
        while (i > 0) {
            if (dp[i] == dp[i - 1] + 1) {
                t.push_back(sheepIdx[i]);
                i--;
            } else {
                int prevIdx = sheepIdx[i - 1];
                int curIdx = sheepIdx[i];
                t.push_back((curIdx + prevIdx + 1) >> 1);
                i -= 2;
            };
        };

        for (int idx : t) cout << idx << ' ';
        cout << '\n';
    };

    void Solve() {
        for (int i = 1; i <= n; i++) dp[i] = INF;

        dp[1] = 1;

        for (int i = 2; i <= k; i++) {
            int prevIdx = sheepIdx[i - 1];
            int curIdx = sheepIdx[i];

            if ((curIdx - prevIdx + 1) & 1) {
                minimize(dp[i], dp[i - 2] + 1);
            };

            minimize(dp[i], dp[i - 1] + 1);
        };

        cout << dp[k] << '\n';
        trace(k);
    };
};  // namespace SUBTASK_1

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (fopen("PASTIRI.INP", "r")) {
        freopen("PASTIRI.INP", "r", stdin);
        freopen("PASTIRI.OUT", "w", stdout);
    };

    cin >> n >> k;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    };

    for (int i = 1; i <= k; i++) {
        cin >> sheepIdx[i];
    };

    sort(sheepIdx + 1, sheepIdx + k + 1);

    if (SUBTASK_1::checkSubtask())
      return SUBTASK_1::Solve(), 0;
};

Compilation message (stderr)

pastiri.cpp: In function 'int main()':
pastiri.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         freopen("PASTIRI.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
pastiri.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |         freopen("PASTIRI.OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...