#include "race.h"
#include <bits/stdc++.h>
#define intt long long
#define FAST_READ ios_base::sync_with_stdio(0);cin.tie(0);
#define SIZE 100001
#define INF INT_MAX
#define F first
#define S second
#define in(a) scanf("%d",&a);
#define outn(a) printf("%d\n",&a);
#define outs(a) printf("%d ",&a);
using namespace std;
int root , n , k , x , y , w , used[SIZE] , dp[SIZE] , co , cent = -1 , we , ans = -1;
vector< pair<int,int> >v[SIZE];
map< int , int > mp;
bool f = true;
void init() {
for (int i = 1;i<=n;i++) {
if (used[i] < 3 && used[i] > 0)used[i] = 0;
}
}
void dfs(int s,int p) {
co++;
used[s]++;
int sz = v[s].size();
for (int i = 0;i<sz;i++) {
int to = v[s][i].first;
int cost = v[s][i].second;
if (to == p)continue;
if (used[to] >= 1)continue;
we += cost;
dfs(to,s);
}
}
void dfs2(int s,int p) {
used[s]++;
dp[s] = 1;
bool f = true;
int sz = v[s].size();
for (int i = 0;i<sz;i++) {
int to = v[s][i].first;
if (used[to] >= 2)continue;
dfs2(to,s);
if (dp[to] > co / 2) f = false;
dp[s] += dp[to];
}
if (f && cent == -1 && co - dp[s] <= co / 2) cent = s;
}
void dfs3(int s,int p,int len,int cst) {
used[s] = 1;
if (cst == k) {
if (ans == -1)ans = len;
else ans = min(ans,len);
return;
}
int sz = v[s].size();
for (int i = 0;i<sz;i++) {
int to = v[s][i].first;
int cost = v[s][i].second;
if (to == p)continue;
if (used[to] >= 1)continue;
dfs3(to,s,len + 1,cst + cost);
}
}
void dfs4(int s,int p,int len,int cst) {
used[s] = 2;
//cout<<s - 1<<" "<<cst<<" "<<k<<endl;
//char aa;
//cin>>aa;
if (cst > k)return;
if (cst == k) {
if (ans == -1)ans = len;
else ans = min(ans,len);
return;
}
else if (mp[k - cst] != 0) {
if (ans == -1)ans = len + mp[k - cst];
else ans = min(ans,len + mp[k - cst]);
}
int sz = v[s].size();
//cout<<sz<<endl;
for (int i = 0;i<sz;i++) {
int to = v[s][i].first;
int cost = v[s][i].second;
if (to == p)continue;
if (used[to] >= 2)continue;
dfs4(to,s,len + 1,cst + cost);
}
if (mp[cst] == 0)mp[cst] = len;
else if (mp[cst] > len)mp[cst] = min(mp[cst],len);
}
int best_path(int N, int K, int h[][2], int l[])
{
k = K;
n = N;
for (int i = 0;i<n - 1;i++) {
x = h[i][0];
y = h[i][1];
x++;
y++;
v[x].push_back({y,l[i]});
v[y].push_back({x,l[i]});
}
while (f) {
f = false;
init();
for (int i = 1;i<=n;i++) {
if (used[i] == 0) {
co = 0; we = 0;
dfs(i , -1);
if (we < k || co == 1)continue;
dp[i] = 0;
cent = -1;
dfs2(i,-1);
if (cent == -1)continue;
f = true;
init();
root = cent;
dfs3(cent,-1,0,0);
mp.clear();
dfs4(cent,-1,0,0);
used[cent] = 3;
}
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2836 KB |
Output is correct |
3 |
Correct |
4 ms |
2948 KB |
Output is correct |
4 |
Correct |
4 ms |
3060 KB |
Output is correct |
5 |
Correct |
4 ms |
3072 KB |
Output is correct |
6 |
Correct |
4 ms |
3128 KB |
Output is correct |
7 |
Correct |
4 ms |
3128 KB |
Output is correct |
8 |
Correct |
4 ms |
3128 KB |
Output is correct |
9 |
Correct |
4 ms |
3208 KB |
Output is correct |
10 |
Correct |
5 ms |
3248 KB |
Output is correct |
11 |
Correct |
4 ms |
3248 KB |
Output is correct |
12 |
Correct |
4 ms |
3260 KB |
Output is correct |
13 |
Correct |
4 ms |
3260 KB |
Output is correct |
14 |
Correct |
4 ms |
3260 KB |
Output is correct |
15 |
Correct |
4 ms |
3260 KB |
Output is correct |
16 |
Correct |
4 ms |
3260 KB |
Output is correct |
17 |
Correct |
4 ms |
3260 KB |
Output is correct |
18 |
Correct |
6 ms |
3260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2836 KB |
Output is correct |
3 |
Correct |
4 ms |
2948 KB |
Output is correct |
4 |
Correct |
4 ms |
3060 KB |
Output is correct |
5 |
Correct |
4 ms |
3072 KB |
Output is correct |
6 |
Correct |
4 ms |
3128 KB |
Output is correct |
7 |
Correct |
4 ms |
3128 KB |
Output is correct |
8 |
Correct |
4 ms |
3128 KB |
Output is correct |
9 |
Correct |
4 ms |
3208 KB |
Output is correct |
10 |
Correct |
5 ms |
3248 KB |
Output is correct |
11 |
Correct |
4 ms |
3248 KB |
Output is correct |
12 |
Correct |
4 ms |
3260 KB |
Output is correct |
13 |
Correct |
4 ms |
3260 KB |
Output is correct |
14 |
Correct |
4 ms |
3260 KB |
Output is correct |
15 |
Correct |
4 ms |
3260 KB |
Output is correct |
16 |
Correct |
4 ms |
3260 KB |
Output is correct |
17 |
Correct |
4 ms |
3260 KB |
Output is correct |
18 |
Correct |
6 ms |
3260 KB |
Output is correct |
19 |
Correct |
4 ms |
3260 KB |
Output is correct |
20 |
Correct |
4 ms |
3260 KB |
Output is correct |
21 |
Correct |
8 ms |
3352 KB |
Output is correct |
22 |
Correct |
6 ms |
3364 KB |
Output is correct |
23 |
Correct |
6 ms |
3396 KB |
Output is correct |
24 |
Correct |
6 ms |
3416 KB |
Output is correct |
25 |
Correct |
9 ms |
3432 KB |
Output is correct |
26 |
Correct |
5 ms |
3464 KB |
Output is correct |
27 |
Correct |
5 ms |
3480 KB |
Output is correct |
28 |
Correct |
7 ms |
3496 KB |
Output is correct |
29 |
Correct |
6 ms |
3512 KB |
Output is correct |
30 |
Correct |
7 ms |
3528 KB |
Output is correct |
31 |
Correct |
6 ms |
3560 KB |
Output is correct |
32 |
Correct |
7 ms |
3576 KB |
Output is correct |
33 |
Correct |
8 ms |
3624 KB |
Output is correct |
34 |
Correct |
5 ms |
3624 KB |
Output is correct |
35 |
Correct |
6 ms |
3640 KB |
Output is correct |
36 |
Correct |
5 ms |
3640 KB |
Output is correct |
37 |
Correct |
5 ms |
3664 KB |
Output is correct |
38 |
Correct |
5 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2836 KB |
Output is correct |
3 |
Correct |
4 ms |
2948 KB |
Output is correct |
4 |
Correct |
4 ms |
3060 KB |
Output is correct |
5 |
Correct |
4 ms |
3072 KB |
Output is correct |
6 |
Correct |
4 ms |
3128 KB |
Output is correct |
7 |
Correct |
4 ms |
3128 KB |
Output is correct |
8 |
Correct |
4 ms |
3128 KB |
Output is correct |
9 |
Correct |
4 ms |
3208 KB |
Output is correct |
10 |
Correct |
5 ms |
3248 KB |
Output is correct |
11 |
Correct |
4 ms |
3248 KB |
Output is correct |
12 |
Correct |
4 ms |
3260 KB |
Output is correct |
13 |
Correct |
4 ms |
3260 KB |
Output is correct |
14 |
Correct |
4 ms |
3260 KB |
Output is correct |
15 |
Correct |
4 ms |
3260 KB |
Output is correct |
16 |
Correct |
4 ms |
3260 KB |
Output is correct |
17 |
Correct |
4 ms |
3260 KB |
Output is correct |
18 |
Correct |
6 ms |
3260 KB |
Output is correct |
19 |
Correct |
848 ms |
10472 KB |
Output is correct |
20 |
Correct |
860 ms |
11840 KB |
Output is correct |
21 |
Correct |
814 ms |
13328 KB |
Output is correct |
22 |
Correct |
875 ms |
14804 KB |
Output is correct |
23 |
Execution timed out |
3046 ms |
16312 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2836 KB |
Output is correct |
3 |
Correct |
4 ms |
2948 KB |
Output is correct |
4 |
Correct |
4 ms |
3060 KB |
Output is correct |
5 |
Correct |
4 ms |
3072 KB |
Output is correct |
6 |
Correct |
4 ms |
3128 KB |
Output is correct |
7 |
Correct |
4 ms |
3128 KB |
Output is correct |
8 |
Correct |
4 ms |
3128 KB |
Output is correct |
9 |
Correct |
4 ms |
3208 KB |
Output is correct |
10 |
Correct |
5 ms |
3248 KB |
Output is correct |
11 |
Correct |
4 ms |
3248 KB |
Output is correct |
12 |
Correct |
4 ms |
3260 KB |
Output is correct |
13 |
Correct |
4 ms |
3260 KB |
Output is correct |
14 |
Correct |
4 ms |
3260 KB |
Output is correct |
15 |
Correct |
4 ms |
3260 KB |
Output is correct |
16 |
Correct |
4 ms |
3260 KB |
Output is correct |
17 |
Correct |
4 ms |
3260 KB |
Output is correct |
18 |
Correct |
6 ms |
3260 KB |
Output is correct |
19 |
Correct |
4 ms |
3260 KB |
Output is correct |
20 |
Correct |
4 ms |
3260 KB |
Output is correct |
21 |
Correct |
8 ms |
3352 KB |
Output is correct |
22 |
Correct |
6 ms |
3364 KB |
Output is correct |
23 |
Correct |
6 ms |
3396 KB |
Output is correct |
24 |
Correct |
6 ms |
3416 KB |
Output is correct |
25 |
Correct |
9 ms |
3432 KB |
Output is correct |
26 |
Correct |
5 ms |
3464 KB |
Output is correct |
27 |
Correct |
5 ms |
3480 KB |
Output is correct |
28 |
Correct |
7 ms |
3496 KB |
Output is correct |
29 |
Correct |
6 ms |
3512 KB |
Output is correct |
30 |
Correct |
7 ms |
3528 KB |
Output is correct |
31 |
Correct |
6 ms |
3560 KB |
Output is correct |
32 |
Correct |
7 ms |
3576 KB |
Output is correct |
33 |
Correct |
8 ms |
3624 KB |
Output is correct |
34 |
Correct |
5 ms |
3624 KB |
Output is correct |
35 |
Correct |
6 ms |
3640 KB |
Output is correct |
36 |
Correct |
5 ms |
3640 KB |
Output is correct |
37 |
Correct |
5 ms |
3664 KB |
Output is correct |
38 |
Correct |
5 ms |
3676 KB |
Output is correct |
39 |
Correct |
848 ms |
10472 KB |
Output is correct |
40 |
Correct |
860 ms |
11840 KB |
Output is correct |
41 |
Correct |
814 ms |
13328 KB |
Output is correct |
42 |
Correct |
875 ms |
14804 KB |
Output is correct |
43 |
Execution timed out |
3046 ms |
16312 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |