QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#139011 | #6127. Kawa Exam | thenymphsofdelphi | ML | 0ms | 15764kb | C++20 | 6.0kb | 2023-08-12 16:14:16 | 2023-08-12 16:14:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
const int N = 1e5 + 5;
struct disjoint_set_union{
int par[N];
void reset(int sz = N){
fill(par, par + sz, -1);
}
int root(int x){
return par[x] < 0 ? x : (par[x] = root(par[x]));
}
bool merge(int x, int y){
if ((x = root(x)) == (y = root(y))){
return false;
}
if (par[x] > par[y]){
swap(x, y);
}
par[x] += par[y];
par[y] = x;
return true;
}
} dsu;
struct counter{
int cnt[N];
int cnt_cnt[N];
int ans;
void insert(int x){
cnt_cnt[cnt[x]]--;
cnt[x]++;
cnt_cnt[cnt[x]]++;
if (ans < cnt[x]){
ans = cnt[x];
}
}
void erase(int x){
cnt_cnt[cnt[x]]--;
cnt[x]--;
cnt_cnt[cnt[x]]++;
if (cnt_cnt[ans] == 0){
ans--;
}
}
} DS, DSout, DSin;
struct edge{
int u, v;
};
int n, m;
int color[N];
edge a[N];
vector <int> adj[N];
int ctrtime, time_in[N], low[N];
bool bridge[N];
void dfs_time(int u, int ip = -1){
time_in[u] = ++ctrtime;
low[u] = time_in[u];
for (auto i: adj[u]){
if (i == ip){
continue;
}
int v = u ^ a[i].u ^ a[i].v;
if (time_in[v] == 0){
dfs_time(v, i);
low[u] = min(low[u], low[v]);
}
else{
low[u] = min(low[u], time_in[v]);
}
}
if (low[u] == time_in[u] and ip != -1){
bridge[ip] = true;
}
}
int cnt_cpn;
int to_cpn[N];
vector <int> cpn_color[N];
int cnt_edge;
edge a2[N];
vector <int> adj2[N];
vector <int> sources;
int sz[N];
int ctrtour, tour[N], tin[N], tout[N];
void dfs_tour(int u, int ip = -1){
tour[++ctrtour] = u;
tin[u] = ctrtour;
sz[u] = (int)cpn_color[u].size();
for (auto i: adj2[u]){
if (i == ip){
continue;
}
int v = u ^ a2[i].u ^ a2[i].v;
dfs_tour(v, i);
sz[u] += sz[v];
}
tout[u] = ctrtour;
}
int ans[N];
void dfs_component(int u, int delta, counter& DS, int ip = -1){
if (delta == 1){
for (auto col: cpn_color[u]){
DS.insert(col);
}
}
else{
for (auto col: cpn_color[u]){
DS.erase(col);
}
}
for (auto i: adj2[u]){
if (i == ip){
continue;
}
int v = u ^ a2[i].u ^ a2[i].v;
dfs_component(v, delta, DS, i);
}
}
void dfs_mergeset(int u, int ip = -1, bool keep = false){
int heavy = -1;
for (auto i: adj2[u]){
if (i == ip){
continue;
}
int v = u ^ a2[i].u ^ a2[i].v;
if (heavy == -1 or sz[v] > sz[heavy]){
heavy = v;
}
}
for (auto i: adj2[u]){
if (i == ip){
continue;
}
int v = u ^ a2[i].u ^ a2[i].v;
if (v != heavy){
dfs_mergeset(v, i);
}
}
for (auto i: adj2[u]){
if (i == ip){
continue;
}
int v = u ^ a2[i].u ^ a2[i].v;
if (v == heavy){
dfs_mergeset(v, i, true);
}
}
for (auto col: cpn_color[u]){
DSout.erase(col);
DSin.insert(col);
}
for (auto i: adj2[u]){
if (i == ip){
continue;
}
int v = u ^ a2[i].u ^ a2[i].v;
if (v != heavy){
for (int t = tin[v]; t <= tout[v]; t++){
int node = tour[t];
for (auto col: cpn_color[node]){
DSout.erase(col);
DSin.insert(col);
}
}
}
}
if (ip != -1){
ans[ip] = ans[ip] - DS.ans + DSout.ans + DSin.ans;
}
if (not keep){
for (int t = tin[u]; t <= tout[u]; t++){
int node = tour[t];
for (auto col: cpn_color[node]){
DSin.erase(col);
DSout.insert(col);
}
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("KEK.inp", "r", stdin);
// freopen("KEK.out", "w", stdout);
int tests; cin >> tests; while (tests--){
cin >> n >> m;
ForE(u, 1, n){
cin >> color[u];
}
ForE(u, 1, n){
adj[u].clear();
}
ForE(i, 1, m){
int u, v;
cin >> u >> v;
a[i] = edge{u, v};
adj[u].emplace_back(i);
adj[v].emplace_back(i);
}
ctrtime = 0;
ForE(u, 1, n){
tin[u] = 0;
low[u] = 0;
bridge[u] = false;
}
ForE(u, 1, n){
if (tin[u] == 0){
dfs_time(u);
}
}
dsu.reset(n + 1);
cnt_cpn = 0;
ForE(i, 1, m){
if (not bridge[i]){
dsu.merge(a[i].u, a[i].v);
}
}
ForE(u, 1, n){
if (dsu.root(u) == u){
to_cpn[u] = ++cnt_cpn;
}
}
cnt_edge = 0;
ForE(x, 1, cnt_cpn){
cpn_color[x].clear();
adj2[x].clear();
}
ForE(u, 1, n){
to_cpn[u] = to_cpn[dsu.root(u)];
cpn_color[to_cpn[u]].emplace_back(color[u]);
}
ForE(i, 1, m){
if (bridge[i]){
int x = to_cpn[a[i].u], y = to_cpn[a[i].v];
a2[++cnt_edge] = edge{x, y};
adj2[x].emplace_back(cnt_edge);
adj2[y].emplace_back(cnt_edge);
}
}
sources.clear();
ctrtour = 0;
ForE(x, 1, cnt_cpn){
tour[x] = 0;
tin[x] = 0;
tout[x] = 0;
}
ForE(x, 1, cnt_cpn){
if (tin[x] == 0){
sources.emplace_back(x);
dfs_tour(x);
}
}
ans[1] = 0;
for (auto x: sources){
dfs_component(x, 1, DS);
ans[1] += DS.ans;
dfs_component(x, -1, DS);
}
ForE(i, 2, m){
ans[i] = ans[i - 1];
}
for (auto x: sources){
dfs_component(x, 1, DS);
dfs_component(x, 1, DSout);
dfs_tour(x);
dfs_mergeset(x);
dfs_component(x, -1, DS);
dfs_component(x, -1, DSout);
}
ForE(i, 1, m){
cout << ans[i] << " \n"[i == m];
}
}
}
/*
==================================================+
INPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15764kb
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
Memory Limit Exceeded
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...