#include <bits/stdc++.h>
#include "closing.h"
using namespace std;
vector<pair<int, long long>> adj[200005];
long long D[200005][2];
vector<long long> pL, pR, pM;
void dfs(int X, int p, int st) {
for(auto u : adj[X]) {
if(u.first == p) continue;
D[u.first][st] = D[X][st] + u.second;
dfs(u.first, X, st);
}
}
long long sum(int l, int r, vector<long long> arr) {
return arr[r] - (l > 0 ? arr[l - 1] : 0);
}
int max_score(int N, int X, int Y, long long K,
vector<int> U, vector<int> V, vector<int> W) {
pL.resize(N); pR.resize(N); pM.resize(N);
for(int l = 0; l < N; l++) D[l][0] = D[l][1] = 2e18 + 7, adj[l].clear();
for(int l = 0; l < N - 1; l++) {
adj[U[l]].push_back({V[l], W[l]});
adj[V[l]].push_back({U[l], W[l]});
}
if(X > Y) swap(X, Y);
D[X][0] = 0, D[Y][1] = 0;
dfs(X, X, 0); dfs(Y, Y, 1);
for(int l = 0; l < N; l++) {
pL[l] = D[l][0];
pR[l] = D[l][1];
pM[l] = min(D[l][0], D[l][1]);
if(l > 0) {
pL[l] += pL[l - 1];
pR[l] += pR[l - 1];
pM[l] += pM[l - 1];
}
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q;
Q.push({0, X}); Q.push({0, Y});
vector<long long> dist(N, 1e18);
dist[X] = dist[Y] = 0;
int res = 0; long long cur = 0;
while(!Q.empty()) {
int A = Q.top().second;
long long B = Q.top().first; Q.pop();
if(cur + B > K) break;
cur += B; res++;
for(auto u : adj[A]) {
if(dist[u.first] > dist[A] + u.second) {
dist[u.first] = dist[A] + u.second;
Q.push({dist[u.first], u.first});
}
}
}
for(int l = 0; l <= N; l++) {
for(int i = l; i <= N; i++) {
long long calc = sum(l, i, pL) + sum(l, i, pR) - sum(l, i, pM);
if(calc > K) continue;
int cur = i - l + 1;
priority_queue<pair<int, long long>, vector<pair<int, long long>>, greater<pair<int, long long>>> pq;
if(i < N - 1) pq.push({D[i + 1][1], i + 1});
if(l > 0) pq.push({D[l - 1][0], l - 1});
while(!pq.empty()) {
int x = pq.top().second; long long w = pq.top().first;
pq.pop(); calc += w;
if(calc > K) break;
++cur;
if(x > i && x < N - 1) pq.push({D[x + 1][1], x + 1});
if(x < l && x > 0) pq.push({D[x - 1][0], x - 1});
}
res = max(res, cur);
}
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
5976 KB |
1st lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1034 ms |
43324 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5980 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5980 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5980 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
5976 KB |
1st lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
5976 KB |
1st lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
5976 KB |
1st lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
5976 KB |
1st lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
5976 KB |
1st lines differ - on the 1st token, expected: '6', found: '8' |
2 |
Halted |
0 ms |
0 KB |
- |