#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
int n, x, y, ch[202020], ans;
ll k, need[202020], dis[202020];
vector< pair<int, ll> > adj[202020];
priority_queue<pll> pq;
int L, midp;
vector< pair<int, ll> > path;
void find_path(int v) {
for (int i = 0; i < adj[v].size(); i++) {
if (path.back().first == y) return;
int u = adj[v][i].first; ll w = adj[v][i].second;
if (ch[u] == 1) continue;
ch[u] = 1;
path.push_back({u, w});
find_path(u);
if (path.back().first != y) path.pop_back();
}
}
int not_intersect() {
int cnt = 0;
for (int i = 0; i < n; i++) ch[i] = 0;
while (!pq.empty()) pq.pop();
pq.push({-0, x});
pq.push({-0, y});
while (!pq.empty()) {
int v = pq.top().second; ll w = -pq.top().first;
pq.pop();
if (k - w >= 0) {
cnt++;
k -= w;
}
else break;
ch[v] = 1;
for (int i = 0; i < (int)adj[v].size(); i++) {
if (ch[adj[v][i].first] == 1) continue;
if (v == path[midp - 1].first && adj[v][i].first == path[midp].first) continue;
if (v == path[midp].first && adj[v][i].first == path[midp - 1].first) continue;
pq.push({-w-adj[v][i].second, adj[v][i].first});
}
}
return cnt;
}
vector<pll> ab;
vector<pll> over, below;
void DFS(int v, ll a, ll b) {
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i].first; ll w = adj[v][i].second;
if (ch[u] == 1) continue;
ch[u] = 1;
ab.push_back({a + w, b + w});
DFS(u, a + w, b + w);
}
}
int intersect() {
int cnt = L + 1;
over.clear();
below.clear();
for (int i = 0; i < n; i++) ch[i] = 0;
for (int i = 0; i <= L; i++) ch[path[i].first] = 1;
for (int i = 0; i < midp; i++) need[i] = dis[i] - dis[0];
for (int i = midp; i <= L; i++) need[i] = dis[L] - dis[i];
for (int i = 1; i <= L; i++) k -= need[i];
if (k < 0) return 0;
for (int i = 0; i < midp; i++) {
ab.push_back({dis[L] - dis[i] - need[i], 2e18});
ll a = dis[i] - dis[0];
ll b = dis[L] - dis[i];
DFS(path[i].first, a, b);
}
for (int i = midp; i <= L; i++) {
ab.push_back({dis[i] - dis[0] - need[i], 2e18});
ll a = dis[L] - dis[i];
ll b = dis[i] - dis[0];
DFS(path[i].first, a, b);
}
for (int i = 0; i < ab.size(); i++) {
if (ab[i].first * 2 <= ab[i].second) {
over.push_back({ab[i].first, 0});
if (ab[i].second <= 1e18) over.push_back({ab[i].second - ab[i].first, 0});
}
}
sort(over.begin(), over.end());
for (int i = 0; i < over.size(); i++) {
if (i >= 1) over[i].first += over[i - 1].first;
over[i].second = i + 1;
//cout << over[i].first << " ";
}
//cout << "\n";
map<ll, ll> m;
for (int i = 0; i < ab.size(); i++) {
if (ab[i].first * 2 > ab[i].second) {
if (m.find(ab[i].first) == m.end()) m.insert({ab[i].first, 1});
else m[ab[i].first]++;
}
}
below.push_back({0, m.begin()->first});
for (int i = 0; i < ab.size(); i++) {
if (ab[i].first * 2 > ab[i].second) {
m[ab[i].first]--;
while (m.size() > 0 && m.begin()->second == 0) m.erase(m.begin());
below.push_back({ab[i].second, 0});
if (m.size() > 0) below.push_back({ab[i].second, m.begin()->first});
}
}
sort(below.begin(), below.end());
below[0].first = below[0].second;
below[0].second = 1;
for (int i = 1; i < below.size(); i += 2) {
if (i >= 2) below[i].first += below[i - 2].first;
if (i + 1 < below.size()) below[i + 1].first = below[i].first + below[i + 1].second;
below[i].second = i + 1;
}
//for (int i = 0; i < below.size(); i++) cout << below[i].first << " ";
//cout << "\n";
for (int i = 0; i < over.size(); i++) {
if (k - over[i].first < 0) break;
int idx = lower_bound(below.begin(), below.end(), make_pair(k - over[i].first + 1, 0LL))
- below.begin();
cnt = max(cnt, L + 1 + i + 1 + idx);
}
int idx = lower_bound(below.begin(), below.end(), make_pair(k + 1, 0LL)) - below.begin();
cnt = max(cnt, L + 1 + idx);
return cnt;
}
int max_score(int N, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W) {
n = N, x = X, y = Y, k = K;
ans = 0;
for (int i = 0; i < n; i++) adj[i].clear();
path.clear();
for (int i = 0; i < (int)U.size(); i++) {
adj[U[i]].push_back({V[i], W[i]});
adj[V[i]].push_back({U[i], W[i]});
}
for (int i = 0; i < n; i++) ch[i] = 0;
path.push_back({x, 0});
ch[x] = 1;
find_path(x);
assert(path.back().first == y);
L = path.size() - 1;
for (int i = 1; i <= L; i++) dis[i] = dis[i - 1] + path[i].second;
for (int i = 0; i <= L; i++) {
if (dis[i] - dis[0] > dis[L] - dis[i]) {
midp = i;
break;
}
}
k = K;
ans = not_intersect();
k = K;
ans = max(ans, intersect());
return ans;
}
Compilation message
closing.cpp: In function 'void find_path(int)':
closing.cpp:15:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for (int i = 0; i < adj[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~
closing.cpp: In function 'void DFS(int, ll, ll)':
closing.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < adj[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~
closing.cpp: In function 'int intersect()':
closing.cpp:93:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int>, std::allocator<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int i = 0; i < ab.size(); i++) {
| ~~^~~~~~~~~~~
closing.cpp:100:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int>, std::allocator<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for (int i = 0; i < over.size(); i++) {
| ~~^~~~~~~~~~~~~
closing.cpp:108:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int>, std::allocator<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for (int i = 0; i < ab.size(); i++) {
| ~~^~~~~~~~~~~
closing.cpp:115:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int>, std::allocator<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for (int i = 0; i < ab.size(); i++) {
| ~~^~~~~~~~~~~
closing.cpp:126:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int>, std::allocator<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
126 | for (int i = 1; i < below.size(); i += 2) {
| ~~^~~~~~~~~~~~~~
closing.cpp:128:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int>, std::allocator<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | if (i + 1 < below.size()) below[i + 1].first = below[i].first + below[i + 1].second;
| ~~~~~~^~~~~~~~~~~~~~
closing.cpp:134:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int>, std::allocator<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | for (int i = 0; i < over.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
30472 KB |
Output is correct |
2 |
Correct |
99 ms |
37076 KB |
Output is correct |
3 |
Correct |
44 ms |
11216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
8800 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
8800 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
8800 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8800 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8800 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8800 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8800 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8800 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
3 |
Halted |
0 ms |
0 KB |
- |