답안 #77171

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
77171 2018-09-23T09:09:37 Z muradeyn 경주 (Race) (IOI11_race) C++14
100 / 100
2005 ms 67112 KB
#include "race.h"
#include <bits/stdc++.h>
#define intt long long
#define FAST_READ ios_base::sync_with_stdio(0);cin.tie(0);
#define SIZE 200001
#define INF INT_MAX
#define F first
#define S second
#define in(a) scanf("%d",&a);
#define outn(a) printf("%d\n",&a);
#define outs(a) printf("%d ",&a);


using namespace std;

int root , n , x , y , w , used[SIZE] , dp[SIZE] , co , cent = -1 , ans = -1;

intt we  , k;

vector< pair<int,intt> >v[SIZE] , all;
vector<int> ve;

map< int , int > mp;

bool f = true;

void init() {
    for (int i = 1;i<=n;i++) {
        if (used[i] < 5 && used[i] > 0)used[i] = 0;
    }
}

void dfs(int s,int p) {
    co++;
    used[s] = 1;
    ve.push_back(s);
    int sz = v[s].size();
    for (int i = 0;i<sz;i++) {
        int to = v[s][i].first;
        intt cost = v[s][i].second;
        if (to == p)continue;
        if (used[to] >= 1)continue;
        we += cost;
        dfs(to,s);
    }
}

void dfs2(int s,int p) {
    used[s] = 2;
    dp[s] = 1;
    bool f = true;
    int sz = v[s].size();
    for (int i = 0;i<sz;i++) {
        int to = v[s][i].first;
        if (used[to] >= 2)continue;
        dfs2(to,s);
        if (dp[to] > co / 2) f = false;
        dp[s] += dp[to];
    }
    if (f && cent == -1 && co - dp[s] <= co / 2) cent = s;
}

void dfs3(int s,int p,int len,intt cst) {
    used[s] = 3;
    if (cst == k) {
        if (ans == -1)ans = len;
        else ans = min(ans,len);
    }
    int sz = v[s].size();
    for (int i = 0;i<sz;i++) {
        int to = v[s][i].first;
        intt cost = v[s][i].second;
        if (to == p)continue;
        if (used[to] >= 3)continue;
        dfs3(to,s,len + 1,cst + cost);
    }
}

void dfs4(int s,int p,int len,intt cst) {
    used[s] = 4;
    //cout<<s - 1<<" "<<cst<<" "<<k<<endl;
    //char aa;
    //cin>>aa;
    if (cst > k)return;
    if (cst == k) {
        if (ans == -1)ans = len;
        else ans = min(ans,len);
    }
    else if (mp[k - cst] != 0) {
        if (ans == -1)ans = len + mp[k - cst];
        else ans = min(ans,len + mp[k - cst]);
    }
    int sz = v[s].size();
    //cout<<sz<<endl;
    for (int i = 0;i<sz;i++) {
        int to = v[s][i].first;
        intt cost = v[s][i].second;
        if (to == p)continue;
        if (used[to] >= 4)continue;
        dfs4(to,s,len + 1,cst + cost);
        if (root == s) {
            int sz1 = all.size();
            for (int j = 0;j<sz1;j++) {
                if (mp[all[j].first] == 0)mp[all[j].first] = all[j].second;
                else mp[all[j].first] = min(mp[all[j].first] * 1LL,all[j].second);
            }
            all.clear();
        }
    }
    all.push_back( {cst,len} );
}

int best_path(int N, int K, int h[][2], int l[])
{
    k = K;
    n = N;
    for (int i = 0;i<n - 1;i++) {
        x = h[i][0];
        y = h[i][1];
        x++;
        y++;
        v[x].push_back({y,l[i]});
        v[y].push_back({x,l[i]});
    }
    while (f) {
        f = false;
        init();
        for (int i = 1;i<=n;i++) {
            if (used[i] == 0) {
              co = 0; we = 0;
                dfs(i , -1);
                ve.clear();
                if (we < k || co == 1)  {
                    for (int i = 0;i<co;i++)used[ve[i]] = 5;
                    continue;
                }
                dp[i] = 0;
                cent = -1;
                dfs2(i,-1);
                if (cent == -1) {
                    for (int i = 0;i<co;i++)used[ve[i]] = 5;
                    continue;
                }
                f = true;
                root = cent;
                dfs3(cent,-1,0,0);
                mp.clear();
                dfs4(cent,-1,0,0);
                used[cent] = 5;
            }
        }
    }
    return ans;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 7 ms 5192 KB Output is correct
4 Correct 6 ms 5208 KB Output is correct
5 Correct 6 ms 5268 KB Output is correct
6 Correct 6 ms 5268 KB Output is correct
7 Correct 6 ms 5268 KB Output is correct
8 Correct 7 ms 5312 KB Output is correct
9 Correct 7 ms 5312 KB Output is correct
10 Correct 6 ms 5312 KB Output is correct
11 Correct 6 ms 5356 KB Output is correct
12 Correct 6 ms 5356 KB Output is correct
13 Correct 6 ms 5356 KB Output is correct
14 Correct 6 ms 5356 KB Output is correct
15 Correct 6 ms 5356 KB Output is correct
16 Correct 5 ms 5356 KB Output is correct
17 Correct 6 ms 5356 KB Output is correct
18 Correct 6 ms 5356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 7 ms 5192 KB Output is correct
4 Correct 6 ms 5208 KB Output is correct
5 Correct 6 ms 5268 KB Output is correct
6 Correct 6 ms 5268 KB Output is correct
7 Correct 6 ms 5268 KB Output is correct
8 Correct 7 ms 5312 KB Output is correct
9 Correct 7 ms 5312 KB Output is correct
10 Correct 6 ms 5312 KB Output is correct
11 Correct 6 ms 5356 KB Output is correct
12 Correct 6 ms 5356 KB Output is correct
13 Correct 6 ms 5356 KB Output is correct
14 Correct 6 ms 5356 KB Output is correct
15 Correct 6 ms 5356 KB Output is correct
16 Correct 5 ms 5356 KB Output is correct
17 Correct 6 ms 5356 KB Output is correct
18 Correct 6 ms 5356 KB Output is correct
19 Correct 6 ms 5356 KB Output is correct
20 Correct 6 ms 5356 KB Output is correct
21 Correct 7 ms 5372 KB Output is correct
22 Correct 7 ms 5372 KB Output is correct
23 Correct 8 ms 5372 KB Output is correct
24 Correct 7 ms 5384 KB Output is correct
25 Correct 8 ms 5500 KB Output is correct
26 Correct 7 ms 5500 KB Output is correct
27 Correct 6 ms 5500 KB Output is correct
28 Correct 8 ms 5500 KB Output is correct
29 Correct 8 ms 5500 KB Output is correct
30 Correct 8 ms 5500 KB Output is correct
31 Correct 8 ms 5500 KB Output is correct
32 Correct 8 ms 5500 KB Output is correct
33 Correct 8 ms 5608 KB Output is correct
34 Correct 7 ms 5608 KB Output is correct
35 Correct 7 ms 5608 KB Output is correct
36 Correct 7 ms 5608 KB Output is correct
37 Correct 7 ms 5608 KB Output is correct
38 Correct 7 ms 5608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 7 ms 5192 KB Output is correct
4 Correct 6 ms 5208 KB Output is correct
5 Correct 6 ms 5268 KB Output is correct
6 Correct 6 ms 5268 KB Output is correct
7 Correct 6 ms 5268 KB Output is correct
8 Correct 7 ms 5312 KB Output is correct
9 Correct 7 ms 5312 KB Output is correct
10 Correct 6 ms 5312 KB Output is correct
11 Correct 6 ms 5356 KB Output is correct
12 Correct 6 ms 5356 KB Output is correct
13 Correct 6 ms 5356 KB Output is correct
14 Correct 6 ms 5356 KB Output is correct
15 Correct 6 ms 5356 KB Output is correct
16 Correct 5 ms 5356 KB Output is correct
17 Correct 6 ms 5356 KB Output is correct
18 Correct 6 ms 5356 KB Output is correct
19 Correct 374 ms 12936 KB Output is correct
20 Correct 385 ms 12948 KB Output is correct
21 Correct 383 ms 13556 KB Output is correct
22 Correct 360 ms 13560 KB Output is correct
23 Correct 253 ms 13560 KB Output is correct
24 Correct 149 ms 13560 KB Output is correct
25 Correct 211 ms 16628 KB Output is correct
26 Correct 78 ms 21368 KB Output is correct
27 Correct 345 ms 21368 KB Output is correct
28 Correct 705 ms 35052 KB Output is correct
29 Correct 728 ms 35052 KB Output is correct
30 Correct 389 ms 35052 KB Output is correct
31 Correct 387 ms 35052 KB Output is correct
32 Correct 545 ms 35052 KB Output is correct
33 Correct 563 ms 35052 KB Output is correct
34 Correct 576 ms 35052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 7 ms 5192 KB Output is correct
4 Correct 6 ms 5208 KB Output is correct
5 Correct 6 ms 5268 KB Output is correct
6 Correct 6 ms 5268 KB Output is correct
7 Correct 6 ms 5268 KB Output is correct
8 Correct 7 ms 5312 KB Output is correct
9 Correct 7 ms 5312 KB Output is correct
10 Correct 6 ms 5312 KB Output is correct
11 Correct 6 ms 5356 KB Output is correct
12 Correct 6 ms 5356 KB Output is correct
13 Correct 6 ms 5356 KB Output is correct
14 Correct 6 ms 5356 KB Output is correct
15 Correct 6 ms 5356 KB Output is correct
16 Correct 5 ms 5356 KB Output is correct
17 Correct 6 ms 5356 KB Output is correct
18 Correct 6 ms 5356 KB Output is correct
19 Correct 6 ms 5356 KB Output is correct
20 Correct 6 ms 5356 KB Output is correct
21 Correct 7 ms 5372 KB Output is correct
22 Correct 7 ms 5372 KB Output is correct
23 Correct 8 ms 5372 KB Output is correct
24 Correct 7 ms 5384 KB Output is correct
25 Correct 8 ms 5500 KB Output is correct
26 Correct 7 ms 5500 KB Output is correct
27 Correct 6 ms 5500 KB Output is correct
28 Correct 8 ms 5500 KB Output is correct
29 Correct 8 ms 5500 KB Output is correct
30 Correct 8 ms 5500 KB Output is correct
31 Correct 8 ms 5500 KB Output is correct
32 Correct 8 ms 5500 KB Output is correct
33 Correct 8 ms 5608 KB Output is correct
34 Correct 7 ms 5608 KB Output is correct
35 Correct 7 ms 5608 KB Output is correct
36 Correct 7 ms 5608 KB Output is correct
37 Correct 7 ms 5608 KB Output is correct
38 Correct 7 ms 5608 KB Output is correct
39 Correct 374 ms 12936 KB Output is correct
40 Correct 385 ms 12948 KB Output is correct
41 Correct 383 ms 13556 KB Output is correct
42 Correct 360 ms 13560 KB Output is correct
43 Correct 253 ms 13560 KB Output is correct
44 Correct 149 ms 13560 KB Output is correct
45 Correct 211 ms 16628 KB Output is correct
46 Correct 78 ms 21368 KB Output is correct
47 Correct 345 ms 21368 KB Output is correct
48 Correct 705 ms 35052 KB Output is correct
49 Correct 728 ms 35052 KB Output is correct
50 Correct 389 ms 35052 KB Output is correct
51 Correct 387 ms 35052 KB Output is correct
52 Correct 545 ms 35052 KB Output is correct
53 Correct 563 ms 35052 KB Output is correct
54 Correct 576 ms 35052 KB Output is correct
55 Correct 40 ms 35052 KB Output is correct
56 Correct 26 ms 35052 KB Output is correct
57 Correct 236 ms 35052 KB Output is correct
58 Correct 69 ms 35052 KB Output is correct
59 Correct 133 ms 35052 KB Output is correct
60 Correct 390 ms 59840 KB Output is correct
61 Correct 399 ms 59840 KB Output is correct
62 Correct 282 ms 59840 KB Output is correct
63 Correct 343 ms 59840 KB Output is correct
64 Correct 2005 ms 59840 KB Output is correct
65 Correct 610 ms 59840 KB Output is correct
66 Correct 694 ms 59840 KB Output is correct
67 Correct 209 ms 59840 KB Output is correct
68 Correct 654 ms 61036 KB Output is correct
69 Correct 641 ms 64244 KB Output is correct
70 Correct 616 ms 67112 KB Output is correct