QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#139010 | #6127. Kawa Exam | thenymphsofdelphi | RE | 0ms | 0kb | C++20 | 6.0kb | 2023-08-12 16:13:51 | 2023-08-12 16:13:53 |
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 |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
詳細信息
Test #1:
score: 0
Dangerous Syscalls
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