QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#309065#6127. Kawa ExamAlex_WeiAC ✓822ms32168kbC++144.5kb2024-01-20 14:33:522024-01-20 14:33:52

Judging History

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

  • [2024-01-20 14:33:52]
  • 评测
  • 测评结果:AC
  • 用时:822ms
  • 内存:32168kb
  • [2024-01-20 14:33:52]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdi = pair<double, int>;
using pdd = pair<double, double>;
using ull = unsigned long long;
using LL = __int128_t;

#define ppc(x) __builtin_popcount(x)
#define clz(x) __builtin_clz(x)

bool Mbe;

constexpr int mod = 1e9 + 7;
void addt(int &x, int y) {
  x += y, x >= mod && (x -= mod);
}
int add(int x, int y) {
  return x += y, x >= mod && (x -= mod), x;
}

struct FastMod {
  ull b, m;
  FastMod(ull b = 1) : b(b), m(ull((LL(1) << 64) / b)) {}
  ull reduce(ull a) {
    ull q = ull((LL(m) * a) >> 64);
    ull r = a - q * b; // can be proven that 0 <= r < 2 * b
    return r >= b ? r - b : r;
  }
} R;

// ---------- templates above ----------

constexpr int K = 17;
constexpr int N = 1e5 + 5;

vector<pii> e[N];
int n, m, a[N], vis[N], pic[N], f[N];
int tot, res[N], ans[N];
struct bucket {
  int maj, buc[N], occ[N], cnt, ap[N], vis[N];
  void init() {
    occ[0] = N;
    for(int i = 0; i <= maj; i++) occ[i] = 0;
    for(int i = 1; i <= cnt; i++) vis[ap[i]] = buc[ap[i]] = 0;
    cnt = maj = 0;
  }
  void upd(int x, int v) {
    if(!vis[x]) ap[++cnt] = x, vis[x] = 1;
    if(v == 1) {
      occ[buc[x]]--, occ[++buc[x]]++;
      if(occ[maj + 1]) maj++;
    }
    else {
      occ[buc[x]]--, occ[--buc[x]]++;
      if(!occ[maj]) maj--;
    }
  }
} in, out;

int dn, dfn[N], rev[N], mi[K][N];
int fa[N], sz[N], son[N];
int get(int x, int y) {
  return dfn[x] < dfn[y] ? x : y;
}
int lca(int u, int v) {
  if(u == v) return u;
  if((u = dfn[u]) > (v = dfn[v])) swap(u, v);
  int d = __lg(v - u++);
  return get(mi[d][u], mi[d][v - (1 << d) + 1]);
}
void dfs(int id, int ff) {
  // cerr << "dfs " << id << " " << ff << endl;
  vis[id] = sz[id] = 1;
  dfn[id] = ++dn, rev[dn] = id;
  mi[0][dn] = fa[id] = ff;
  in.upd(a[id], 1);
  for(pii _ : e[id]) {
    int it = _.first;
    if(vis[it]) continue;
    pic[_.second] = 1, dfs(it, id);
    sz[id] += sz[it];
    if(sz[son[id]] < sz[it]) son[id] = it;
  }
}
void count(int x, int v) {
  for(int p = dfn[x]; p < dfn[x] + sz[x]; p++) {
    in.upd(a[rev[p]], v);
    out.upd(a[rev[p]], -v);
  }
}
void dfs2(int id, int res) {
  // cerr << "dfs2 " << id << " " << fa[id] << " " << son[id] << " " << res << endl;
  int sonid = 0;
  for(pii _ : e[id]) {
    int it = _.first, eid = _.second;
    if(!pic[eid] || it == fa[id]) continue;
    if(it == son[id]) {sonid = eid; continue;}
    dfs2(it, res), f[id] += f[it];
    if(!f[it]) ans[eid] = tot - res + in.maj + out.maj;
    count(it, -1);
  }
  if(son[id]) {
    dfs2(son[id], res), f[id] += f[son[id]];
    if(!f[son[id]]) ans[sonid] = tot - res + in.maj + out.maj;
  }
  for(pii _ : e[id]) {
    int it = _.first;
    if(pic[_.second] && it != fa[id] && it != son[id]) count(it, 1);
  }
  in.upd(a[id], 1), out.upd(a[id], -1);
}
void solve() {
  cin >> n >> m, dn = tot = 0;
  for(int i = 1; i <= n; i++) {
    vis[i] = f[i] = son[i] = 0;
    e[i].clear();
  }
  for(int i = 1; i <= m; i++) pic[i] = ans[i] = 0;
  for(int i = 1; i <= n; i++) cin >> a[i];
  for(int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    e[u].push_back({v, i});
    e[v].push_back({u, i});
  }
  for(int i = 1; i <= n; i++) {
    if(!vis[i]) {
      in.init(), dfs(i, 0);
      tot += res[i] = in.maj;
    }
  }
  for(int i = 1; i <= __lg(n); i++) {
    for(int j = 1; j + (1 << i) - 1 <= n; j++) {
      mi[i][j] = get(mi[i - 1][j], mi[i - 1][j + (1 << i - 1)]);
    }
  }
  for(int i = 1; i <= n; i++) {
    for(pii _ : e[i]) {
      int it = _.first;
      if(i > it || pic[_.second]) continue;
      f[i]++, f[it]++, f[lca(i, it)] -= 2;
    }
  }
  for(int i = 1; i <= n; i++) {
    if(fa[i]) continue;
    in.init(), out.init();
    for(int p = dfn[i]; p < dfn[i] + sz[i]; p++) out.upd(a[rev[p]], 1);
    dfs2(i, res[i]);
  }
  for(int i = 1; i <= m; i++) {
    if(ans[i]) cout << ans[i];
    else cout << tot;
    if(i == m) cout << '\n';
    else cout << ' ';
  }
}

bool Med;
signed main() {
  fprintf(stderr, "%.3lf MB\n", (&Mbe - &Med) / 1048576.0);
  // ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  #ifdef ALEX_WEI
    FILE* IN = freopen("1.in", "r", stdin);
    FILE* OUT = freopen("1.out", "w", stdout);
  #endif
  int T = 1;
  cin >> T;
  while(T--) solve();
  fprintf(stderr, "%.3lf ms\n", 1e3 * clock() / CLOCKS_PER_SEC);
  return 0;
}

/*
g++ a.cpp -o a -std=c++14 -O2 -DALEX_WEI
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 822ms
memory: 32168kb

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:

ok 5557 lines

Test #3:

score: 0
Accepted
time: 603ms
memory: 29988kb

input:

10
100000 99999
3983 3983 20157 97983 20157 20157 3983 3983 97983 20157 20157 3983 97983 20157 3983 20157 20157 3983 3983 3983 97983 97983 20157 3983 3983 97983 20157 97983 20157 97983 3983 97983 97983 3983 20157 3983 20157 20157 97983 3983 3983 3983 3983 97983 97983 3983 97983 97983 3983 20157 3983...

output:

33392 33393 33393 33393 33393 33392 33392 33393 33393 33393 33392 33393 33393 33392 33393 33393 33392 33392 33392 33393 33393 33393 33392 33392 33393 33393 33393 33393 33393 33392 33393 33393 33392 33393 33392 33393 33393 33393 33392 33392 33392 33392 33393 33393 33392 33393 33393 33392 33393 33392 ...

result:

ok 10 lines

Test #4:

score: 0
Accepted
time: 584ms
memory: 29892kb

input:

10
100000 99999
27534 27534 3780 3780 27534 53544 27534 3780 3780 53544 53544 27534 53544 53544 3780 3780 3780 3780 53544 27534 3780 3780 53544 27534 27534 53544 27534 27534 53544 27534 27534 27534 3780 27534 27534 3780 3780 3780 27534 53544 3780 53544 27534 3780 3780 3780 27534 27534 27534 3780 275...

output:

33613 33601 33601 33600 33600 33601 33601 33601 33600 33601 33600 33600 33601 33601 33601 33601 33601 33601 33600 33600 33601 33601 33601 33601 33600 33601 33601 33600 33601 33600 33601 33600 33601 33601 33601 33601 33600 33601 33601 33601 33601 33601 33601 33601 33601 33601 33600 33601 33600 33601 ...

result:

ok 10 lines

Test #5:

score: 0
Accepted
time: 587ms
memory: 24872kb

input:

10
100000 99999
92499 92270 92270 92499 92499 92499 92270 54017 92270 92270 92270 54017 54017 54017 54017 92270 92499 54017 92270 54017 92499 92499 92270 92270 54017 54017 54017 54017 92270 92270 92499 54017 54017 92499 92499 54017 92270 92270 54017 92499 92270 92270 54017 54017 54017 92499 92499 54...

output:

33506 33482 33507 33482 33508 33483 33508 33483 33508 33483 33507 33483 33506 33483 33505 33483 33503 33483 33503 33482 33504 33483 33505 33483 33504 33483 33502 33483 33501 33483 33500 33482 33502 33483 33500 33483 33501 33482 33502 33483 33501 33483 33500 33482 33500 33483 33498 33483 33499 33483 ...

result:

ok 10 lines

Test #6:

score: 0
Accepted
time: 617ms
memory: 24924kb

input:

10
100000 99999
76207 76207 88551 88551 98176 76207 98176 88551 88551 98176 88551 76207 76207 98176 98176 76207 76207 88551 76207 88551 76207 88551 88551 76207 88551 76207 98176 88551 76207 98176 88551 88551 76207 88551 98176 88551 76207 76207 98176 88551 76207 98176 76207 88551 88551 88551 88551 76...

output:

33484 33484 33476 33484 33477 33485 33476 33485 33477 33485 33477 33486 33477 33484 33477 33485 33476 33485 33476 33485 33476 33483 33477 33483 33477 33485 33476 33485 33477 33485 33476 33487 33476 33487 33476 33486 33477 33486 33476 33486 33477 33486 33476 33486 33476 33486 33477 33487 33477 33487 ...

result:

ok 10 lines

Test #7:

score: 0
Accepted
time: 592ms
memory: 24284kb

input:

10
100000 99999
70486 49904 70486 49904 87935 49904 49904 87935 87935 49904 49904 87935 49904 87935 87935 70486 49904 87935 87935 49904 70486 87935 49904 70486 87935 87935 49904 49904 49904 87935 70486 70486 70486 49904 70486 87935 87935 87935 70486 87935 70486 49904 87935 49904 49904 87935 70486 87...

output:

33491 33486 33489 33486 33489 33486 33489 33486 33487 33486 33487 33486 33486 33485 33486 33486 33486 33486 33485 33486 33485 33485 33486 33486 33485 33486 33485 33486 33485 33485 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33485 33486 33485 ...

result:

ok 10 lines

Test #8:

score: 0
Accepted
time: 594ms
memory: 24284kb

input:

10
100000 99999
98004 33580 98004 98004 98004 92291 92291 98004 98004 92291 92291 33580 98004 92291 33580 98004 98004 33580 98004 92291 92291 33580 92291 92291 98004 33580 98004 33580 33580 98004 33580 92291 33580 33580 92291 92291 92291 98004 33580 98004 92291 92291 33580 92291 98004 98004 92291 92...

output:

33462 33463 33421 33463 33422 33465 33421 33463 33422 33464 33422 33462 33422 33464 33421 33464 33422 33464 33422 33465 33422 33463 33422 33462 33422 33463 33422 33465 33421 33464 33422 33464 33422 33463 33422 33463 33421 33463 33421 33462 33422 33460 33422 33461 33421 33461 33422 33460 33422 33459 ...

result:

ok 10 lines