답안 #223369

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
223369 2020-04-15T08:12:11 Z wendy_virgo 경주 (Race) (IOI11_race) C++14
100 / 100
1056 ms 33528 KB
#include <bits/stdc++.h>

using namespace std;

template<typename TH>
void _dbg(const char* sdbg, TH h)
{
	cerr << sdbg << " = " << h << "\n";
}

template<typename TH, typename... TA>
void _dbg(const char* sdbg, TH h, TA... t)
{
	while (*sdbg != ',') cerr << *sdbg++;
	cerr << " = " << h << ",";
	_dbg(sdbg + 1, t...);
}

#define db(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define chkpt cerr << "--- Checkpoint here ---\n";

const int N=2e5+5,K=1e6+6,INF=1e9,LOG_N=20;

struct Edge{
    int u,v,w;

    int Adj(int x){
        return u^v^x;
    }
};

int n,k;
Edge edgeList[N];
vector<int> g[N];
bool del[N];
int sz[N],ans=INF,f[K];

void DFS_Size(int u,int fa){
    sz[u]=1;
    for(int id:g[u]){
        int v=edgeList[id].Adj(u);
        if(v!=fa&&!del[v]){
            DFS_Size(v,u);
            sz[u]+=sz[v];
        }
    }
}

int FindCentroid(int u,int fa,int curSz){
    int maxSize=0,big=0;
    for(int id:g[u]){
        int v=edgeList[id].Adj(u);
        if(v!=fa&&!del[v]&&sz[v]>maxSize){
            maxSize=sz[v];
            big=v;
        }
    }
    if(maxSize<=curSz/2){
        return u;
    }
    return FindCentroid(big,u,curSz);
}

void DFS_UpdateAns(int u,int fa,int64_t s,int d){
    if(s<=k&&f[k-s]!=INF){
        ans=min(ans,f[k-s]+d);
    }
    for(int id:g[u]){
        int v=edgeList[id].Adj(u),w=edgeList[id].w;
        if(!del[v]&&v!=fa){
            DFS_UpdateAns(v,u,s+w,d+1);
        }
    }
}

void DFS_UpdateF(int u,int fa,int64_t s,int d){
    if(s<=k){
        f[s]=min(f[s],d);
    }
    for(int id:g[u]){
        int v=edgeList[id].Adj(u),w=edgeList[id].w;
        if(!del[v]&&v!=fa){
            DFS_UpdateF(v,u,s+w,d+1);
        }
    }
}

void DFS_Reset(int u,int fa,int64_t s,int d){
    if(s<=k){
        f[s]=INF;
    }
    for(int id:g[u]){
        int v=edgeList[id].Adj(u),w=edgeList[id].w;
        if(!del[v]&&v!=fa){
            DFS_Reset(v,u,s+w,d+1);
        }
    }
}

int BuildCentroidTree(int u){
    DFS_Size(u,-1);
    int cen=FindCentroid(u,-1,sz[u]);
    for(int id:g[cen]){
        int v=edgeList[id].Adj(cen),w=edgeList[id].w;
        if(!del[v]){
            f[0]=0;
            DFS_UpdateAns(v,cen,w,1);
            DFS_UpdateF(v,cen,w,1);
        }
    }
    DFS_Reset(cen,cen,0,0);
    del[cen]=true;
    for(int id:g[cen]){
        int v=edgeList[id].Adj(cen);
        if(!del[v]){
            int tmp=BuildCentroidTree(v);
//            cout<<cen<<' '<<tmp<<'\n';
        }
    }
    return cen;
}

int Solve(){
    for(int i=0;i<K;++i){
        f[i]=INF;
	}
	BuildCentroidTree(1);
	return ans==INF?-1:ans;
}

int best_path(int N,int K,int H[][2],int L[])
{
    n=N;
    k=K;
    for(int i=0;i<=n-2;++i){
        int u=H[i][0]+1,v=H[i][1]+1;
        g[u].push_back(i+1);
        g[v].push_back(i+1);
        edgeList[i+1]={u,v,0};
    }
    for(int i=0;i<=n-2;++i){
        edgeList[i+1].w=L[i];
    }
    return Solve();
}

//int main()
//{
////    freopen("IOI11_race.inp","r",stdin);
//	ios_base::sync_with_stdio(0);
//	cin.tie(0);
//	cin>>n>>k;
//	for(int i=1;i<=n-1;++i){
//        int u,v,w;
//        cin>>u>>v>>w;
//        u++;
//        v++;
////        cout<<u<<' '<<v<<'\n';
//        g[u].push_back(i);
//        g[v].push_back(i);
//        edgeList[i]={u,v,w};
//	}
////	for(int i=1;i<=n-1;++i){
////        cin>>edgeList[i].w;
////	}
//	cout<<Solve();
//    return 0;
//}

Compilation message

race.cpp: In function 'int BuildCentroidTree(int)':
race.cpp:116:17: warning: unused variable 'tmp' [-Wunused-variable]
             int tmp=BuildCentroidTree(v);
                 ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 8960 KB Output is correct
2 Correct 10 ms 9012 KB Output is correct
3 Correct 10 ms 8960 KB Output is correct
4 Correct 10 ms 8960 KB Output is correct
5 Correct 10 ms 8960 KB Output is correct
6 Correct 10 ms 8960 KB Output is correct
7 Correct 12 ms 8960 KB Output is correct
8 Correct 10 ms 8960 KB Output is correct
9 Correct 10 ms 8960 KB Output is correct
10 Correct 10 ms 8960 KB Output is correct
11 Correct 10 ms 8960 KB Output is correct
12 Correct 10 ms 8960 KB Output is correct
13 Correct 12 ms 8960 KB Output is correct
14 Correct 10 ms 8960 KB Output is correct
15 Correct 10 ms 8960 KB Output is correct
16 Correct 10 ms 8960 KB Output is correct
17 Correct 10 ms 8960 KB Output is correct
18 Correct 11 ms 8960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 8960 KB Output is correct
2 Correct 10 ms 9012 KB Output is correct
3 Correct 10 ms 8960 KB Output is correct
4 Correct 10 ms 8960 KB Output is correct
5 Correct 10 ms 8960 KB Output is correct
6 Correct 10 ms 8960 KB Output is correct
7 Correct 12 ms 8960 KB Output is correct
8 Correct 10 ms 8960 KB Output is correct
9 Correct 10 ms 8960 KB Output is correct
10 Correct 10 ms 8960 KB Output is correct
11 Correct 10 ms 8960 KB Output is correct
12 Correct 10 ms 8960 KB Output is correct
13 Correct 12 ms 8960 KB Output is correct
14 Correct 10 ms 8960 KB Output is correct
15 Correct 10 ms 8960 KB Output is correct
16 Correct 10 ms 8960 KB Output is correct
17 Correct 10 ms 8960 KB Output is correct
18 Correct 11 ms 8960 KB Output is correct
19 Correct 9 ms 8960 KB Output is correct
20 Correct 10 ms 8960 KB Output is correct
21 Correct 11 ms 9088 KB Output is correct
22 Correct 11 ms 9088 KB Output is correct
23 Correct 11 ms 9088 KB Output is correct
24 Correct 11 ms 9088 KB Output is correct
25 Correct 11 ms 9088 KB Output is correct
26 Correct 11 ms 9088 KB Output is correct
27 Correct 11 ms 9088 KB Output is correct
28 Correct 11 ms 8960 KB Output is correct
29 Correct 11 ms 9088 KB Output is correct
30 Correct 11 ms 9088 KB Output is correct
31 Correct 11 ms 9088 KB Output is correct
32 Correct 11 ms 9088 KB Output is correct
33 Correct 11 ms 9088 KB Output is correct
34 Correct 11 ms 9088 KB Output is correct
35 Correct 11 ms 9088 KB Output is correct
36 Correct 11 ms 9088 KB Output is correct
37 Correct 10 ms 8960 KB Output is correct
38 Correct 11 ms 8960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 8960 KB Output is correct
2 Correct 10 ms 9012 KB Output is correct
3 Correct 10 ms 8960 KB Output is correct
4 Correct 10 ms 8960 KB Output is correct
5 Correct 10 ms 8960 KB Output is correct
6 Correct 10 ms 8960 KB Output is correct
7 Correct 12 ms 8960 KB Output is correct
8 Correct 10 ms 8960 KB Output is correct
9 Correct 10 ms 8960 KB Output is correct
10 Correct 10 ms 8960 KB Output is correct
11 Correct 10 ms 8960 KB Output is correct
12 Correct 10 ms 8960 KB Output is correct
13 Correct 12 ms 8960 KB Output is correct
14 Correct 10 ms 8960 KB Output is correct
15 Correct 10 ms 8960 KB Output is correct
16 Correct 10 ms 8960 KB Output is correct
17 Correct 10 ms 8960 KB Output is correct
18 Correct 11 ms 8960 KB Output is correct
19 Correct 305 ms 15096 KB Output is correct
20 Correct 308 ms 15224 KB Output is correct
21 Correct 291 ms 15096 KB Output is correct
22 Correct 302 ms 15224 KB Output is correct
23 Correct 169 ms 15096 KB Output is correct
24 Correct 97 ms 15224 KB Output is correct
25 Correct 228 ms 18424 KB Output is correct
26 Correct 145 ms 21240 KB Output is correct
27 Correct 341 ms 21624 KB Output is correct
28 Correct 696 ms 33528 KB Output is correct
29 Correct 669 ms 32632 KB Output is correct
30 Correct 342 ms 21604 KB Output is correct
31 Correct 348 ms 21752 KB Output is correct
32 Correct 472 ms 21624 KB Output is correct
33 Correct 457 ms 21276 KB Output is correct
34 Correct 461 ms 21112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 8960 KB Output is correct
2 Correct 10 ms 9012 KB Output is correct
3 Correct 10 ms 8960 KB Output is correct
4 Correct 10 ms 8960 KB Output is correct
5 Correct 10 ms 8960 KB Output is correct
6 Correct 10 ms 8960 KB Output is correct
7 Correct 12 ms 8960 KB Output is correct
8 Correct 10 ms 8960 KB Output is correct
9 Correct 10 ms 8960 KB Output is correct
10 Correct 10 ms 8960 KB Output is correct
11 Correct 10 ms 8960 KB Output is correct
12 Correct 10 ms 8960 KB Output is correct
13 Correct 12 ms 8960 KB Output is correct
14 Correct 10 ms 8960 KB Output is correct
15 Correct 10 ms 8960 KB Output is correct
16 Correct 10 ms 8960 KB Output is correct
17 Correct 10 ms 8960 KB Output is correct
18 Correct 11 ms 8960 KB Output is correct
19 Correct 9 ms 8960 KB Output is correct
20 Correct 10 ms 8960 KB Output is correct
21 Correct 11 ms 9088 KB Output is correct
22 Correct 11 ms 9088 KB Output is correct
23 Correct 11 ms 9088 KB Output is correct
24 Correct 11 ms 9088 KB Output is correct
25 Correct 11 ms 9088 KB Output is correct
26 Correct 11 ms 9088 KB Output is correct
27 Correct 11 ms 9088 KB Output is correct
28 Correct 11 ms 8960 KB Output is correct
29 Correct 11 ms 9088 KB Output is correct
30 Correct 11 ms 9088 KB Output is correct
31 Correct 11 ms 9088 KB Output is correct
32 Correct 11 ms 9088 KB Output is correct
33 Correct 11 ms 9088 KB Output is correct
34 Correct 11 ms 9088 KB Output is correct
35 Correct 11 ms 9088 KB Output is correct
36 Correct 11 ms 9088 KB Output is correct
37 Correct 10 ms 8960 KB Output is correct
38 Correct 11 ms 8960 KB Output is correct
39 Correct 305 ms 15096 KB Output is correct
40 Correct 308 ms 15224 KB Output is correct
41 Correct 291 ms 15096 KB Output is correct
42 Correct 302 ms 15224 KB Output is correct
43 Correct 169 ms 15096 KB Output is correct
44 Correct 97 ms 15224 KB Output is correct
45 Correct 228 ms 18424 KB Output is correct
46 Correct 145 ms 21240 KB Output is correct
47 Correct 341 ms 21624 KB Output is correct
48 Correct 696 ms 33528 KB Output is correct
49 Correct 669 ms 32632 KB Output is correct
50 Correct 342 ms 21604 KB Output is correct
51 Correct 348 ms 21752 KB Output is correct
52 Correct 472 ms 21624 KB Output is correct
53 Correct 457 ms 21276 KB Output is correct
54 Correct 461 ms 21112 KB Output is correct
55 Correct 26 ms 9728 KB Output is correct
56 Correct 23 ms 9728 KB Output is correct
57 Correct 126 ms 15100 KB Output is correct
58 Correct 55 ms 15472 KB Output is correct
59 Correct 146 ms 21344 KB Output is correct
60 Correct 683 ms 32848 KB Output is correct
61 Correct 363 ms 21756 KB Output is correct
62 Correct 341 ms 21624 KB Output is correct
63 Correct 480 ms 21752 KB Output is correct
64 Correct 1056 ms 21208 KB Output is correct
65 Correct 707 ms 21368 KB Output is correct
66 Correct 725 ms 30456 KB Output is correct
67 Correct 149 ms 21736 KB Output is correct
68 Correct 487 ms 21624 KB Output is correct
69 Correct 507 ms 21880 KB Output is correct
70 Correct 473 ms 21200 KB Output is correct