QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#497138 | #6127. Kawa Exam | ucup-team1198# | AC ✓ | 420ms | 54892kb | C++20 | 8.1kb | 2024-07-28 19:53:37 | 2024-07-28 19:53:37 |
Judging History
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;
i = 0;
}
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
vector<int> solve(vector<int> A, vector<pair<int, int>> edges) {
int n = A.size();
int m = edges.size();
for (int i = 0; i < m; ++i) {
auto [a, b] = edges[i];
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)
ans[i] += total_add;
return ans;
}
vector<int> solve_dumb(vector<int> A, vector<pair<int, int>> edges) {
int n = A.size();
int m = edges.size();
vector<int> ans;
for (int without = 0; without < m; ++without) {
vector<vector<int>> G(n);
for (int i = 0; i < m; ++i) {
if (i == without)
continue;
auto [a, b] = edges[i];
G[a].emplace_back(b);
G[b].emplace_back(a);
}
int res = 0;
vector<bool> was(n, false);
for (int i = 0; i < n; ++i) {
if (was[i])
continue;
vector<int> Q;
Q.emplace_back(i);
was[i] = true;
int q_st = 0;
while (q_st < Q.size()) {
int v = Q[q_st];
++q_st;
for (int u : G[v]) {
if (!was[u]) {
was[u] = true;
Q.emplace_back(u);
}
}
}
map<int, int> cnt;
for (int v : Q)
++cnt[A[v]];
int mx = 0;
for (auto [x, c] : cnt)
mx = max(mx, c);
res += mx;
}
ans.emplace_back(res);
}
return ans;
}
//#define STRESS
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef STRESS
for (int t = 0; t < 100100; ++t) {
int n = rand() % 5 + 1;
int m = rand() % 7 + 1;
vector<int> A(n);
for (int i = 0; i < n; ++i)
A[i] = rand() % n;
vector<pair<int, int>> edges(m);
for (int i = 0; i < m; ++i) {
edges[i] = make_pair(rand() % n, rand() % n);
}
auto ans1 = solve(A, edges);
auto ans2 = solve_dumb(A, edges);
if (ans1 != ans2) {
cerr << "BUG!!!\n";
cerr << n << ' ' << m << '\n';
for (int i = 0; i < n; ++i)
cerr << A[i] << ' ';
cerr << '\n';
for (int i = 0; i < m; ++i) {
cerr << edges[i].first + 1 << ' ' << edges[i].second + 1 << '\n';
}
cerr << "wrong: ";
for (int x : ans1)
cerr << x << ' ';
cerr << "\nright: ";
for (int x : ans2)
cerr << x << ' ';
cerr << "\n\n\n";
}
}
#else
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<int> A(n);
for (int i = 0; i < n; ++i)
cin >> A[i];
vector<pair<int, int>> edges(m);
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
edges[i] = make_pair(a, b);
}
auto ans = solve(A, edges);
for (int i = 0; i < m; ++i)
cout << ans[i] << (i + 1 == m ? '\n' : ' ');
}
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17972kb
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: 420ms
memory: 54892kb
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: 241ms
memory: 38980kb
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: 224ms
memory: 39256kb
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: 270ms
memory: 34912kb
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: 265ms
memory: 34064kb
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: 244ms
memory: 33536kb
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: 257ms
memory: 33156kb
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