QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#497135#6127. Kawa Examucup-team1198#WA 446ms53316kbC++206.0kb2024-07-28 19:41:022024-07-28 19:41:02

Judging History

你现在查看的是最新测评结果

  • [2024-07-28 19:41:02]
  • 评测
  • 测评结果:WA
  • 用时:446ms
  • 内存:53316kb
  • [2024-07-28 19:41:02]
  • 提交

answer

#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;

const int MAXN = 100100;

vector<pair<int, int>> G[MAXN];
int tin[MAXN];
int tup[MAXN];
int ttime = 0;
int col[MAXN];
bool is_bridge[MAXN];

void dfs1(int v, int p) {
  tin[v] = ttime;
  tup[v] = ttime;
  ++ttime;
  for (auto [u, i] : G[v]) {
    if (i == p)
      continue;
    if (tin[u] == -1) {
      dfs1(u, i);
      tup[v] = min(tup[v], tup[u]);
      if (tup[u] > tin[v]) {
        is_bridge[i] = true;
      }
    } else {
      tup[v] = min(tup[v], tin[u]);
    }
  }
}

void dfs2(int v, int c) {
  col[v] = c;
  for (auto [u, i] : G[v]) {
    if (is_bridge[i])
      continue;
    if (col[u] == -1) {
      dfs2(u, c);
    }
  }
}

vector<int> vals[MAXN];
int sz[MAXN];


struct Something {
  int id[MAXN];
  vector<int> start_values;
  vector<int> real_values;
  int cur_mx;
  int i;
  int int_cnt;
  int start_int_cnt;

  void setup(vector<pair<int, int>> vals) {
    real_values.resize(vals.size());
    for (int i = 0; i < vals.size(); ++i) {
      id[vals[i].second] = i;
      real_values[i] = vals[i].first;
    }
    start_values = real_values;
    cur_mx = real_values[0];
    start_int_cnt = 1;
    while (start_int_cnt < start_values.size() && start_values[start_int_cnt] >= cur_mx)
      ++start_int_cnt;
    int_cnt = start_int_cnt;
    i = 0;
  }
  void erase(int x) {
    --real_values[id[x]];
    while (true) {
      while (i < int_cnt && real_values[i] < cur_mx)
        ++i;
      if (i < int_cnt)
        break;
      --cur_mx;
      while (int_cnt < start_values.size() && start_values[int_cnt] >= cur_mx)
        ++int_cnt;
      i = 0;
    }
  }
  void insert(int x) {
    ++real_values[id[x]];
  } // all inserts are after all erase and then a rebuild
  void rebuild() {
    cur_mx = start_values[0];
    int_cnt = start_int_cnt;
  }
  int get_mx() {
    return cur_mx;
  }
};

struct SomethingDumb {
  int cnt[MAXN];
  int mx_id = 0;

  void insert(int x) {
    ++cnt[x];
    if (cnt[x] > cnt[mx_id])
      mx_id = x;
  }
  void erase(int x) {
    --cnt[x];
  }
  int get_mx() {
    return cnt[mx_id];
  }
};

const int K = 18;
Something lefts[K];
SomethingDumb rights[K];
vector<int> vertices[K];

void move(Something& from, SomethingDumb& to, int x) {
  from.erase(x);
  to.insert(x);
}

void move(SomethingDumb& from, Something& to, int x) {
  from.erase(x);
  to.insert(x);
}

vector<int> comp;

void dfs_sz(int v, int p) {
  comp.emplace_back(v);
  sz[v] = 1;
  for (int j = 0; j < G[v].size(); ++j) {
    auto [u, i] = G[v][j];
    if (i == p) {
      swap(G[v][j], G[v].back());
      G[v].pop_back();
      --j;
      continue;
    }
    dfs_sz(u, i);
    sz[v] += sz[u];
    if (sz[u] > sz[G[v][0].first])
      swap(G[v][0], G[v][j]);
  }
}

void dfs_dp(int v, int w, int p, vector<int>& ans, int cur) {
  if (!G[v].empty()) {
    dfs_dp(G[v][0].first, w, G[v][0].second, ans, cur);
    for (int j = 1; j < G[v].size(); ++j) {
      auto [u, i] = G[v][j];
      dfs_dp(u, w + 1, i, ans, cur);
      for (int t : vertices[w + 1]) {
        vertices[w].emplace_back(t);
        for (int y : vals[t]) {
          move(rights[w + 1], lefts[w + 1], y);
          move(lefts[w], rights[w], y);
        }
      }
      lefts[w + 1].rebuild();
      vertices[w + 1].clear();
    }
  }
  for (int x : vals[v])
    move(lefts[w], rights[w], x);
  vertices[w].emplace_back(v);
  if (p != -1)
    ans[p] += lefts[w].get_mx() + rights[w].get_mx() - cur;
}

//#define STRESS

void solve() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> edges(m);
  vector<int> A(n);
  for (int i = 0; i < n; ++i) {
#ifdef STRESS
    A[i] = rand() % n;
#else
    cin >> A[i];
#endif
  }
  for (int i = 0; i < m; ++i) {
    int a, b;
#ifdef STRESS
    a = rand() % (i + 1);
    b = i + 1;
#else
    cin >> a >> b;
    --a;
    --b;
#endif
    edges[i] = make_pair(a, b);
    G[a].emplace_back(b, i);
    G[b].emplace_back(a, i);
  }
  ttime = 0;
  fill(tin, tin + n, -1);
  fill(is_bridge, is_bridge + m, false);
  for (int i = 0; i < n; ++i) {
    if (tin[i] == -1) {
      dfs1(i, -1);
    }
  }
  int comp_cnt = 0;
  fill(col, col + n, -1);
  for (int i = 0; i < n; ++i) {
    if (col[i] == -1) {
      dfs2(i, comp_cnt);
      ++comp_cnt;
    }
  }
  for (int i = 0; i < n; ++i)
    G[i].clear();
  for (int i = 0; i < n; ++i)
    vals[col[i]].emplace_back(A[i]);

  vector<int> ans(m);
  for (int i = 0; i < m; ++i) {
    if (is_bridge[i]) {
      auto [u, v] = edges[i];
      u = col[u];
      v = col[v];
      G[u].emplace_back(v, i);
      G[v].emplace_back(u, i);
    }
  }
  int total_add = 0;
  fill(sz, sz + n, 0);
  for (int i = 0; i < comp_cnt; ++i) {
    if (sz[i] != 0)
      continue;
    dfs_sz(i, -1);
    map<int, int> cnt;
    for (int v : comp) {
      for (int x : vals[v])
        ++cnt[x];
    }
    vector<pair<int, int>> vvals;
    for (auto [x, c] : cnt)
      vvals.emplace_back(c, x);
    sort(vvals.rbegin(), vvals.rend());
    for (int j = 0; j < K; ++j) {
      lefts[j].setup(vvals);
    }

    int cur = lefts[0].get_mx();
    total_add += cur;
    dfs_dp(i, 0, -1, ans, cur);
    vertices[0].clear();

    for (int v : comp) {
      for (int x : vals[v]) {
        rights[0].erase(x);
      }
    }
    comp.clear();
  }

  for (int i = 0; i < comp_cnt; ++i)
    vals[i].clear();
  for (int i = 0; i < comp_cnt; ++i)
    G[i].clear();
  for (int i = 0; i < m; ++i)
    cout << ans[i] + total_add << (i + 1 == m ? '\n' : ' ');
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 20012kb

input:

3
7 5
1 2 1 2 1 2 1
1 2
1 3
2 4
5 6
5 7
3 3
1 2 3
1 2
1 3
2 3
2 3
12345 54321
1 2
1 2
1 1

output:

6 5 5 5 4
1 1 1
1 1 1

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 446ms
memory: 53316kb

input:

5557
2 7
79960 79960
2 2
1 1
1 1
2 2
1 1
2 1
1 2
9 8
21881 70740 70740 21881 22458 22458 639 21881 70740
3 3
1 6
5 8
7 5
5 7
2 3
5 1
7 6
6 7
13064 20716 6746 13064 6746 69225
5 5
4 1
4 1
1 6
4 5
3 2
3 2
8 4
45146 14400 45146 45146 14400 72969 14400 45146
8 6
1 3
4 6
8 3
18 13
48132 37949 92338 92338...

output:

2 2 2 2 2 2 2
6 6 7 6 6 6 6 6
3 3 3 4 4 3 3
7 7 7 7
9 9 9 8 9 8 9 8 9 9 10 9 9
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
7 8
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
9 10 9
16 16 16 16 16 17 16 16
10 10 11 10 12 11 10 10 10 10 10 10 10 12 10 10 10 10 10 11
10 9 9 9 9 9 9 9 9 9 9 9 9 9 10 ...

result:

wrong answer 28th lines differ - expected: '8 7 7 8 8 8 8 7 8 8 8 8 7 7 7 7 7 7 7 7', found: '7 7 7 8 8 8 8 7 8 8 8 7 7 7 7 7 7 7 7 7'