답안 #850489

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
850489 2023-09-16T17:30:24 Z eltu0815 봉쇄 시간 (IOI23_closing) C++17
8 / 100
96 ms 34644 KB
#include "closing.h"

#include <bits/stdc++.h>
#define MAX 200005
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

int visited[MAX], parent[MAX];
vector<pii> graph[MAX];

ll tmp[MAX];
ll cost1[MAX], cost2[MAX];
ll dist1[MAX], dist2[MAX];

vector<pair<ll, ll> > vec;
multiset<pair<ll, ll> > st1;
multiset<pair<ll, ll> > st2;

void init(int node, int par) {
    parent[node] = par;
    for(auto [v, w] : graph[node]) if(v != par) init(v, node);
}

void dfs(int node, int par, ll dist[]) {
    for(auto [v, w] : graph[node]) if(v != par) {
        dist[v] = dist[node] + w;
        dfs(v, node, dist);
    }
}

ll c[MAX];
vector<int> path;

int max_score(int N, int X, int Y, long long K, vector<int> U, vector<int> V, vector<int> W)
{
    path.clear();
    for(int i = 0; i < N; ++i) graph[i].clear();
    for(int i = 0; i < N; ++i) dist1[i] = dist2[i] = visited[i] = c[i] = 0;
    for(int i = 0; i < N - 1; ++i) {
        graph[U[i]].push_back({V[i], W[i]});
        graph[V[i]].push_back({U[i], W[i]});
    }
    
    init(0, -1);
    dfs(X, -1, dist1);
    dfs(Y, -1, dist2);
    
    int mx = 0; ll sum = 0;
    priority_queue<pair<ll, int> > pq;
    pq.push({0, X}); pq.push({0, Y});
    while(!pq.empty()) {
        ll d = -pq.top().first;
        int node = pq.top().second;
        pq.pop();
        
        if(sum + d > K) break;
        ++mx; sum += d; visited[node] = 1;
        for(auto [v, w] : graph[node]) {
            if(visited[v]) continue;
            pq.push({-(d + w), v});
        }
    }
    
    if(dist1[Y] > 2 * K) return mx;
    
    int node = X;
    for(int i = 0; i < N; ++i) visited[i] = 0;
    while(node != -1) visited[node]++, node = parent[node];
    
    node = Y; int flag = 0;
    while(node != -1) {
        if(visited[node] == 1 && !flag) flag = 1;
        else if(flag) visited[node]--;
        else visited[node]++;
        node = parent[node];
    }
    
    node = X;
    while(node != -1 && visited[node]) {
        path.push_back(node);
        node = parent[node];
    }
    path.pop_back();
    
    node = Y;
    vector<int> path2;
    while(node != -1 && visited[node]) {
        path2.push_back(node);
        node = parent[node];
    }
    reverse(path2.begin(), path2.end());
    for(auto v : path2) path.push_back(v);
    
    int p = 0, q = path.size() - 1, r = 0; cost1[0] = 0;
    for(int i = 1; i <= 2 * N; ++i) cost1[i] = (ll)(2e18);
    for(int i = 1; i <= 2 * N; ++i) {
        int u = p == (int)path.size() ? -1 : path[p];
        int v = q == -1 ? -1 : path[q];
        
        if(u == -1 && v == -1) break;
        if(u == v) r = i;
        if(u == -1) {
            cost1[i] = cost1[i - 1] + dist2[v] - c[v];
            c[v] = dist2[v]; --q;
        }
        else if(v == -1) {
            cost1[i] = cost1[i - 1] + dist1[u] - c[u];
            c[u] = dist1[u]; ++p;
        }
        else if(dist1[u] - c[u] <= dist2[v] - c[v]) {
            cost1[i] = cost1[i - 1] + dist1[u] - c[u];
            c[u] = dist1[u]; ++p;
        }
        else {
            cost1[i] = cost1[i - 1] + dist2[v] - c[v];
            c[v] = dist2[v]; --q;
        }
    }
    for(int i = 0; i < r; ++i) cost1[i] = (ll)(2e18);
    
    vec.clear(); st1.clear(); st2.clear();
    for(int i = 1; i <= 2 * N; ++i) cost2[i] = (ll)(2e18);
    for(int i = 0; i < N; ++i) if(!visited[i]) {
        ll c1 = min(dist1[i], dist2[i]);
        ll c2 = max(dist1[i], dist2[i]);
        st1.insert({c1, c2}); st2.insert({c1 + c2, c1});
    }
    for(int i = 1; i <= 2 * N; ++i) {
        if(st1.empty()) break;
        if(st2.empty() || vec.empty() || vec.back().first + st1.begin()->first <= st2.begin()->first) {
            ll d = st1.begin()->first;
            ll nd = st1.begin()->second;
            st1.erase(st1.begin());
            cost2[i] = cost2[i - 1] + d;
            vec.push_back({d, nd});
            
            if(nd != -1) {
                st1.insert({nd, -1});
                st2.erase(st2.find({d + nd, d}));
            }
        }
        else {
            ll d1 = st2.begin()->first;
            ll d2 = st2.begin()->second;
            cost2[i] = cost2[i - 2] + d1;
            st2.erase(st2.begin());
            
            st1.insert(vec.back());
            vec.pop_back();
            vec.push_back({d2, d1 - d2});
            vec.push_back({d1 - d2, -1});
        }
    }
    
    for(int i = 2 * N, p = 0; i >= 0; --i) {
        while(p < 2 * N && cost1[i] + cost2[p + 1] <= K) ++p;
        if(cost1[i] + cost2[p] <= K) mx = max(mx, i + p);
    }
    return mx;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 30216 KB Output is correct
2 Correct 96 ms 34644 KB Output is correct
3 Correct 50 ms 13136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Incorrect 2 ms 12636 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Incorrect 2 ms 12636 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Incorrect 2 ms 12636 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Incorrect 2 ms 12636 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Incorrect 2 ms 12636 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Incorrect 2 ms 12636 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Incorrect 2 ms 12636 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Incorrect 2 ms 12636 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
4 Halted 0 ms 0 KB -