#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
// C++
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include "race.h"
using namespace std;
vector<vector< pair<long long,long long> > > g;// {node,dist}
vector<long long> off,offset_node;
void dfs(long long node,long long par,vector<map<long long,long long> > &hash,long long k,long long &ans)
{
for(auto child:g[node])
{
if(child.first==par) continue;
dfs(child.first,node,hash,k,ans);
// }
// for(auto child:g[node])
// {
if(child.first == par) continue;
long long off_node = off[node],off_child=off[child.first],edge=child.second,off_used=off_child;
long long offset_node_used = offset_node[child.first],ll=1;
if(hash[node].size()<hash[child.first].size())
{
// cout<<node<<"*\n";
off_used = off[node];
offset_node_used = offset_node[node];
off[node] = off_child+edge;
offset_node[node] = ll+ offset_node[child.first];
edge=0;
ll=0;
swap(hash[node],hash[child.first]);
}
for(auto it=hash[child.first].begin();it!=hash[child.first].end();it++)
{
long long dist = it->first + off_used + edge;
long long mn_nodes = ll+it->second+offset_node_used;
if(hash[node].find(k-dist-off[node])!=hash[node].end())
ans=min(ans,mn_nodes+hash[node][k-dist-off[node]] + offset_node[node]);
}
for(auto it=hash[child.first].begin();it!=hash[child.first].end();it++)
{
long long dist = it->first + off_used + edge;
long long mn_nodes = ll+it->second+offset_node_used;
// if(hash[node].find(k-dist-off[node])!=hash[node].end())
// ans=min(ans,mn_nodes+hash[node][k-dist-off[node]] + offset_node[node]);
// else if(k==dist+off[node]) ans =min(ans,mn_nodes+offset_node[node]);
if(hash[node].find(dist-off[node])!=hash[node].end())
{
long long v1 = hash[node][dist-off[node]]+offset_node[node];
hash[node][dist-off[node]] = min(v1,mn_nodes)-offset_node[node];
}
else hash[node][dist-off[node]] = mn_nodes-offset_node[node];
}
}
}
// void dfs2(int node,int par,vector<map<long long,long long> > &hash){
// for(auto child:g[node]){
// if(child.first==par) continue;
// if(child.second==0) hash[node][0]=1;
// dfs2(child.first,node,hash);
// }
// }
int best_path(int N, int K, int H[][2], int L[])
{
long long n,k,a,b,wt;
long long ans=1e18;
vector<map<long long,long long> > hash;
n=N;
k=K;
g.resize(n+10);
off.assign(n+10,0);
offset_node.assign(n+10,0);
hash.resize(n+10);
for(long long i=0;i<=n;i++) hash[i][0]=0;
for(long long i=0;i<n-1;i++){
a=H[i][0];
b=H[i][1];
wt=L[i];
g[a].push_back(make_pair(b,wt));
g[b].push_back(make_pair(a,wt));
}
// dfs2(0,-1,hash);
dfs(0,-1,hash,k,ans);
if(ans==1e18) ans=-1;
int ans2=ans;
return ans2;
}
Compilation message
race.cpp: In function 'void dfs(long long int, long long int, std::vector<std::map<long long int, long long int> >&, long long int, long long int&)':
race.cpp:85:19: warning: unused variable 'off_node' [-Wunused-variable]
85 | long long off_node = off[node],off_child=off[child.first],edge=child.second,off_used=off_child;
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2748 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2664 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
102 ms |
34060 KB |
Output is correct |
20 |
Correct |
108 ms |
33876 KB |
Output is correct |
21 |
Correct |
102 ms |
33616 KB |
Output is correct |
22 |
Correct |
102 ms |
33264 KB |
Output is correct |
23 |
Correct |
145 ms |
46160 KB |
Output is correct |
24 |
Correct |
111 ms |
36172 KB |
Output is correct |
25 |
Correct |
92 ms |
35668 KB |
Output is correct |
26 |
Correct |
52 ms |
44636 KB |
Output is correct |
27 |
Correct |
147 ms |
51548 KB |
Output is correct |
28 |
Correct |
253 ms |
97168 KB |
Output is correct |
29 |
Correct |
248 ms |
94816 KB |
Output is correct |
30 |
Correct |
149 ms |
51536 KB |
Output is correct |
31 |
Correct |
148 ms |
51704 KB |
Output is correct |
32 |
Correct |
211 ms |
51520 KB |
Output is correct |
33 |
Correct |
226 ms |
56652 KB |
Output is correct |
34 |
Correct |
327 ms |
88400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2748 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2664 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 KB |
Output is correct |
39 |
Correct |
102 ms |
34060 KB |
Output is correct |
40 |
Correct |
108 ms |
33876 KB |
Output is correct |
41 |
Correct |
102 ms |
33616 KB |
Output is correct |
42 |
Correct |
102 ms |
33264 KB |
Output is correct |
43 |
Correct |
145 ms |
46160 KB |
Output is correct |
44 |
Correct |
111 ms |
36172 KB |
Output is correct |
45 |
Correct |
92 ms |
35668 KB |
Output is correct |
46 |
Correct |
52 ms |
44636 KB |
Output is correct |
47 |
Correct |
147 ms |
51548 KB |
Output is correct |
48 |
Correct |
253 ms |
97168 KB |
Output is correct |
49 |
Correct |
248 ms |
94816 KB |
Output is correct |
50 |
Correct |
149 ms |
51536 KB |
Output is correct |
51 |
Correct |
148 ms |
51704 KB |
Output is correct |
52 |
Correct |
211 ms |
51520 KB |
Output is correct |
53 |
Correct |
226 ms |
56652 KB |
Output is correct |
54 |
Correct |
327 ms |
88400 KB |
Output is correct |
55 |
Correct |
12 ms |
6236 KB |
Output is correct |
56 |
Correct |
6 ms |
4956 KB |
Output is correct |
57 |
Correct |
57 ms |
31468 KB |
Output is correct |
58 |
Correct |
43 ms |
24268 KB |
Output is correct |
59 |
Correct |
79 ms |
50672 KB |
Output is correct |
60 |
Correct |
240 ms |
95624 KB |
Output is correct |
61 |
Correct |
176 ms |
54864 KB |
Output is correct |
62 |
Correct |
146 ms |
51536 KB |
Output is correct |
63 |
Correct |
206 ms |
51792 KB |
Output is correct |
64 |
Correct |
412 ms |
105928 KB |
Output is correct |
65 |
Correct |
389 ms |
103672 KB |
Output is correct |
66 |
Correct |
260 ms |
89936 KB |
Output is correct |
67 |
Correct |
152 ms |
44128 KB |
Output is correct |
68 |
Correct |
281 ms |
67080 KB |
Output is correct |
69 |
Correct |
321 ms |
72008 KB |
Output is correct |
70 |
Correct |
288 ms |
64476 KB |
Output is correct |