답안 #960395

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
960395 2024-04-10T11:30:55 Z Prieved1 봉쇄 시간 (IOI23_closing) C++17
8 / 100
118 ms 43380 KB
#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN=200010;
vector<pair<int,int>> g[MAXN];
long long distX[MAXN], distY[MAXN];
int dp[MAXN];
vector<int> path;
void dfsx(int at, int p) {
  for(auto [to,w]:g[at]) {
    if(to==p)continue;
    distX[to]=distX[at]+w;
    dfsx(to, at);
  }
}
void dfsy(int at, int p) {
  for(auto [to,w]:g[at]) {
    if(to==p)continue;
    distY[to]=distY[at]+w;
    dfsy(to, at);
    dp[at]|=dp[to];
  }
  if(dp[at])path.push_back(at);
}
int max_score(int N, int X, int Y, long long K,
              std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
  for(int i = 0;i<N-1;i++) {
    g[U[i]].push_back({V[i], W[i]});
    g[V[i]].push_back({U[i], W[i]});
  }
  dp[X]=1;
  dfsx(X, -1);
  dfsy(Y, -1);
  int ans=0;
  // case 1
  {
    int cnt=0;
    priority_queue<long long> pq;
    for(int i = 0;i<N;i++) {
      pq.push(-distX[i]);
      pq.push(-distY[i]);
    }
    long long sum=0;
    while(sum<=K and pq.size()) {
      sum-=pq.top();
      cnt++;
      pq.pop();
      if(sum<=K)ans=max(ans,cnt);
    }
  }
  // case 2
  {
    long long sum=0;
    int cnt=0;
    priority_queue<pair<long long,long long>> pq;
    multiset<long long> ms;
    for(int i = 0;i<N;i++) {
      if(distX[i]>distY[i])swap(distX[i], distY[i]);
      distY[i]-=distX[i];
    }
    int upd[N];
    memset(upd, 0, sizeof upd);
    for(int i:path) {
      sum+=distX[i];
      upd[i]++;
      cnt++;
    }
    for(int i = 0;i<N;i++) {
      if(upd[i]==1) {
        pq.push({-2*distY[i], -1});
        continue;
      }
      if(distX[i]>distY[i]) {
        pq.push({-distY[i]-distX[i], -distX[i]-2}); 
        ms.insert(distX[i]);
      }
      else {
        pq.push({-2*distX[i], distY[i]});
      }
    }
    while(pq.size() and sum<=K) {
      auto at=pq.top();
      pq.pop();
      at.first*=-1;
      if(at.second == -1 or at.second>=0) {
        at.first/=2;
        if(sum+at.first<=K) {
          sum+=at.first;
          if(at.second>=0) {
            pq.push({-2*at.second, -1});
          }
          cnt++;
        }
        else break;
      }
      else {
        at.second+=2;
        at.first*=2;
        at.second*=-1;
        if(sum+at.first<=K) {
          sum+=at.first;
          cnt+=2;
          ms.erase(ms.find(at.second));
        }
        else continue;
      }
      ans=max(ans, cnt);
      if(ms.size() and sum+(*ms.begin())<=K)ans=max(ans, cnt+1);
    }
  }
  for(int i = 0;i<=N;i++) {
    dp[i]=0;
    distX[i]=distY[i]=0;
    g[i].clear();
  }
  path.clear();
  return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 37732 KB Output is correct
2 Correct 118 ms 43380 KB Output is correct
3 Correct 61 ms 12004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6744 KB Output is correct
2 Incorrect 2 ms 6744 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6744 KB Output is correct
2 Incorrect 2 ms 6744 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6744 KB Output is correct
2 Incorrect 2 ms 6744 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 2 ms 6744 KB Output is correct
3 Incorrect 2 ms 6744 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 2 ms 6744 KB Output is correct
3 Incorrect 2 ms 6744 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 2 ms 6744 KB Output is correct
3 Incorrect 2 ms 6744 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 2 ms 6744 KB Output is correct
3 Incorrect 2 ms 6744 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 2 ms 6744 KB Output is correct
3 Incorrect 2 ms 6744 KB 1st lines differ - on the 1st token, expected: '30', found: '24'
4 Halted 0 ms 0 KB -