#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) (__builtin_popcountll(a))
#define bithigh(a) (63-__builtin_clzll(a))
#define lg bithigh
#define highpow(a) (1LL << (ll)lg(a))
using namespace std;
const ll LINF = 2e18;
const int mxN = 1e6+10, INF = 2e9;
ll n, x, y, k, a[mxN];
vector<array<ll, 2> > g[mxN];
bool vis[2][mxN];
int BFS(){
priority_queue<array<ll, 3>,
vector<array<ll, 3> >,
greater<array<ll, 3> > > pq;
pq.push({0, x, 0});
pq.push({0, y, 1});
vis[0][x] = vis[1][y] = 1;
int cnt = 0;
ll sum = 0;
while (pq.size()){
auto [d, s, t] = pq.top(); pq.pop();
if (sum + d > k) break;
sum += d;
a[s] = d;
cnt++;
for (auto [u, w] : g[s]){
if (!vis[t][u]){
pq.push({max(0LL, d + w - a[u]), u, t});
vis[t][u] = 1;
}
}
}
return cnt;
}
int Solve1(){ return BFS(); }
ll idx[mxN], dist[2][mxN], pre[3][mxN];
int Next(int s){
for (auto [u, w] : g[s])
if (!~idx[u]) return u;
return -1;
}
void dfs(int s, int p, ll d, int t){
dist[t][s] = d;
for (auto [u, w] : g[s])
if (u^p) dfs(u, s, d+w, t);
}
int Solve2(){
int s = 0;
for (; s < n; s++)
if (g[s].size() == 1)
break;
for (int i = 0; i < n; i++)
idx[i] = -1;
int i = 0;
while (~s){
idx[s] = i;
s = Next(s);
i++;
}
if (idx[x] > idx[y]) swap(x, y);
dfs(x, -1, 0, 0);
dfs(y, -1, 0, 1);
for (int i = 0; i < n; i++){
pre[0][i] = dist[0][i];
pre[1][i] = dist[1][i];
pre[2][i] = max(dist[0][i], dist[1][i]);
}
for (int i = 1; i < n; i++)
for (int j = 0; j < 3; j++)
pre[j][i] += pre[j][i-1];
return -1;
}
int par[mxN];
void Par(int s, int p){
par[s] = p;
for (auto [u, w] : g[s])
if (u^p) Par(u, s);
}
int Solve3(){
Par(x, -1);
vector<int> path;
int s = y;
while (~s){
path.pb(s);
s = par[s];
}
dfs(x, -1, 0, 0);
dfs(y, -1, 0, 1);
int ans = BFS();
return ans;
}
ll Dist(int s, int p, ll d){
if (s == y) return d;
ll dist = 0;
for (auto [u, w] : g[s])
if (u^p) smax(dist, Dist(u, s, d + w));
return dist;
}
int Solve(){
if (Dist(x, -1, 0) > 2*k) return Solve1();
int mx = 0;
for (int i = 0; i < n; i++)
smax(mx, (int)g[i].size());
if (mx <= 2) return Solve2();
return Solve3();
}
int max_score(int N, int X, int Y, long long K, vector<int> U, vector<int> V, vector<int> W){
n = N; k = K;
x = X; y = Y;
for (int i = 0; i < n-1; i++){
g[U[i]].pb({V[i], W[i]});
g[V[i]].pb({U[i], W[i]});
}
int ans = Solve();
for (int i = 0; i < n; i++){
g[i].clear();
vis[0][i] = vis[1][i] = 0;
a[i] = 0;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
41308 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
48424 KB |
Output is correct |
2 |
Correct |
89 ms |
53296 KB |
Output is correct |
3 |
Correct |
50 ms |
31568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
41308 KB |
1st lines differ - on the 1st token, expected: '3', found: '-1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
41308 KB |
1st lines differ - on the 1st token, expected: '3', found: '-1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
41308 KB |
1st lines differ - on the 1st token, expected: '3', found: '-1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
41308 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
41308 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
41308 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
41308 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
41308 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |