제출 #1353146

#제출 시각아이디문제언어결과실행 시간메모리
1353146hunglpParkovi (COCI22_parkovi)C++20
20 / 110
460 ms48652 KiB
//(づ ̄3 ̄)づ╭❤️~
// Huwng Lee ~_~
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

#define int long long
#define pl pair <ll, ll>
#define pll pair <pl, ll>
#define pi pair <int, int>
#define pb push_back
#define fi first
#define se second
#define ld long double
#define fti(i, x, y) for(int i = (x), _y = (y); i <= _y; ++ i)
#define ftd(i, x, y) for(int i = (x), _y = (y); i >= _y; -- i)

using namespace std;

typedef long long ll;

const int e = 2e5 + 2;
const ll oo = 1e18;

int n, k;

ll need[e], cover[e];

ll d1[e], d2[e], d[e], tmp;

vector <pi> a[e];

vector <int> ans;

bool park[e];

void lph()
{
    freopen("park.inp", "r", stdin);
    freopen("park.out", "w", stdout);
}

void inp()
{
    cin >> n >> k;
    fti(i, 1, n - 1)
    {
        int u, v, w;
        cin >> u >> v >> w;
        a[u].pb({v, w});
        a[v].pb({u, w});
    }
}

void reset(ll d[])
{
    fti(i, 1, n)
        d[i] = oo;
}

ll bfs(queue <int> &qu, ll d[])
{
    while(qu.size())
    {
        int u = qu.front();
        qu.pop();
        for(auto [v, w]: a[u])
        {
            if(d[v] > d[u] + w)
            {
                d[v] = d[u] + w;
                qu.push(v);
            }
        }
    }
    ll res = 0;
    fti(i, 1, n)
        res = max(res, d[i]);
    return res;
}

void sub1()
{
    int nmask = (1 << n);
    ll res = oo;
    fti(mask, 0, nmask - 1)
    {
        if(__builtin_popcount(mask) != k)
            continue;
        queue <int> qu;
        vector <int> tmp;
        reset(d);
        fti(i, 1, n)
        {
            if((mask >> i - 1) & 1)
            {
                d[i] = 0;
                qu.push(i);
                tmp.pb(i);
            }
        }
        ll mx = bfs(qu, d);
        if(mx < res)
        {
            res = mx;
            ans = tmp;
        }
    }
    cout << res << "\n";
    for(int u: ans)
        cout << u << " ";
}

void dk(int u, int p, ll d[], int &node)
{
    for(auto [v, w]: a[u])
    {
        if(v == p)
            continue;
        d[v] = d[u] + w;
        if(d[v] > tmp)
            tmp = d[v], node = v;
        dk(v, u, d, node);
    }
}

void sub2()
{
    int st = 0, en = 0;

    d[1] = 0;
    dk(1, 0, d, st);

    d1[st] = 0;
    tmp = 0;
    dk(st, 0, d1, en);

    d2[en] = 0;
    tmp = 0;
    dk(en, 0, d2, st);

    ll ans = oo, id = 0;
    fti(i, 1, n)
    {
        ll mx = max(d1[i], d2[i]);
        if(mx < ans)
            ans = mx, id = i;
    }
    cout << ans << "\n" << id;
}

bool check1(ll x, vector <int> &tmp)
{
    int cnt = 0, j = 0;
    ll s = 0;
    fti(i, 1, n)
    {
        if(d1[i] - s >= x)
        {
            ++ cnt;
            tmp.pb(i);
            s = d1[i];
        }
        if(cnt > k)
            return 0;
    }
    return cnt <= k;
}

void sub3()
{
    reset(d);
    queue <int> qu;
    qu.push(1);
    d[1] = 0;
    bfs(qu, d1);

    while(qu.size()) qu.pop();
    d[n] = 1;
    qu.push(n);
    bfs(qu, d2);

    ll l = 0, r = oo, res = 0;
    while(l <= r)
    {
        ll mid = (l + r) >> 1;
        vector <int> tmp;
        if(check1(mid, tmp))
        {
            r = mid - 1;
            res = mid;
            ans = tmp;
        }
        else
            l = mid + 1;
    }
    cout << res << "\n";
    for(int u: ans)
        cout << u << " ";
}

void dfs(int u, int p, int &cnt, vector <int> &tmp, ll R)
{
    need[u] = 0;
    cover[u] = oo;
    for(auto [v, w]: a[u])
    {
        if(v == p)
            continue;
        dfs(v, u, cnt, tmp, R);
        if(need[v] != -1)
        {
            if(need[v] + w > R)
            {
                ++ cnt;
                cover[u] = min(cover[u], 1ll * w);
                tmp.pb(v);
                park[v] = 1;
            }
            else
                need[u] = max(need[u], need[v] + w);
        }
        else if(cover[v] != oo)
            cover[u] = min(cover[u], cover[v] + w);
    }
    if(need[u] != -1 && cover[u] != oo && need[u] + cover[u] <= R)
        need[u] = -1;
}

bool check(ll R, vector <int> &tmp)
{
    fti(i, 1, n)
        park[i] = 0;
    int cnt = 0;
    dfs(1, 0, cnt, tmp, R);
    if(need[1] != -1)
    {
        ++ cnt, tmp.pb(1);
        park[1] = 1;
    }
    return cnt <= k;
}

void sub4()
{
    ll l = 0, r = oo, res = oo;
    while(l <= r)
    {
        ll mid = (l + r) >> 1;
        vector <int> tmp;
        if(check(mid, tmp))
        {
            r = mid - 1, res = mid;
            ans = tmp;
        }
        else
            l = mid + 1;
    }
    cout << res << "\n";
    k -= ans.size();
    fti(i, 1, n)
        if(!park[i] && k)
            ans.pb(i), -- k;
    for(int u: ans)
        cout << u << " ";
}

void proc()
{
//    if(n <= 20)
//        sub1();
//    else if(k == 1)
//        sub2();
//    else
//        sub3();
    sub4();
}

main()
{
//    lph();
    ios_base::sync_with_stdio(0);cin.tie(nullptr);
    inp();
    proc();
    return 0;
}
/*
9 3
1 2 5
1 3 1
3 4 10
3 5 9
5 6 8
2 7 1
2 8 2
8 9 7

*/

컴파일 시 표준 에러 (stderr) 메시지

Main.cpp:279:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  279 | main()
      | ^~~~
Main.cpp: In function 'void lph()':
Main.cpp:39:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |     freopen("park.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:40:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |     freopen("park.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…