답안 #888476

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
888476 2023-12-17T14:01:39 Z nguyentunglam Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
1838 ms 32852 KB
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define endl "\n"
#define int long long
using namespace std;

const int N = 510, M = 1e5 + 10;

int lab[N], p[N];

int n, m, q;

int x[M], y[M], w[M];

bool in[M];

int root(int v) {
  return lab[v] < 0 ? v : lab[v] = root(lab[v]);
}

void join(int u, int v) {
  u = root(u); v = root(v);
  if (u == v) return;
  if (lab[u] > lab[v]) swap(u, v);
  lab[u] += lab[v];
  lab[v] = u;
}

tuple<int, int, int> e[M];

int32_t main() {
  #define task ""

  cin.tie(0) -> sync_with_stdio(0);

  if (fopen("task.inp", "r")) {
    freopen("task.inp", "r", stdin);
    freopen("task.out", "w", stdout);
  }

  if (fopen(task".inp", "r")) {
    freopen (task".inp", "r", stdin);
    freopen (task".out", "w", stdout);
  }

  cin >> n >> m;

  vector<tuple<int, int, int> > e;

  for(int i = 1; i <= m; i++) {
    int a, b, c; cin >> a >> b >> c;
    e.emplace_back(c, a, b);
  }

  sort(all(e));

  for(int i = 1; i <= m; i++) tie(w[i], x[i], y[i]) = e[i - 1];

  for(int i = 1; i <= n; i++) p[i] = m;

  vector<tuple<int, int, int> > event;

//  for(int i = 1; i <= m; i++) cout << x[i] << " " << y[i] << " " << w[i] << endl;
  #define ii pair<int, int>
  priority_queue<ii, vector<ii>, greater<ii> > Q;

  long long sum = 0;

  int cnt = 0;

  for(int i = m; i >= 1; i--) if (x[i] != y[i]) {
    fill(lab + 1, lab + n + 1, -1);
    p[0] = i;
    int better = 0;
    for(int j = 1; j < n; j++) {
      int k = min(p[j - 1] - 1, p[j]);
      while (k > 0 && root(x[k]) == root(y[k])) k--;
      if (k == 0) break;
      p[j] = k;
      join(x[k], y[k]);
      if (better == 0 && root(x[i]) == root(y[i])) better = k;
    }
    if (!better) {
      in[i] = 1;
      Q.push({w[i], i});
      sum += w[i];
      cnt--;
    }
    else event.emplace_back(w[better] + w[i] + 1 >> 1, better, i);
  }

  sort(all(event));

  cin >> q;

  int j = 0;

  auto calc = [&] (int x) {
    while (!Q.empty() && Q.top().first < x) {
      int j = Q.top().second; Q.pop();
      if (!in[j]) continue;
      sum -= w[j] * 2;
      cnt += 2;
    }
  };

  while (q--) {
    int x; cin >> x;

    while (j < event.size() && x >= get<0> (event[j])) {
      int useless, a, b; tie(useless, a, b) = event[j];
      calc(x);
      in[a] = 0;
      if (w[a] >= x) {
        sum -= w[a];
        cnt++;
      }
      else {
        sum += w[a];
        cnt--;
      }
      in[b] = 1;
      Q.push({w[b], b});
      sum += w[b];
      cnt--;
      j++;
    }
    calc(x);
    cout << sum + 1LL * x * cnt << endl;
  }

}

Compilation message

reconstruction.cpp: In function 'int32_t main()':
reconstruction.cpp:89:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   89 |     else event.emplace_back(w[better] + w[i] + 1 >> 1, better, i);
      |                             ~~~~~~~~~~~~~~~~~^~~
reconstruction.cpp:110:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::tuple<long long int, long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |     while (j < event.size() && x >= get<0> (event[j])) {
      |            ~~^~~~~~~~~~~~~~
reconstruction.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     freopen("task.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |     freopen("task.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:42:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |     freopen (task".inp", "r", stdin);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:43:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |     freopen (task".out", "w", stdout);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2460 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2520 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2460 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2520 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 916 ms 9808 KB Output is correct
21 Correct 1146 ms 11580 KB Output is correct
22 Correct 862 ms 11632 KB Output is correct
23 Correct 834 ms 11852 KB Output is correct
24 Correct 910 ms 12184 KB Output is correct
25 Correct 895 ms 11872 KB Output is correct
26 Correct 894 ms 12100 KB Output is correct
27 Correct 879 ms 13048 KB Output is correct
28 Correct 899 ms 12508 KB Output is correct
29 Correct 547 ms 12480 KB Output is correct
30 Correct 897 ms 11544 KB Output is correct
31 Correct 902 ms 13092 KB Output is correct
32 Correct 952 ms 11964 KB Output is correct
33 Correct 933 ms 12776 KB Output is correct
34 Correct 371 ms 12228 KB Output is correct
35 Correct 915 ms 13056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2804 KB Output is correct
4 Correct 989 ms 28128 KB Output is correct
5 Correct 980 ms 30320 KB Output is correct
6 Correct 992 ms 30224 KB Output is correct
7 Correct 593 ms 32264 KB Output is correct
8 Correct 483 ms 32576 KB Output is correct
9 Correct 499 ms 32852 KB Output is correct
10 Correct 971 ms 30288 KB Output is correct
11 Correct 430 ms 32720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2460 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2520 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2648 KB Output is correct
20 Correct 158 ms 14820 KB Output is correct
21 Correct 170 ms 14904 KB Output is correct
22 Correct 163 ms 14748 KB Output is correct
23 Correct 167 ms 14804 KB Output is correct
24 Correct 190 ms 14848 KB Output is correct
25 Correct 155 ms 14620 KB Output is correct
26 Correct 167 ms 14708 KB Output is correct
27 Correct 170 ms 14792 KB Output is correct
28 Correct 169 ms 14716 KB Output is correct
29 Correct 170 ms 14588 KB Output is correct
30 Correct 178 ms 14932 KB Output is correct
31 Correct 155 ms 14748 KB Output is correct
32 Correct 171 ms 15340 KB Output is correct
33 Correct 163 ms 14768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2460 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2520 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 916 ms 9808 KB Output is correct
21 Correct 1146 ms 11580 KB Output is correct
22 Correct 862 ms 11632 KB Output is correct
23 Correct 834 ms 11852 KB Output is correct
24 Correct 910 ms 12184 KB Output is correct
25 Correct 895 ms 11872 KB Output is correct
26 Correct 894 ms 12100 KB Output is correct
27 Correct 879 ms 13048 KB Output is correct
28 Correct 899 ms 12508 KB Output is correct
29 Correct 547 ms 12480 KB Output is correct
30 Correct 897 ms 11544 KB Output is correct
31 Correct 902 ms 13092 KB Output is correct
32 Correct 952 ms 11964 KB Output is correct
33 Correct 933 ms 12776 KB Output is correct
34 Correct 371 ms 12228 KB Output is correct
35 Correct 915 ms 13056 KB Output is correct
36 Correct 900 ms 12800 KB Output is correct
37 Correct 1173 ms 12376 KB Output is correct
38 Correct 772 ms 13288 KB Output is correct
39 Correct 844 ms 12956 KB Output is correct
40 Correct 950 ms 12992 KB Output is correct
41 Correct 927 ms 12268 KB Output is correct
42 Correct 899 ms 11992 KB Output is correct
43 Correct 898 ms 11972 KB Output is correct
44 Correct 884 ms 13532 KB Output is correct
45 Correct 584 ms 12032 KB Output is correct
46 Correct 910 ms 11720 KB Output is correct
47 Correct 909 ms 12804 KB Output is correct
48 Correct 940 ms 12468 KB Output is correct
49 Correct 905 ms 12548 KB Output is correct
50 Correct 371 ms 13252 KB Output is correct
51 Correct 887 ms 12940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2460 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2520 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 916 ms 9808 KB Output is correct
21 Correct 1146 ms 11580 KB Output is correct
22 Correct 862 ms 11632 KB Output is correct
23 Correct 834 ms 11852 KB Output is correct
24 Correct 910 ms 12184 KB Output is correct
25 Correct 895 ms 11872 KB Output is correct
26 Correct 894 ms 12100 KB Output is correct
27 Correct 879 ms 13048 KB Output is correct
28 Correct 899 ms 12508 KB Output is correct
29 Correct 547 ms 12480 KB Output is correct
30 Correct 897 ms 11544 KB Output is correct
31 Correct 902 ms 13092 KB Output is correct
32 Correct 952 ms 11964 KB Output is correct
33 Correct 933 ms 12776 KB Output is correct
34 Correct 371 ms 12228 KB Output is correct
35 Correct 915 ms 13056 KB Output is correct
36 Correct 1 ms 2396 KB Output is correct
37 Correct 1 ms 2396 KB Output is correct
38 Correct 1 ms 2804 KB Output is correct
39 Correct 989 ms 28128 KB Output is correct
40 Correct 980 ms 30320 KB Output is correct
41 Correct 992 ms 30224 KB Output is correct
42 Correct 593 ms 32264 KB Output is correct
43 Correct 483 ms 32576 KB Output is correct
44 Correct 499 ms 32852 KB Output is correct
45 Correct 971 ms 30288 KB Output is correct
46 Correct 430 ms 32720 KB Output is correct
47 Correct 1 ms 2648 KB Output is correct
48 Correct 158 ms 14820 KB Output is correct
49 Correct 170 ms 14904 KB Output is correct
50 Correct 163 ms 14748 KB Output is correct
51 Correct 167 ms 14804 KB Output is correct
52 Correct 190 ms 14848 KB Output is correct
53 Correct 155 ms 14620 KB Output is correct
54 Correct 167 ms 14708 KB Output is correct
55 Correct 170 ms 14792 KB Output is correct
56 Correct 169 ms 14716 KB Output is correct
57 Correct 170 ms 14588 KB Output is correct
58 Correct 178 ms 14932 KB Output is correct
59 Correct 155 ms 14748 KB Output is correct
60 Correct 171 ms 15340 KB Output is correct
61 Correct 163 ms 14768 KB Output is correct
62 Correct 900 ms 12800 KB Output is correct
63 Correct 1173 ms 12376 KB Output is correct
64 Correct 772 ms 13288 KB Output is correct
65 Correct 844 ms 12956 KB Output is correct
66 Correct 950 ms 12992 KB Output is correct
67 Correct 927 ms 12268 KB Output is correct
68 Correct 899 ms 11992 KB Output is correct
69 Correct 898 ms 11972 KB Output is correct
70 Correct 884 ms 13532 KB Output is correct
71 Correct 584 ms 12032 KB Output is correct
72 Correct 910 ms 11720 KB Output is correct
73 Correct 909 ms 12804 KB Output is correct
74 Correct 940 ms 12468 KB Output is correct
75 Correct 905 ms 12548 KB Output is correct
76 Correct 371 ms 13252 KB Output is correct
77 Correct 887 ms 12940 KB Output is correct
78 Correct 1072 ms 29580 KB Output is correct
79 Correct 1838 ms 31096 KB Output is correct
80 Correct 951 ms 30524 KB Output is correct
81 Correct 987 ms 30268 KB Output is correct
82 Correct 1080 ms 30068 KB Output is correct
83 Correct 1067 ms 29080 KB Output is correct
84 Correct 1061 ms 29760 KB Output is correct
85 Correct 1038 ms 29088 KB Output is correct
86 Correct 1036 ms 29544 KB Output is correct
87 Correct 713 ms 31304 KB Output is correct
88 Correct 1075 ms 29248 KB Output is correct
89 Correct 1076 ms 29924 KB Output is correct
90 Correct 1047 ms 29312 KB Output is correct
91 Correct 1069 ms 30244 KB Output is correct
92 Correct 539 ms 32072 KB Output is correct
93 Correct 1056 ms 30252 KB Output is correct