답안 #1049337

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1049337 2024-08-08T16:40:11 Z beaconmc 경주 (Race) (IOI11_race) C++14
100 / 100
1534 ms 192536 KB
#include "race.h"
#include <bits/stdc++.h>
 
typedef long long ll;
#define FOR(i,x,y) for(ll i=x; i<y; i++)
#define FORNEG(i,x,y) for(ll i=x; i>y; i--)
 
using namespace std;
 
const ll maxn = 200005;
const ll INF = 1000000000;
 
vector<vector<ll>> edges[maxn];
ll par[maxn];
ll centpar[maxn];
ll depth[maxn];
ll realdepth[maxn];
ll ancc[maxn][20];
bool visited[maxn];
ll sub[maxn];
 
ll anc(ll a, ll x){
    if (a==0) return 0;
    if (x==0) return par[a];
    if (ancc[a][x] != -1) return ancc[a][x];
    return ancc[a][x] = anc(anc(a,x-1),x-1);
}
 
ll lca(ll a, ll b){
    if (depth[a] < depth[b]) swap(a,b);
    FORNEG(i,19,-1) if (depth[anc(a, i)] >= depth[b]) a = anc(a,i);
    if (a==b) return a;
    FORNEG(i,19,-1) if (anc(a,i) != anc(b,i)) a = anc(a,i), b = anc(b,i);
    return par[a];
}
 
void initdfs(ll a,ll p, ll d, ll dd){
    par[a] = p;
    depth[a] = d;
    realdepth[a] = dd;
    for (auto&i : edges[a]){
        if (i[0] != p) initdfs(i[0], a, d+1, dd+i[1]);
    }
}
 
ll centroid(ll root, ll a, ll p){
    for (auto&i : edges[a]){
        if (!visited[i[0]] && i[0] != p && sub[i[0]] > sub[root]/2) return centroid(root, i[0], a);
    }
    return a;
}
void dfs(ll a, ll p){
    sub[a] = 1;
    for (auto&i : edges[a]){
 
        if(i[0] != p && !visited[i[0]]) dfs(i[0], a), sub[a] += sub[i[0]];
    }
}
void build(ll a, ll p){
 
    dfs(a,p);
    ll cent = centroid(a, a, p);
 
 
    centpar[cent] = p;
    visited[cent] = 1;
    for (auto&i : edges[cent]){
        if (!visited[i[0]]) build(i[0], cent);
    }
}
 
vector<array<ll,3>> dists[maxn];
 
int best_path(int N, int K, int H[][2], int L[])
{
    FOR(i,0,maxn) par[i] = -1, centpar[i] = -1, par[i] -1, depth[i] = -1, realdepth[i] = -1,visited[i] = 0,dists[i].clear(),edges[i].clear();
    FOR(i,0,maxn)FOR(j,0,20) ancc[i][j] = -1;
    
    
    FOR(i,0,N-1){
 
        edges[H[i][0]].push_back({H[i][1], L[i]});
        edges[H[i][1]].push_back({H[i][0], L[i]});
    }
 
    
    initdfs(0,-1,0,0);
    dfs(0,-1);
 
    build(0,-1);
 
 
 
    
 
    FOR(i,0,N){
        ll temp = centpar[i];
        ll prev = i;
        ll cnt = 0;
        while (temp != -1){
            cnt++;
 
            ll idk = lca(temp,i);
            ll cur = depth[temp] + depth[i] - depth[idk]*2;
 
           
            ll dist = realdepth[temp] + realdepth[i] - realdepth[idk]*2;
 
            dists[temp].push_back({prev, dist, cur });
            
            prev = temp;
            temp = centpar[temp];
        }
    }
    ll ans = INF;
    ll cnt = 0;
    FOR(i,0,N){

        sort(dists[i].begin(), dists[i].end());
        unordered_map<ll, ll> sus;
        sus[0] = 0;
        vector<array<ll, 3>> cache;
        if (dists[i].size()) cache.push_back(dists[i][0]);

        FOR(j,1,dists[i].size()){
            if (dists[i][j][0] != dists[i][j-1][0]){
                for (auto&k : cache){
                    if (sus.count(k[1])) sus[k[1]] = min(sus[k[1]], k[2]);
                    else sus[k[1]] = k[2];
                }
                cache.clear();
            }
            ll req = K - dists[i][j][1];
            if (sus.count(req)) ans = min(ans, sus[req] + dists[i][j][2]);
            cache.push_back(dists[i][j]);
        }
    }
 
    if (ans >= INF) return -1;
    else return ans;
 
}
 
 
 
 

Compilation message

race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:76:56: warning: right operand of comma operator has no effect [-Wunused-value]
   76 |     FOR(i,0,maxn) par[i] = -1, centpar[i] = -1, par[i] -1, depth[i] = -1, realdepth[i] = -1,visited[i] = 0,dists[i].clear(),edges[i].clear();
      |                                                 ~~~~~~~^~
race.cpp:5:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define FOR(i,x,y) for(ll i=x; i<y; i++)
......
  125 |         FOR(j,1,dists[i].size()){
      |             ~~~~~~~~~~~~~~~~~~~  
race.cpp:125:9: note: in expansion of macro 'FOR'
  125 |         FOR(j,1,dists[i].size()){
      |         ^~~
race.cpp:116:8: warning: unused variable 'cnt' [-Wunused-variable]
  116 |     ll cnt = 0;
      |        ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 52828 KB Output is correct
2 Correct 6 ms 52792 KB Output is correct
3 Correct 6 ms 52828 KB Output is correct
4 Correct 6 ms 52828 KB Output is correct
5 Correct 6 ms 52828 KB Output is correct
6 Correct 6 ms 52824 KB Output is correct
7 Correct 8 ms 52852 KB Output is correct
8 Correct 6 ms 52824 KB Output is correct
9 Correct 6 ms 52828 KB Output is correct
10 Correct 6 ms 52828 KB Output is correct
11 Correct 6 ms 52828 KB Output is correct
12 Correct 6 ms 52828 KB Output is correct
13 Correct 6 ms 52740 KB Output is correct
14 Correct 6 ms 52772 KB Output is correct
15 Correct 6 ms 52828 KB Output is correct
16 Correct 6 ms 52824 KB Output is correct
17 Correct 6 ms 52828 KB Output is correct
18 Correct 6 ms 52828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 52828 KB Output is correct
2 Correct 6 ms 52792 KB Output is correct
3 Correct 6 ms 52828 KB Output is correct
4 Correct 6 ms 52828 KB Output is correct
5 Correct 6 ms 52828 KB Output is correct
6 Correct 6 ms 52824 KB Output is correct
7 Correct 8 ms 52852 KB Output is correct
8 Correct 6 ms 52824 KB Output is correct
9 Correct 6 ms 52828 KB Output is correct
10 Correct 6 ms 52828 KB Output is correct
11 Correct 6 ms 52828 KB Output is correct
12 Correct 6 ms 52828 KB Output is correct
13 Correct 6 ms 52740 KB Output is correct
14 Correct 6 ms 52772 KB Output is correct
15 Correct 6 ms 52828 KB Output is correct
16 Correct 6 ms 52824 KB Output is correct
17 Correct 6 ms 52828 KB Output is correct
18 Correct 6 ms 52828 KB Output is correct
19 Correct 7 ms 52572 KB Output is correct
20 Correct 6 ms 52828 KB Output is correct
21 Correct 7 ms 53044 KB Output is correct
22 Correct 8 ms 53008 KB Output is correct
23 Correct 8 ms 53084 KB Output is correct
24 Correct 8 ms 53084 KB Output is correct
25 Correct 7 ms 53084 KB Output is correct
26 Correct 7 ms 53004 KB Output is correct
27 Correct 7 ms 53084 KB Output is correct
28 Correct 7 ms 52952 KB Output is correct
29 Correct 8 ms 53084 KB Output is correct
30 Correct 8 ms 53084 KB Output is correct
31 Correct 7 ms 52984 KB Output is correct
32 Correct 7 ms 53084 KB Output is correct
33 Correct 9 ms 53084 KB Output is correct
34 Correct 7 ms 53020 KB Output is correct
35 Correct 7 ms 53008 KB Output is correct
36 Correct 7 ms 53016 KB Output is correct
37 Correct 9 ms 53084 KB Output is correct
38 Correct 7 ms 53084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 52828 KB Output is correct
2 Correct 6 ms 52792 KB Output is correct
3 Correct 6 ms 52828 KB Output is correct
4 Correct 6 ms 52828 KB Output is correct
5 Correct 6 ms 52828 KB Output is correct
6 Correct 6 ms 52824 KB Output is correct
7 Correct 8 ms 52852 KB Output is correct
8 Correct 6 ms 52824 KB Output is correct
9 Correct 6 ms 52828 KB Output is correct
10 Correct 6 ms 52828 KB Output is correct
11 Correct 6 ms 52828 KB Output is correct
12 Correct 6 ms 52828 KB Output is correct
13 Correct 6 ms 52740 KB Output is correct
14 Correct 6 ms 52772 KB Output is correct
15 Correct 6 ms 52828 KB Output is correct
16 Correct 6 ms 52824 KB Output is correct
17 Correct 6 ms 52828 KB Output is correct
18 Correct 6 ms 52828 KB Output is correct
19 Correct 334 ms 98960 KB Output is correct
20 Correct 329 ms 98372 KB Output is correct
21 Correct 302 ms 97788 KB Output is correct
22 Correct 288 ms 93976 KB Output is correct
23 Correct 349 ms 105880 KB Output is correct
24 Correct 167 ms 85380 KB Output is correct
25 Correct 562 ms 116904 KB Output is correct
26 Correct 326 ms 118536 KB Output is correct
27 Correct 344 ms 113652 KB Output is correct
28 Correct 1504 ms 192536 KB Output is correct
29 Correct 1534 ms 191396 KB Output is correct
30 Correct 328 ms 114420 KB Output is correct
31 Correct 344 ms 113032 KB Output is correct
32 Correct 512 ms 113648 KB Output is correct
33 Correct 1050 ms 162472 KB Output is correct
34 Correct 1147 ms 162800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 52828 KB Output is correct
2 Correct 6 ms 52792 KB Output is correct
3 Correct 6 ms 52828 KB Output is correct
4 Correct 6 ms 52828 KB Output is correct
5 Correct 6 ms 52828 KB Output is correct
6 Correct 6 ms 52824 KB Output is correct
7 Correct 8 ms 52852 KB Output is correct
8 Correct 6 ms 52824 KB Output is correct
9 Correct 6 ms 52828 KB Output is correct
10 Correct 6 ms 52828 KB Output is correct
11 Correct 6 ms 52828 KB Output is correct
12 Correct 6 ms 52828 KB Output is correct
13 Correct 6 ms 52740 KB Output is correct
14 Correct 6 ms 52772 KB Output is correct
15 Correct 6 ms 52828 KB Output is correct
16 Correct 6 ms 52824 KB Output is correct
17 Correct 6 ms 52828 KB Output is correct
18 Correct 6 ms 52828 KB Output is correct
19 Correct 7 ms 52572 KB Output is correct
20 Correct 6 ms 52828 KB Output is correct
21 Correct 7 ms 53044 KB Output is correct
22 Correct 8 ms 53008 KB Output is correct
23 Correct 8 ms 53084 KB Output is correct
24 Correct 8 ms 53084 KB Output is correct
25 Correct 7 ms 53084 KB Output is correct
26 Correct 7 ms 53004 KB Output is correct
27 Correct 7 ms 53084 KB Output is correct
28 Correct 7 ms 52952 KB Output is correct
29 Correct 8 ms 53084 KB Output is correct
30 Correct 8 ms 53084 KB Output is correct
31 Correct 7 ms 52984 KB Output is correct
32 Correct 7 ms 53084 KB Output is correct
33 Correct 9 ms 53084 KB Output is correct
34 Correct 7 ms 53020 KB Output is correct
35 Correct 7 ms 53008 KB Output is correct
36 Correct 7 ms 53016 KB Output is correct
37 Correct 9 ms 53084 KB Output is correct
38 Correct 7 ms 53084 KB Output is correct
39 Correct 334 ms 98960 KB Output is correct
40 Correct 329 ms 98372 KB Output is correct
41 Correct 302 ms 97788 KB Output is correct
42 Correct 288 ms 93976 KB Output is correct
43 Correct 349 ms 105880 KB Output is correct
44 Correct 167 ms 85380 KB Output is correct
45 Correct 562 ms 116904 KB Output is correct
46 Correct 326 ms 118536 KB Output is correct
47 Correct 344 ms 113652 KB Output is correct
48 Correct 1504 ms 192536 KB Output is correct
49 Correct 1534 ms 191396 KB Output is correct
50 Correct 328 ms 114420 KB Output is correct
51 Correct 344 ms 113032 KB Output is correct
52 Correct 512 ms 113648 KB Output is correct
53 Correct 1050 ms 162472 KB Output is correct
54 Correct 1147 ms 162800 KB Output is correct
55 Correct 27 ms 56472 KB Output is correct
56 Correct 26 ms 56500 KB Output is correct
57 Correct 237 ms 105100 KB Output is correct
58 Correct 65 ms 72668 KB Output is correct
59 Correct 341 ms 119356 KB Output is correct
60 Correct 1513 ms 192108 KB Output is correct
61 Correct 378 ms 114424 KB Output is correct
62 Correct 347 ms 113948 KB Output is correct
63 Correct 511 ms 113392 KB Output is correct
64 Correct 824 ms 147704 KB Output is correct
65 Correct 697 ms 140788 KB Output is correct
66 Correct 1472 ms 189444 KB Output is correct
67 Correct 131 ms 90104 KB Output is correct
68 Correct 452 ms 116032 KB Output is correct
69 Correct 478 ms 116304 KB Output is correct
70 Correct 450 ms 113648 KB Output is correct