QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#497202 | #6127. Kawa Exam | ucup-team1198 | AC ✓ | 950ms | 83028kb | C++20 | 5.1kb | 2024-07-28 21:12:49 | 2024-07-28 21:12:49 |
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 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 << (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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 18004kb
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: 950ms
memory: 83028kb
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: 379ms
memory: 42188kb
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: 382ms
memory: 42108kb
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: 474ms
memory: 38048kb
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: 476ms
memory: 39416kb
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: 474ms
memory: 37036kb
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: 480ms
memory: 36340kb
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