답안 #699372

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
699372 2023-02-16T17:01:51 Z anhduc2701 Parkovi (COCI22_parkovi) C++17
110 / 110
1952 ms 64076 KB
/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI 3.14159265359
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define BIT(x,i) (1&((x)>>(i)))
#define MASK(x) (1LL<<(x))
#define task "tnc"  
typedef long long ll;
const ll INF=1e18;
const int maxn=1e6+5;
const int mod=1e9+7;
const int mo=998244353;
using pi=pair<ll,ll>;
using vi=vector<ll>;
using pii=pair<pair<ll,ll>,ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,k;
int res[maxn];
vector<pair<int,int>>G[maxn];
int sum=0;
int f[maxn];
int dist[maxn];
int mark[maxn];
int dp[maxn];
void dfs(int u,int pa,int mid){
     mark[u] = 0, dp[u] = 0, f[u] = -INF, dist[u] = 0;
     for(int i=0; i<(int)G[u].size(); ++i)
    {
        int v = G[u][i].fi;
        if(v == pa) continue;
        dfs(v, u, mid);
        int w = G[u][i].se;
        f[u] = max(f[u], f[v] - w);
        dp[u] += dp[v];
        if(dist[v] == 0 && f[v] >= 0)
        {
            mark[v] = 2;
            continue;
        }
        if(dist[v] + w > mid)
        {
            mark[v] = 1;
            f[u] = max(f[u], mid - w);
            dp[u]++;
        }
    }
    for(auto v:G[u]){
        if(v.fi==pa || mark[v.fi])continue;
        if(f[u]-v.se>=dist[v.fi]){
            mark[v.fi]=2;
        }
    }
    for(auto v:G[u]){
        if(v.fi==pa || mark[v.fi])continue;
        dist[u]=max(dist[u],dist[v.fi]+v.se);
    }
}
bool check(long long mid)
{
    dfs(1, -1, mid);
    dp[1]++;
    if(dist[1] == 0 && f[1] >= 0)
        --dp[1];
    return (dp[1] <= k);
}

signed main()
{
	cin.tie(0),cout.tie(0)->sync_with_stdio(0);
    //freopen(task".inp" , "r" , stdin);
    //freopen(task".out" , "w" , stdout);
    cin>>n>>k;

    int l,r;
    l=0;
    for(int i=1;i<n;i++){
        int x,y,w;
        cin>>x>>y>>w;
        G[x].pb({y,w});
        G[y].pb({x,w});
    }
    r=1e15;
    int best;
    while(l <= r)
    {
        long long mid = (l+r)/2;
        if(check(mid))
        {
            best = mid;
            for(int i=1; i<=n; ++i)
                if(mark[i] == 1) res[i] = 1;
                else res[i] = 0;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    int cnt = 0;
    for(int i=1; i<=n; ++i) 
        cnt += res[i];
    cnt = k - cnt;
    for(int i=1; i<=n; ++i)
        if(res[i] == 0 && cnt > 0)
            res[i] = 1, --cnt;
    cout << best << '\n';
     for(int i=1; i<=n; ++i)
         if(res[i]) cout << i << " ";
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23764 KB Output is correct
2 Correct 15 ms 23776 KB Output is correct
3 Correct 17 ms 23832 KB Output is correct
4 Correct 19 ms 23852 KB Output is correct
5 Correct 19 ms 23756 KB Output is correct
6 Correct 14 ms 23788 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 20 ms 23760 KB Output is correct
9 Correct 18 ms 23748 KB Output is correct
10 Correct 12 ms 23824 KB Output is correct
11 Correct 18 ms 23784 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 15 ms 23780 KB Output is correct
14 Correct 15 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 490 ms 57656 KB Output is correct
2 Correct 500 ms 58572 KB Output is correct
3 Correct 567 ms 40844 KB Output is correct
4 Correct 1890 ms 44936 KB Output is correct
5 Correct 1853 ms 44268 KB Output is correct
6 Correct 1932 ms 44316 KB Output is correct
7 Correct 1679 ms 43136 KB Output is correct
8 Correct 1777 ms 44172 KB Output is correct
9 Correct 1786 ms 44388 KB Output is correct
10 Correct 1832 ms 44836 KB Output is correct
11 Correct 1122 ms 46636 KB Output is correct
12 Correct 1082 ms 46388 KB Output is correct
13 Correct 1224 ms 48500 KB Output is correct
14 Correct 1111 ms 44828 KB Output is correct
15 Correct 1090 ms 44416 KB Output is correct
16 Correct 1129 ms 46024 KB Output is correct
17 Correct 1077 ms 45116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 544 ms 59132 KB Output is correct
2 Correct 477 ms 58228 KB Output is correct
3 Correct 417 ms 60408 KB Output is correct
4 Correct 439 ms 60432 KB Output is correct
5 Correct 460 ms 62828 KB Output is correct
6 Correct 478 ms 62656 KB Output is correct
7 Correct 494 ms 64076 KB Output is correct
8 Correct 451 ms 63068 KB Output is correct
9 Correct 465 ms 62740 KB Output is correct
10 Correct 461 ms 62008 KB Output is correct
11 Correct 428 ms 60532 KB Output is correct
12 Correct 509 ms 63544 KB Output is correct
13 Correct 563 ms 63980 KB Output is correct
14 Correct 523 ms 63192 KB Output is correct
15 Correct 437 ms 61132 KB Output is correct
16 Correct 523 ms 59540 KB Output is correct
17 Correct 413 ms 59496 KB Output is correct
18 Correct 432 ms 61516 KB Output is correct
19 Correct 424 ms 60460 KB Output is correct
20 Correct 486 ms 61424 KB Output is correct
21 Correct 459 ms 60852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23764 KB Output is correct
2 Correct 15 ms 23776 KB Output is correct
3 Correct 17 ms 23832 KB Output is correct
4 Correct 19 ms 23852 KB Output is correct
5 Correct 19 ms 23756 KB Output is correct
6 Correct 14 ms 23788 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 20 ms 23760 KB Output is correct
9 Correct 18 ms 23748 KB Output is correct
10 Correct 12 ms 23824 KB Output is correct
11 Correct 18 ms 23784 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 15 ms 23780 KB Output is correct
14 Correct 15 ms 23764 KB Output is correct
15 Correct 490 ms 57656 KB Output is correct
16 Correct 500 ms 58572 KB Output is correct
17 Correct 567 ms 40844 KB Output is correct
18 Correct 1890 ms 44936 KB Output is correct
19 Correct 1853 ms 44268 KB Output is correct
20 Correct 1932 ms 44316 KB Output is correct
21 Correct 1679 ms 43136 KB Output is correct
22 Correct 1777 ms 44172 KB Output is correct
23 Correct 1786 ms 44388 KB Output is correct
24 Correct 1832 ms 44836 KB Output is correct
25 Correct 1122 ms 46636 KB Output is correct
26 Correct 1082 ms 46388 KB Output is correct
27 Correct 1224 ms 48500 KB Output is correct
28 Correct 1111 ms 44828 KB Output is correct
29 Correct 1090 ms 44416 KB Output is correct
30 Correct 1129 ms 46024 KB Output is correct
31 Correct 1077 ms 45116 KB Output is correct
32 Correct 544 ms 59132 KB Output is correct
33 Correct 477 ms 58228 KB Output is correct
34 Correct 417 ms 60408 KB Output is correct
35 Correct 439 ms 60432 KB Output is correct
36 Correct 460 ms 62828 KB Output is correct
37 Correct 478 ms 62656 KB Output is correct
38 Correct 494 ms 64076 KB Output is correct
39 Correct 451 ms 63068 KB Output is correct
40 Correct 465 ms 62740 KB Output is correct
41 Correct 461 ms 62008 KB Output is correct
42 Correct 428 ms 60532 KB Output is correct
43 Correct 509 ms 63544 KB Output is correct
44 Correct 563 ms 63980 KB Output is correct
45 Correct 523 ms 63192 KB Output is correct
46 Correct 437 ms 61132 KB Output is correct
47 Correct 523 ms 59540 KB Output is correct
48 Correct 413 ms 59496 KB Output is correct
49 Correct 432 ms 61516 KB Output is correct
50 Correct 424 ms 60460 KB Output is correct
51 Correct 486 ms 61424 KB Output is correct
52 Correct 459 ms 60852 KB Output is correct
53 Correct 1821 ms 44732 KB Output is correct
54 Correct 1723 ms 45380 KB Output is correct
55 Correct 1931 ms 46316 KB Output is correct
56 Correct 1744 ms 45708 KB Output is correct
57 Correct 1776 ms 45984 KB Output is correct
58 Correct 1736 ms 45172 KB Output is correct
59 Correct 1782 ms 46932 KB Output is correct
60 Correct 1952 ms 44844 KB Output is correct
61 Correct 1636 ms 42940 KB Output is correct
62 Correct 1705 ms 43368 KB Output is correct
63 Correct 1868 ms 44928 KB Output is correct
64 Correct 1749 ms 44812 KB Output is correct
65 Correct 1776 ms 44992 KB Output is correct
66 Correct 1729 ms 44724 KB Output is correct
67 Correct 1653 ms 43776 KB Output is correct
68 Correct 1868 ms 46396 KB Output is correct
69 Correct 447 ms 62700 KB Output is correct
70 Correct 415 ms 60068 KB Output is correct
71 Correct 526 ms 63728 KB Output is correct
72 Correct 627 ms 44084 KB Output is correct
73 Correct 545 ms 43636 KB Output is correct
74 Correct 589 ms 44468 KB Output is correct
75 Correct 1097 ms 46972 KB Output is correct
76 Correct 1181 ms 46796 KB Output is correct
77 Correct 1116 ms 46408 KB Output is correct
78 Correct 1099 ms 45828 KB Output is correct