답안 #848758

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
848758 2023-09-13T12:58:06 Z math_rabbit_1028 봉쇄 시간 (IOI23_closing) C++17
26 / 100
95 ms 36868 KB
#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<ll> over, below;
bool compare(pll p, pll q) {
    if (p.second == q.second) return p.first < q.first;
    return p.second < q.second;
}
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);
    }

    over.push_back(0);
    for (int i = 0; i < ab.size(); i++) {
        if (ab[i].first * 2 <= ab[i].second) {
            over.push_back(ab[i].first);
            if (ab[i].second <= 1e18) over.push_back(ab[i].second - ab[i].first);
        }
    }
    sort(over.begin(), over.end());
    for (int i = 0; i < over.size(); i++) {
        if (i >= 1) over[i] += over[i - 1];
    }

    ll diff = 0;
    below.push_back(0);
    sort(ab.begin(), ab.end(), compare);
    for (int i = 0; i < ab.size(); i++) {
        if (ab[i].first * 2 > ab[i].second) {
            ll t = below.back() + ab[i].second;
            diff = max(diff, ab[i].second - ab[i].first);
            below.push_back(t - diff);
            below.push_back(t);
        }
    }
    sort(below.begin(), below.end());
    for (int i = 0; i < over.size(); i++) {
        if (k - over[i] < 0) break;
        int idx = upper_bound(below.begin(), below.end(), k - over[i]) - below.begin() - 1;
        cnt = max(cnt, L + 1 + i + 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:62: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]
   62 |     for (int i = 0; i < adj[v].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
closing.cpp: In function 'int intersect()':
closing.cpp:98: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]
   98 |     for (int i = 0; i < ab.size(); i++) {
      |                     ~~^~~~~~~~~~~
closing.cpp:105:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |     for (int i = 0; i < over.size(); i++) {
      |                     ~~^~~~~~~~~~~~~
closing.cpp:112: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]
  112 |     for (int i = 0; i < ab.size(); i++) {
      |                     ~~^~~~~~~~~~~
closing.cpp:121:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |     for (int i = 0; i < over.size(); i++) {
      |                     ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 30408 KB Output is correct
2 Correct 95 ms 36868 KB Output is correct
3 Correct 60 ms 11344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 1 ms 8796 KB Output is correct
3 Correct 2 ms 8792 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8792 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8792 KB Output is correct
10 Correct 2 ms 8792 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 1 ms 8796 KB Output is correct
3 Correct 2 ms 8792 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8792 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8792 KB Output is correct
10 Correct 2 ms 8792 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 1 ms 8792 KB Output is correct
13 Correct 2 ms 8792 KB Output is correct
14 Correct 2 ms 8792 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 2 ms 8792 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Incorrect 3 ms 8792 KB 2nd lines differ - on the 1st token, expected: '25', found: '30'
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 1 ms 8796 KB Output is correct
3 Correct 2 ms 8792 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8792 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8792 KB Output is correct
10 Correct 2 ms 8792 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 1 ms 8792 KB Output is correct
13 Correct 2 ms 8792 KB Output is correct
14 Correct 2 ms 8792 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 2 ms 8792 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Incorrect 3 ms 8792 KB 2nd lines differ - on the 1st token, expected: '25', found: '30'
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8792 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 2 ms 8792 KB Output is correct
5 Correct 1 ms 8796 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 1 ms 9048 KB Output is correct
8 Correct 1 ms 8792 KB Output is correct
9 Correct 2 ms 8792 KB Output is correct
10 Correct 1 ms 8796 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 1 ms 8792 KB Output is correct
14 Correct 1 ms 8792 KB Output is correct
15 Correct 1 ms 8792 KB Output is correct
16 Correct 2 ms 8792 KB Output is correct
17 Correct 2 ms 8792 KB Output is correct
18 Correct 2 ms 8792 KB Output is correct
19 Correct 2 ms 8792 KB Output is correct
20 Correct 2 ms 8792 KB Output is correct
21 Correct 2 ms 8792 KB Output is correct
22 Correct 2 ms 8792 KB Output is correct
23 Correct 2 ms 8792 KB Output is correct
24 Correct 2 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8792 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 2 ms 8792 KB Output is correct
5 Correct 1 ms 8796 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8792 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8792 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 2 ms 8792 KB Output is correct
13 Correct 1 ms 8792 KB Output is correct
14 Correct 2 ms 8792 KB Output is correct
15 Correct 2 ms 8792 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 2 ms 8792 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Correct 1 ms 9048 KB Output is correct
20 Correct 1 ms 8792 KB Output is correct
21 Correct 2 ms 8792 KB Output is correct
22 Correct 1 ms 8796 KB Output is correct
23 Correct 2 ms 8792 KB Output is correct
24 Correct 2 ms 8796 KB Output is correct
25 Correct 1 ms 8792 KB Output is correct
26 Correct 1 ms 8792 KB Output is correct
27 Correct 1 ms 8792 KB Output is correct
28 Correct 2 ms 8792 KB Output is correct
29 Correct 2 ms 8792 KB Output is correct
30 Correct 2 ms 8792 KB Output is correct
31 Correct 2 ms 8792 KB Output is correct
32 Correct 2 ms 8792 KB Output is correct
33 Correct 2 ms 8792 KB Output is correct
34 Correct 2 ms 8792 KB Output is correct
35 Correct 2 ms 8792 KB Output is correct
36 Correct 2 ms 8796 KB Output is correct
37 Incorrect 2 ms 8792 KB 2nd lines differ - on the 1st token, expected: '19', found: '23'
38 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8792 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 2 ms 8792 KB Output is correct
5 Correct 1 ms 8796 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8792 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8792 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 2 ms 8792 KB Output is correct
13 Correct 1 ms 8792 KB Output is correct
14 Correct 2 ms 8792 KB Output is correct
15 Correct 2 ms 8792 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 2 ms 8792 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Incorrect 3 ms 8792 KB 2nd lines differ - on the 1st token, expected: '25', found: '30'
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8792 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 2 ms 8792 KB Output is correct
5 Correct 1 ms 8796 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8792 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8792 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 2 ms 8792 KB Output is correct
13 Correct 1 ms 8792 KB Output is correct
14 Correct 2 ms 8792 KB Output is correct
15 Correct 2 ms 8792 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 2 ms 8792 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Incorrect 3 ms 8792 KB 2nd lines differ - on the 1st token, expected: '25', found: '30'
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8792 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 2 ms 8792 KB Output is correct
5 Correct 1 ms 8796 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8792 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8792 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 2 ms 8792 KB Output is correct
13 Correct 1 ms 8792 KB Output is correct
14 Correct 2 ms 8792 KB Output is correct
15 Correct 2 ms 8792 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 2 ms 8792 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Incorrect 3 ms 8792 KB 2nd lines differ - on the 1st token, expected: '25', found: '30'
20 Halted 0 ms 0 KB -