QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#497198#6127. Kawa Examucup-team1198WA 3ms19948kbC++205.1kb2024-07-28 21:11:572024-07-28 21:11:57

Judging History

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

  • [2024-07-28 21:11:57]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:19948kb
  • [2024-07-28 21:11:57]
  • 提交

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 cnt[MAXN];
  multiset<int> S;

  void insert(int x) {
    ++cnt[x];
    S.emplace(cnt[x]);
    if (cnt[x] != 1)
      S.erase(S.find(cnt[x] - 1));
  }
  void erase(int x) {
    --cnt[x];
    S.erase(S.find(cnt[x] + 1));
    if (cnt[x])
      S.emplace(cnt[x]);
  }
  int get_mx() {
    if (S.empty())
      return 0;
    return *(--S.end());
  }
};

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

void move(Something& 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);
        }
      }
      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);
    vector<int> ccur;
    for (int v : comp) {
      for (int x : vals[v])
        ccur.emplace_back(x);
    }
    for (int j = 0; j < K; ++j) {
      for (int x : ccur)
        ++lefts[j].cnt[x];  
    }
    sort(ccur.begin(), ccur.end());
    ccur.resize(unique(ccur.begin(), ccur.end()) - ccur.begin());
    for (int j = 0; j < K; ++j) {
      for (int x : ccur)
        lefts[j].S.emplace(lefts[j].cnt[x]);  
    }

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

    for (int x : ccur)
      rights[0].cnt[x] = 0;
    rights[0].S.clear();
    for (int j = 1; j < K; ++j) {
      for (int x : ccur)
        lefts[j].cnt[x] = 0;
      lefts[j].S.clear();
    }
    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 << ' ';
  cout << '\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: 0
Wrong Answer
time: 3ms
memory: 19948kb

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:

wrong answer 1st lines differ - expected: '6 5 5 5 4', found: '6 5 5 5 4 '