#include "race.h"
#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define all(a) a.begin(), a.end()
#define v vector
typedef long long ll;
typedef pair<int, int > pii;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void minn(T &a, T b) { a = min(a, b); }
template<class T> void maxx(T &a, T b) { a = max(a, b); }
// g++ -std=c++11 -Wall -Wextra -O2 -pipe grader.cpp cluedo.cpp -o cluedo && cluedo < grader.in.1
set<pii> adj[200000]; // node, length
int subt_sz[200000];
int dfs_sz(int x, int p) {
subt_sz[x] = 1;
for (auto c : adj[x]) if (c.first != p) subt_sz[x] += dfs_sz(c.first, x);
return subt_sz[x];
}
int getCentroid(int x, int p) {
dfs_sz(x, p);
int maxsize = subt_sz[x]/2;
while (true) {
int next = -1;
for (auto c : adj[x]) if (c.first!=p && subt_sz[c.first] > maxsize) {
next = c.first;
break;
}
if (next == -1) return x;
p = x, x = next;
}
assert(0);
}
int ans = INT_MAX, LN; // min highways, with len = LN
void dfs_subt(int x, int p, int len, int highways, map<int,int> &table){
if(table.find(len) == table.end()) table[len] = highways;
else minn(table[len], highways);
for(auto c : adj[x]) if(c.first != p && len+c.second <= LN)
dfs_subt(c.first, x, len+c.second, highways+1, table);
}
void dfsSolve(int root) {
//cout << "initial root = " << root << endl;
root = getCentroid(root, -1);
//cout << "centroid = " << root << endl;
map<int, multiset<int>> table; // <len, possible highways>
v<map<int,int>> subdata; // {len, highways}
for(auto subt : adj[root]){
subdata.push_back(map<int,int>());
dfs_subt(subt.first, root, subt.second, 1, subdata.back());
//cout << "subt " << subt.first << " data " << endl;
// for(auto pp : subdata.back())
//cout << "len " << pp.first << "; highways " << pp.second << endl;
for(auto pp : subdata.back())
table[pp.first].insert(pp.second);
}
for(auto &subtable : subdata){
for(auto pp : subtable)
table[pp.first].erase(table[pp.first].find(pp.second));
for(auto pp : subtable){
if(pp.first == LN){
minn(ans, pp.second);
//cout << "ans (single) " << pp.second << endl;
}
else
if(table.find(LN-pp.first) != table.end() && table[LN-pp.first].size() > 0){
minn(ans, pp.second + *table[LN-pp.first].begin());
//cout << "ans " << (pp.second) << " + " << *table[LN-pp.first].begin() << endl;
}
}
for(auto pp : subtable)
table[pp.first].insert(pp.second);
}
//cout << endl << endl;
for (auto subt : adj[root]) {
assert(adj[subt.first].find({root, subt.second}) != adj[subt.first].end());
adj[subt.first].erase({root, subt.second});
dfsSolve(subt.first);
}
}
void add_edge(int a, int b, int c){
adj[a].insert({b,c});
adj[b].insert({a,c});
}
int best_path(int N, int K, int H[][2], int L[]){
FOR(i,0,N-1)
add_edge(H[i][0], H[i][1], L[i]);
LN = K;
dfsSolve(0);
return (ans == INT_MAX ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
11 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
12 ms |
9720 KB |
Output is correct |
5 |
Correct |
11 ms |
9756 KB |
Output is correct |
6 |
Correct |
11 ms |
9720 KB |
Output is correct |
7 |
Correct |
11 ms |
9720 KB |
Output is correct |
8 |
Correct |
11 ms |
9720 KB |
Output is correct |
9 |
Correct |
11 ms |
9720 KB |
Output is correct |
10 |
Correct |
11 ms |
9720 KB |
Output is correct |
11 |
Correct |
11 ms |
9724 KB |
Output is correct |
12 |
Correct |
11 ms |
9720 KB |
Output is correct |
13 |
Correct |
11 ms |
9848 KB |
Output is correct |
14 |
Correct |
11 ms |
9720 KB |
Output is correct |
15 |
Correct |
11 ms |
9720 KB |
Output is correct |
16 |
Correct |
11 ms |
9720 KB |
Output is correct |
17 |
Correct |
11 ms |
9848 KB |
Output is correct |
18 |
Correct |
11 ms |
9720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
11 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
12 ms |
9720 KB |
Output is correct |
5 |
Correct |
11 ms |
9756 KB |
Output is correct |
6 |
Correct |
11 ms |
9720 KB |
Output is correct |
7 |
Correct |
11 ms |
9720 KB |
Output is correct |
8 |
Correct |
11 ms |
9720 KB |
Output is correct |
9 |
Correct |
11 ms |
9720 KB |
Output is correct |
10 |
Correct |
11 ms |
9720 KB |
Output is correct |
11 |
Correct |
11 ms |
9724 KB |
Output is correct |
12 |
Correct |
11 ms |
9720 KB |
Output is correct |
13 |
Correct |
11 ms |
9848 KB |
Output is correct |
14 |
Correct |
11 ms |
9720 KB |
Output is correct |
15 |
Correct |
11 ms |
9720 KB |
Output is correct |
16 |
Correct |
11 ms |
9720 KB |
Output is correct |
17 |
Correct |
11 ms |
9848 KB |
Output is correct |
18 |
Correct |
11 ms |
9720 KB |
Output is correct |
19 |
Correct |
11 ms |
9848 KB |
Output is correct |
20 |
Correct |
11 ms |
9720 KB |
Output is correct |
21 |
Correct |
14 ms |
9976 KB |
Output is correct |
22 |
Correct |
12 ms |
9848 KB |
Output is correct |
23 |
Correct |
13 ms |
9848 KB |
Output is correct |
24 |
Correct |
15 ms |
9848 KB |
Output is correct |
25 |
Correct |
15 ms |
10232 KB |
Output is correct |
26 |
Correct |
12 ms |
9848 KB |
Output is correct |
27 |
Correct |
12 ms |
9848 KB |
Output is correct |
28 |
Correct |
14 ms |
10104 KB |
Output is correct |
29 |
Correct |
14 ms |
10104 KB |
Output is correct |
30 |
Correct |
15 ms |
10232 KB |
Output is correct |
31 |
Correct |
15 ms |
10104 KB |
Output is correct |
32 |
Correct |
15 ms |
10232 KB |
Output is correct |
33 |
Correct |
15 ms |
10232 KB |
Output is correct |
34 |
Correct |
13 ms |
9848 KB |
Output is correct |
35 |
Correct |
14 ms |
9976 KB |
Output is correct |
36 |
Correct |
14 ms |
9848 KB |
Output is correct |
37 |
Correct |
15 ms |
10104 KB |
Output is correct |
38 |
Correct |
16 ms |
10104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
11 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
12 ms |
9720 KB |
Output is correct |
5 |
Correct |
11 ms |
9756 KB |
Output is correct |
6 |
Correct |
11 ms |
9720 KB |
Output is correct |
7 |
Correct |
11 ms |
9720 KB |
Output is correct |
8 |
Correct |
11 ms |
9720 KB |
Output is correct |
9 |
Correct |
11 ms |
9720 KB |
Output is correct |
10 |
Correct |
11 ms |
9720 KB |
Output is correct |
11 |
Correct |
11 ms |
9724 KB |
Output is correct |
12 |
Correct |
11 ms |
9720 KB |
Output is correct |
13 |
Correct |
11 ms |
9848 KB |
Output is correct |
14 |
Correct |
11 ms |
9720 KB |
Output is correct |
15 |
Correct |
11 ms |
9720 KB |
Output is correct |
16 |
Correct |
11 ms |
9720 KB |
Output is correct |
17 |
Correct |
11 ms |
9848 KB |
Output is correct |
18 |
Correct |
11 ms |
9720 KB |
Output is correct |
19 |
Correct |
497 ms |
22264 KB |
Output is correct |
20 |
Correct |
495 ms |
22332 KB |
Output is correct |
21 |
Correct |
480 ms |
22400 KB |
Output is correct |
22 |
Correct |
519 ms |
22392 KB |
Output is correct |
23 |
Correct |
230 ms |
22264 KB |
Output is correct |
24 |
Correct |
210 ms |
22264 KB |
Output is correct |
25 |
Correct |
310 ms |
24824 KB |
Output is correct |
26 |
Correct |
169 ms |
26744 KB |
Output is correct |
27 |
Correct |
629 ms |
35064 KB |
Output is correct |
28 |
Correct |
591 ms |
44024 KB |
Output is correct |
29 |
Correct |
584 ms |
43384 KB |
Output is correct |
30 |
Correct |
640 ms |
35064 KB |
Output is correct |
31 |
Correct |
638 ms |
35064 KB |
Output is correct |
32 |
Correct |
653 ms |
34936 KB |
Output is correct |
33 |
Correct |
921 ms |
34792 KB |
Output is correct |
34 |
Correct |
431 ms |
35476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
11 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
12 ms |
9720 KB |
Output is correct |
5 |
Correct |
11 ms |
9756 KB |
Output is correct |
6 |
Correct |
11 ms |
9720 KB |
Output is correct |
7 |
Correct |
11 ms |
9720 KB |
Output is correct |
8 |
Correct |
11 ms |
9720 KB |
Output is correct |
9 |
Correct |
11 ms |
9720 KB |
Output is correct |
10 |
Correct |
11 ms |
9720 KB |
Output is correct |
11 |
Correct |
11 ms |
9724 KB |
Output is correct |
12 |
Correct |
11 ms |
9720 KB |
Output is correct |
13 |
Correct |
11 ms |
9848 KB |
Output is correct |
14 |
Correct |
11 ms |
9720 KB |
Output is correct |
15 |
Correct |
11 ms |
9720 KB |
Output is correct |
16 |
Correct |
11 ms |
9720 KB |
Output is correct |
17 |
Correct |
11 ms |
9848 KB |
Output is correct |
18 |
Correct |
11 ms |
9720 KB |
Output is correct |
19 |
Correct |
11 ms |
9848 KB |
Output is correct |
20 |
Correct |
11 ms |
9720 KB |
Output is correct |
21 |
Correct |
14 ms |
9976 KB |
Output is correct |
22 |
Correct |
12 ms |
9848 KB |
Output is correct |
23 |
Correct |
13 ms |
9848 KB |
Output is correct |
24 |
Correct |
15 ms |
9848 KB |
Output is correct |
25 |
Correct |
15 ms |
10232 KB |
Output is correct |
26 |
Correct |
12 ms |
9848 KB |
Output is correct |
27 |
Correct |
12 ms |
9848 KB |
Output is correct |
28 |
Correct |
14 ms |
10104 KB |
Output is correct |
29 |
Correct |
14 ms |
10104 KB |
Output is correct |
30 |
Correct |
15 ms |
10232 KB |
Output is correct |
31 |
Correct |
15 ms |
10104 KB |
Output is correct |
32 |
Correct |
15 ms |
10232 KB |
Output is correct |
33 |
Correct |
15 ms |
10232 KB |
Output is correct |
34 |
Correct |
13 ms |
9848 KB |
Output is correct |
35 |
Correct |
14 ms |
9976 KB |
Output is correct |
36 |
Correct |
14 ms |
9848 KB |
Output is correct |
37 |
Correct |
15 ms |
10104 KB |
Output is correct |
38 |
Correct |
16 ms |
10104 KB |
Output is correct |
39 |
Correct |
497 ms |
22264 KB |
Output is correct |
40 |
Correct |
495 ms |
22332 KB |
Output is correct |
41 |
Correct |
480 ms |
22400 KB |
Output is correct |
42 |
Correct |
519 ms |
22392 KB |
Output is correct |
43 |
Correct |
230 ms |
22264 KB |
Output is correct |
44 |
Correct |
210 ms |
22264 KB |
Output is correct |
45 |
Correct |
310 ms |
24824 KB |
Output is correct |
46 |
Correct |
169 ms |
26744 KB |
Output is correct |
47 |
Correct |
629 ms |
35064 KB |
Output is correct |
48 |
Correct |
591 ms |
44024 KB |
Output is correct |
49 |
Correct |
584 ms |
43384 KB |
Output is correct |
50 |
Correct |
640 ms |
35064 KB |
Output is correct |
51 |
Correct |
638 ms |
35064 KB |
Output is correct |
52 |
Correct |
653 ms |
34936 KB |
Output is correct |
53 |
Correct |
921 ms |
34792 KB |
Output is correct |
54 |
Correct |
431 ms |
35476 KB |
Output is correct |
55 |
Correct |
61 ms |
12408 KB |
Output is correct |
56 |
Correct |
37 ms |
11000 KB |
Output is correct |
57 |
Correct |
278 ms |
22136 KB |
Output is correct |
58 |
Correct |
223 ms |
35860 KB |
Output is correct |
59 |
Correct |
1042 ms |
59684 KB |
Output is correct |
60 |
Correct |
2895 ms |
115544 KB |
Output is correct |
61 |
Correct |
927 ms |
38264 KB |
Output is correct |
62 |
Correct |
1148 ms |
44536 KB |
Output is correct |
63 |
Correct |
1274 ms |
44664 KB |
Output is correct |
64 |
Correct |
2744 ms |
80820 KB |
Output is correct |
65 |
Correct |
539 ms |
35576 KB |
Output is correct |
66 |
Correct |
1923 ms |
47876 KB |
Output is correct |
67 |
Correct |
795 ms |
63240 KB |
Output is correct |
68 |
Correct |
1428 ms |
60108 KB |
Output is correct |
69 |
Correct |
1389 ms |
64452 KB |
Output is correct |
70 |
Correct |
1319 ms |
62712 KB |
Output is correct |