답안 #680896

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
680896 2023-01-12T01:36:24 Z 1075508020060209tc 경주 (Race) (IOI11_race) C++14
100 / 100
684 ms 36864 KB
#include<bits/stdc++.h>
#include "race.h"
using namespace std;
//#define int long long
int n;int k;
vector<pair<int,int>>e[200005];
int dp[1000006];
int sz[200005];
int iscen[200005];
int dfssz(int nw,int pa){
int ret=1;
for(int i=0;i<e[nw].size();i++){
    int v=e[nw][i].first;
    if(v==pa||iscen[v]){continue;}
    ret+=dfssz(v,nw);
}
sz[nw]=ret;
return ret;
}

void dfscen(int nw,int pa,int nd,int &pl){
for(int i=0;i<e[nw].size();i++){
    int v=e[nw][i].first;
    if(v==pa||iscen[v]){continue;}
    dfscen(v,nw,nd,pl);
}
if(pl==0&&sz[nw]>nd/2){
    pl=nw;
}
}
int ans=1e8;
void dfs(int nw,int pa,int dis,int d,int ad){
if(dis<=k&&ad==0){
    ans=min(ans,d+dp[k-dis]);
}
if(ad==1){
    if(dis<=k){dp[dis]=min(dp[dis],d);}
}
if(ad==-1){
    if(dis<=k){dp[dis]=1e7;}
}
for(int i=0;i<e[nw].size();i++){
    int v=e[nw][i].first;int w=e[nw][i].second;
    if(iscen[v]||v==pa){continue;}
    dfs(v,nw,dis+w,d+1,ad);
}
}


void solve(int rt){
int cen=0;
dfscen(rt,0,dfssz(rt,0),cen);
dp[0]=0;
for(int i=0;i<e[cen].size();i++){
    int v=e[cen][i].first;int w=e[cen][i].second;
    if(iscen[v]){continue;}
    dfs(v,cen,w,1,0);
    dfs(v,cen,w,1,1);
}
for(int i=0;i<e[cen].size();i++){
    int v=e[cen][i].first;int w=e[cen][i].second;
    if(iscen[v]){continue;}
    dfs(v,cen,w,1,-1);
}
iscen[cen]=1;
for(int i=0;i<e[cen].size();i++){
    int v=e[cen][i].first;int w=e[cen][i].second;
    if(iscen[v]){continue;}
    solve(v);
}



}

int best_path(int N, int K, int H[][2], int L[]){

n=N;k=K;//cin>>n>>k;

for(int i=0;i<=k;i++){
    dp[i]=1e7;
}

for(int i=0;i<=n-2;i++){
    int a;int b;int c;
    //cin>>a>>b>>c;
    a=H[i][0];b=H[i][1];c=L[i];
    a++;b++;
    e[a].push_back({b,c});
    e[b].push_back({a,c});
}
solve(1);
if(ans<=200000){
    //cout<<ans<<endl;
    return ans;
}else{
    //cout<<-1<<endl;
    return -1;
}


}




Compilation message

race.cpp: In function 'int dfssz(int, int)':
race.cpp:12:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | for(int i=0;i<e[nw].size();i++){
      |             ~^~~~~~~~~~~~~
race.cpp: In function 'void dfscen(int, int, int, int&)':
race.cpp:22:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 | for(int i=0;i<e[nw].size();i++){
      |             ~^~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int, int, int, int)':
race.cpp:42:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 | for(int i=0;i<e[nw].size();i++){
      |             ~^~~~~~~~~~~~~
race.cpp: In function 'void solve(int)':
race.cpp:54:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 | for(int i=0;i<e[cen].size();i++){
      |             ~^~~~~~~~~~~~~~
race.cpp:60:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 | for(int i=0;i<e[cen].size();i++){
      |             ~^~~~~~~~~~~~~~
race.cpp:66:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 | for(int i=0;i<e[cen].size();i++){
      |             ~^~~~~~~~~~~~~~
race.cpp:67:31: warning: unused variable 'w' [-Wunused-variable]
   67 |     int v=e[cen][i].first;int w=e[cen][i].second;
      |                               ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5004 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 4924 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 5044 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5004 KB Output is correct
12 Correct 3 ms 5004 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 5004 KB Output is correct
18 Correct 3 ms 5004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5004 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 4924 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 5044 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5004 KB Output is correct
12 Correct 3 ms 5004 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 5004 KB Output is correct
18 Correct 3 ms 5004 KB Output is correct
19 Correct 4 ms 5000 KB Output is correct
20 Correct 3 ms 5004 KB Output is correct
21 Correct 3 ms 5016 KB Output is correct
22 Correct 6 ms 8660 KB Output is correct
23 Correct 5 ms 8024 KB Output is correct
24 Correct 6 ms 8404 KB Output is correct
25 Correct 5 ms 8276 KB Output is correct
26 Correct 6 ms 6420 KB Output is correct
27 Correct 5 ms 8208 KB Output is correct
28 Correct 4 ms 5844 KB Output is correct
29 Correct 5 ms 6296 KB Output is correct
30 Correct 4 ms 6424 KB Output is correct
31 Correct 5 ms 7636 KB Output is correct
32 Correct 5 ms 7828 KB Output is correct
33 Correct 5 ms 8148 KB Output is correct
34 Correct 5 ms 7320 KB Output is correct
35 Correct 7 ms 8240 KB Output is correct
36 Correct 6 ms 8728 KB Output is correct
37 Correct 5 ms 8148 KB Output is correct
38 Correct 4 ms 7064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5004 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 4924 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 5044 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5004 KB Output is correct
12 Correct 3 ms 5004 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 5004 KB Output is correct
18 Correct 3 ms 5004 KB Output is correct
19 Correct 238 ms 11972 KB Output is correct
20 Correct 190 ms 12092 KB Output is correct
21 Correct 187 ms 11996 KB Output is correct
22 Correct 172 ms 12052 KB Output is correct
23 Correct 134 ms 12308 KB Output is correct
24 Correct 79 ms 12248 KB Output is correct
25 Correct 187 ms 15540 KB Output is correct
26 Correct 114 ms 19220 KB Output is correct
27 Correct 224 ms 19444 KB Output is correct
28 Correct 504 ms 33748 KB Output is correct
29 Correct 560 ms 32708 KB Output is correct
30 Correct 227 ms 19276 KB Output is correct
31 Correct 241 ms 19424 KB Output is correct
32 Correct 309 ms 19484 KB Output is correct
33 Correct 395 ms 18288 KB Output is correct
34 Correct 338 ms 19268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5004 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 4924 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 5044 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5004 KB Output is correct
12 Correct 3 ms 5004 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 5004 KB Output is correct
18 Correct 3 ms 5004 KB Output is correct
19 Correct 4 ms 5000 KB Output is correct
20 Correct 3 ms 5004 KB Output is correct
21 Correct 3 ms 5016 KB Output is correct
22 Correct 6 ms 8660 KB Output is correct
23 Correct 5 ms 8024 KB Output is correct
24 Correct 6 ms 8404 KB Output is correct
25 Correct 5 ms 8276 KB Output is correct
26 Correct 6 ms 6420 KB Output is correct
27 Correct 5 ms 8208 KB Output is correct
28 Correct 4 ms 5844 KB Output is correct
29 Correct 5 ms 6296 KB Output is correct
30 Correct 4 ms 6424 KB Output is correct
31 Correct 5 ms 7636 KB Output is correct
32 Correct 5 ms 7828 KB Output is correct
33 Correct 5 ms 8148 KB Output is correct
34 Correct 5 ms 7320 KB Output is correct
35 Correct 7 ms 8240 KB Output is correct
36 Correct 6 ms 8728 KB Output is correct
37 Correct 5 ms 8148 KB Output is correct
38 Correct 4 ms 7064 KB Output is correct
39 Correct 238 ms 11972 KB Output is correct
40 Correct 190 ms 12092 KB Output is correct
41 Correct 187 ms 11996 KB Output is correct
42 Correct 172 ms 12052 KB Output is correct
43 Correct 134 ms 12308 KB Output is correct
44 Correct 79 ms 12248 KB Output is correct
45 Correct 187 ms 15540 KB Output is correct
46 Correct 114 ms 19220 KB Output is correct
47 Correct 224 ms 19444 KB Output is correct
48 Correct 504 ms 33748 KB Output is correct
49 Correct 560 ms 32708 KB Output is correct
50 Correct 227 ms 19276 KB Output is correct
51 Correct 241 ms 19424 KB Output is correct
52 Correct 309 ms 19484 KB Output is correct
53 Correct 395 ms 18288 KB Output is correct
54 Correct 338 ms 19268 KB Output is correct
55 Correct 13 ms 5716 KB Output is correct
56 Correct 14 ms 5588 KB Output is correct
57 Correct 114 ms 12164 KB Output is correct
58 Correct 37 ms 11872 KB Output is correct
59 Correct 113 ms 19996 KB Output is correct
60 Correct 570 ms 36864 KB Output is correct
61 Correct 248 ms 19540 KB Output is correct
62 Correct 210 ms 23356 KB Output is correct
63 Correct 292 ms 23388 KB Output is correct
64 Correct 684 ms 21684 KB Output is correct
65 Correct 510 ms 20272 KB Output is correct
66 Correct 582 ms 34252 KB Output is correct
67 Correct 129 ms 24008 KB Output is correct
68 Correct 333 ms 24084 KB Output is correct
69 Correct 366 ms 24248 KB Output is correct
70 Correct 316 ms 23492 KB Output is correct