답안 #1078491

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1078491 2024-08-27T18:31:15 Z aykhn 봉쇄 시간 (IOI23_closing) C++17
0 / 100
1000 ms 298532 KB
#include <bits/stdc++.h>
#include "closing.h"
 
using namespace std;

#define int long long
 
struct BIT
{
  int n;
  vector<long long> ft;
  void init(int N)
  {
    n = N + 5;
    ft.assign(n + 5, 0);
  }
  void add(int pos, long long val)
  {
    for (pos = pos + 1; pos <= n; pos += pos & -pos) ft[pos] += val;
  }
  long long get(int pos, long long res = 0)
  {
    for (pos = pos + 1; pos > 0; pos -= pos & -pos) res += ft[pos];
    return res;
  }
};
 
const int MXN = 2e5 + 5;
 
int n, x, y;
long long k;
vector<array<long long, 2>> adj[MXN];
long long d[2][MXN], p[3][MXN];
vector<int> pt, PT;
int in[MXN], IN[MXN];

void dfs(int a, int p, int t)
{
  PT.push_back(a);
  if (a != p && a == y) pt = PT;
  for (const array<long long, 2> &v : adj[a])
  {
    if (v[0] == p) continue;
    d[t][v[0]] = d[t][a] + v[1];
    dfs(v[0], a, t);
  }
  PT.pop_back();
}
void getcomp(int a, int p, vector<int> &comp)
{
  for (array<long long, 2> &v : adj[a])
  {
    if (v[0] == p || in[v[0]]) continue;
    getcomp(v[0], a, comp);
  }
  comp.push_back(a);
}
#undef int long long
int max_score(int N, int X, int Y, long long K, vector<int> U, vector<int> V, vector<int> W)
{
  #define int long long
  n = N, x = X, y = Y, k = K;
  for (int i = 0; i < n; i++)
  {
    d[0][i] = d[1][i] = 0;
    adj[i].clear();
    in[i] = 0;
  }
  for (int i = 0; i < n - 1; i++)
  {
    adj[U[i]].push_back({V[i], W[i]});
    adj[V[i]].push_back({U[i], W[i]});
  }
  dfs(x, x, 0);
  dfs(y, y, 1);
  int ans = 0;
  {
    vector<long long> v;
    for (int i = 0; i < n; i++) v.push_back(min(d[0][i], d[1][i]));
    sort(v.begin(), v.end());
    long long k1 = k;
    for (int i = 0; i < n; i++)
    {
      k1 -= v[i];
      if (k1 < 0) break;
      ans++;
    }
  }
  vector<int> cc[n];
  for (int i = 0; i < pt.size(); i++) 
  {
    IN[pt[i]] = 1;
    getcomp(pt[i], pt[i], cc[pt[i]]);
    cc[i].pop_back();
  }
  vector<array<int, 2>> v[2];
  for (int i = 0; i < n; i++)
  {
    if (IN[i]) continue;
    v[0].push_back({min(d[0][i], d[1][i]), i});
    v[1].push_back({max(d[0][i], d[1][i]), i});
  }
  sort(v[0].begin(), v[0].end());
  sort(v[1].begin(), v[1].end());
  vector<int> vv = pt;
  sort(vv.begin(), vv.end(), [&](const int &x, const int &y)
  {
    return max(d[0][x], d[1][x]) < max(d[0][y], d[1][y]);
  });
  for (int take = 1; take <= pt.size(); take++)
  {
    int cost = 0;
    for (int i = 0; i < take; i++) cost += max(d[0][vv[i]], d[1][vv[i]]);
    for (int i = take; i < pt.size(); i++) cost += min(d[0][vv[i]], d[1][vv[i]]);
    if (cost > k) continue;
    int res = pt.size() + take;
    vector<int> v[2];
    for (int i = 0; i < n; i++)
    {
      if (i < take) for (int &k : cc[vv[i]]) v[1].push_back(k);
      else for (int &k : cc[vv[i]]) v[0].push_back(k);
    }
    sort(v[1].begin(), v[1].end(), [&](const int &x, const int &y)
    {
      return max(d[0][x], d[1][x]) < max(d[0][y], d[1][y]);
    });
    for (int cnt = v[1].size(); cnt >= 0; cnt--)
    {
      int cc = cost, rr = res;
      for (int j = 0; j < cnt; j++) cc += max(d[0][v[1][j]], d[1][v[1][j]]), rr += 2;
      if (cc > k) continue;
      sort(v[0].begin(), v[0].end(), [&](const int &x, const int &y)
      {
        return min(d[0][x], d[1][x]) < min(d[0][y], d[1][y]);
      });
      int prev = cc;
      for (int &k : v[0])
      {
        cc += min(d[0][k], d[1][k]);
        if (cc > k) break;
        rr++;
      }
      ans = max(ans, rr);
      if (cnt)
      {
        v[0].push_back(v[1].back());
      }
    }
  }
  return ans;
  #undef int
}

Compilation message

closing.cpp:58:12: warning: extra tokens at end of #undef directive
   58 | #undef int long long
      |            ^~~~
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:90:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |   for (int i = 0; i < pt.size(); i++)
      |                   ~~^~~~~~~~~~~
closing.cpp:110:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |   for (int take = 1; take <= pt.size(); take++)
      |                      ~~~~~^~~~~~~~~~~~
closing.cpp:114:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |     for (int i = take; i < pt.size(); i++) cost += min(d[0][vv[i]], d[1][vv[i]]);
      |                        ~~^~~~~~~~~~~
closing.cpp:136:11: warning: unused variable 'prev' [-Wunused-variable]
  136 |       int prev = cc;
      |           ^~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 10076 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1071 ms 298532 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4956 KB Output is correct
2 Runtime error 5 ms 10072 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4956 KB Output is correct
2 Runtime error 5 ms 10072 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4956 KB Output is correct
2 Runtime error 5 ms 10072 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 10076 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 10076 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 10076 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 10076 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 10076 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -