답안 #791270

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
791270 2023-07-23T21:02:28 Z Username4132 Parkovi (COCI22_parkovi) C++14
110 / 110
561 ms 32192 KB
#include<iostream>
#include<vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using plb = pair<ll, bool>;
#define forn(i, n) for(int i=0; i<(int)n; ++i)
#define PB push_back
#define F first
#define S second

const ll INF = 200000000000010;
const int MAXN = 200010;
int n, k;
bool pres[MAXN];
vector<int> ans;
ll target;
vector<pii> g[MAXN];

plb dfs(int v, int p, ll cost){
    ll mxD=0, mxS=-1;
    for(pii to:g[v]) if(to.F!=p){
        plb res = dfs(to.F, v, to.S);
        if(res.S) mxS = max(mxS, res.F - to.S);
        else mxD = max(mxD, res.F + to.S);
    }
    if(mxD <= mxS) return {mxS, true};
    else if(mxD + cost > target){
        ans.PB(v);
        return {target, true};
    }
    else return {mxD, false};
}


int main(){
    scanf("%d %d", &n, &k);
    forn(i, n-1){
        int a, b, c; scanf("%d %d %d", &a, &b, &c); --a, --b;
        g[a].PB({b, c}), g[b].PB({a, c});
    }
    ll lo=-1, hi=INF;
    while(hi-lo>1){
        ll mid = (hi+lo)/2;
        ans.clear(), target=mid;
        dfs(0, 0, 2*INF);
        if((int)ans.size()<=k) hi=mid;
        else lo=mid;
    }
    ans.clear();
    target=hi;
    dfs(0, 0, 2*INF);
    for(int el:ans) pres[el]=true;
    forn(i, n) if(!pres[i] && (int)ans.size()<k) ans.PB(i), pres[i]=true;
    printf("%lld\n", hi);
    forn(i, n) if(pres[i]) printf("%d ", i+1);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:39:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         int a, b, c; scanf("%d %d %d", &a, &b, &c); --a, --b;
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5052 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 2 ms 4992 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 5004 KB Output is correct
14 Correct 2 ms 4900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 229 ms 28348 KB Output is correct
2 Correct 239 ms 30376 KB Output is correct
3 Correct 214 ms 17616 KB Output is correct
4 Correct 407 ms 16152 KB Output is correct
5 Correct 380 ms 15784 KB Output is correct
6 Correct 456 ms 15796 KB Output is correct
7 Correct 359 ms 14504 KB Output is correct
8 Correct 416 ms 14996 KB Output is correct
9 Correct 428 ms 15528 KB Output is correct
10 Correct 461 ms 15792 KB Output is correct
11 Correct 341 ms 17328 KB Output is correct
12 Correct 298 ms 17268 KB Output is correct
13 Correct 368 ms 18576 KB Output is correct
14 Correct 347 ms 15928 KB Output is correct
15 Correct 309 ms 15528 KB Output is correct
16 Correct 329 ms 16892 KB Output is correct
17 Correct 349 ms 16208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 251 ms 30836 KB Output is correct
2 Correct 231 ms 30100 KB Output is correct
3 Correct 245 ms 28840 KB Output is correct
4 Correct 223 ms 28724 KB Output is correct
5 Correct 241 ms 31800 KB Output is correct
6 Correct 260 ms 31208 KB Output is correct
7 Correct 288 ms 32192 KB Output is correct
8 Correct 227 ms 30320 KB Output is correct
9 Correct 235 ms 30164 KB Output is correct
10 Correct 222 ms 29500 KB Output is correct
11 Correct 221 ms 28564 KB Output is correct
12 Correct 267 ms 32192 KB Output is correct
13 Correct 295 ms 32116 KB Output is correct
14 Correct 307 ms 31192 KB Output is correct
15 Correct 231 ms 28880 KB Output is correct
16 Correct 230 ms 27872 KB Output is correct
17 Correct 230 ms 27828 KB Output is correct
18 Correct 245 ms 29036 KB Output is correct
19 Correct 244 ms 29760 KB Output is correct
20 Correct 287 ms 30164 KB Output is correct
21 Correct 263 ms 29784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5052 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 2 ms 4992 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 5004 KB Output is correct
14 Correct 2 ms 4900 KB Output is correct
15 Correct 229 ms 28348 KB Output is correct
16 Correct 239 ms 30376 KB Output is correct
17 Correct 214 ms 17616 KB Output is correct
18 Correct 407 ms 16152 KB Output is correct
19 Correct 380 ms 15784 KB Output is correct
20 Correct 456 ms 15796 KB Output is correct
21 Correct 359 ms 14504 KB Output is correct
22 Correct 416 ms 14996 KB Output is correct
23 Correct 428 ms 15528 KB Output is correct
24 Correct 461 ms 15792 KB Output is correct
25 Correct 341 ms 17328 KB Output is correct
26 Correct 298 ms 17268 KB Output is correct
27 Correct 368 ms 18576 KB Output is correct
28 Correct 347 ms 15928 KB Output is correct
29 Correct 309 ms 15528 KB Output is correct
30 Correct 329 ms 16892 KB Output is correct
31 Correct 349 ms 16208 KB Output is correct
32 Correct 251 ms 30836 KB Output is correct
33 Correct 231 ms 30100 KB Output is correct
34 Correct 245 ms 28840 KB Output is correct
35 Correct 223 ms 28724 KB Output is correct
36 Correct 241 ms 31800 KB Output is correct
37 Correct 260 ms 31208 KB Output is correct
38 Correct 288 ms 32192 KB Output is correct
39 Correct 227 ms 30320 KB Output is correct
40 Correct 235 ms 30164 KB Output is correct
41 Correct 222 ms 29500 KB Output is correct
42 Correct 221 ms 28564 KB Output is correct
43 Correct 267 ms 32192 KB Output is correct
44 Correct 295 ms 32116 KB Output is correct
45 Correct 307 ms 31192 KB Output is correct
46 Correct 231 ms 28880 KB Output is correct
47 Correct 230 ms 27872 KB Output is correct
48 Correct 230 ms 27828 KB Output is correct
49 Correct 245 ms 29036 KB Output is correct
50 Correct 244 ms 29760 KB Output is correct
51 Correct 287 ms 30164 KB Output is correct
52 Correct 263 ms 29784 KB Output is correct
53 Correct 370 ms 16064 KB Output is correct
54 Correct 452 ms 16396 KB Output is correct
55 Correct 447 ms 16892 KB Output is correct
56 Correct 417 ms 16972 KB Output is correct
57 Correct 426 ms 17704 KB Output is correct
58 Correct 386 ms 16152 KB Output is correct
59 Correct 561 ms 19004 KB Output is correct
60 Correct 402 ms 15380 KB Output is correct
61 Correct 325 ms 14412 KB Output is correct
62 Correct 350 ms 15700 KB Output is correct
63 Correct 414 ms 15472 KB Output is correct
64 Correct 424 ms 17024 KB Output is correct
65 Correct 431 ms 15880 KB Output is correct
66 Correct 432 ms 16672 KB Output is correct
67 Correct 341 ms 15240 KB Output is correct
68 Correct 485 ms 18356 KB Output is correct
69 Correct 246 ms 30312 KB Output is correct
70 Correct 217 ms 28372 KB Output is correct
71 Correct 296 ms 31664 KB Output is correct
72 Correct 149 ms 17020 KB Output is correct
73 Correct 159 ms 16556 KB Output is correct
74 Correct 181 ms 18188 KB Output is correct
75 Correct 327 ms 17736 KB Output is correct
76 Correct 343 ms 16924 KB Output is correct
77 Correct 335 ms 17036 KB Output is correct
78 Correct 336 ms 17056 KB Output is correct