Submission #827438

# Submission time Handle Problem Language Result Execution time Memory
827438 2023-08-16T13:15:37 Z Kubogi Parkovi (COCI22_parkovi) C++17
0 / 110
357 ms 35892 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fileio(name) if (fopen(name".inp", "r")) freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout)

const int maxn = 2e5+4, inf = 1e18;
int n, k;
vector<pair<int, int> > adj[maxn];

int f[maxn], g[maxn];
bool chosen[maxn];
void dfs(int u, int p, int val, int wp) {
    for (auto e: adj[u]) {
        int v = e.first, w = e.second;
        if (v != p) {
            dfs(v, u, val, w);
            f[u] = max(f[u], w + f[v]);
            g[u] = min(g[u], w + g[v]);
        }
    }
    if (f[u] + g[u] > val) {
        if (f[u] + wp > val) {
            chosen[u] = true;
            f[u] = -inf, g[u] = 0;
        }
    }
}

int cnt(int val) {
    fill(f+1, f+1+n, 0);
    fill(g+1, g+1+n, inf);
    fill(chosen+1, chosen+1+n, false);
    dfs(1, 0, val, -inf);
    chosen[1] = (f[1] + g[1] > val);

    int res = 0;
    for (int i = 1; i <= n; i++) {
        res += chosen[i];
    }
    return res;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    fileio("ktree");
    // freopen("debug.txt", "w", stderr);

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

    int l = 0, r = 1e15;
    while (l <= r) {
        int mid = (l+r)>>1;
        if (cnt(mid) <= k) r = mid-1;
        else l = mid+1;
    }
    cout << l << "\n";
    int x = cnt(l);
    for (int i = 1; i <= n; i++) {
        if (chosen[i]) cout << i << " ";
        else if (x < k) {
            cout << i << " ";
            x++;
        }
    }

    return 0 ^ 0;
}

Compilation message

Main.cpp: In function 'int32_t main()':
Main.cpp:5:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define fileio(name) if (fopen(name".inp", "r")) freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout)
      |                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:47:5: note: in expansion of macro 'fileio'
   47 |     fileio("ktree");
      |     ^~~~~~
Main.cpp:5:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define fileio(name) if (fopen(name".inp", "r")) freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout)
      |                                                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:47:5: note: in expansion of macro 'fileio'
   47 |     fileio("ktree");
      |     ^~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 336 ms 34544 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 357 ms 35892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -