题解:P9886 [ICPC 2018 Qingdao R] Kawa Exam
第
在联通块里面,自然是选择众数作为这些题的答案,贡献是众数出现次数。
所以算出每个联通块的众数出现次数并加起来,记为
如果联通块具有树的模型,是方便我们遍历或操作的,所以对联通块遍历成树并记录没用到的边,称为“无用边”。
考虑断什么边,答案是什么。考虑联通块
- 任意一条“无用边”,断掉任然联通,无影响。因为是树上的附加边,而树本身是联通的。
-
-
就考虑怎么判断一条边满足条件二、三。
因为
这样子做是
在树上统计子树的信息,可以使用树上启发式合并,运用到这题可以这么做:
- 使用树状数组代替桶。
-
- 现在,树状数组里就有完整的关于
u 子树的信息了,判断[1,\text{dfn}_u),[\text{dfn}_u+\text{size}_u,n] 是否有值。如果有,说明有连到子树外的边,否则就没有。 - 算完后,如果
u 是它父亲的轻儿子,删掉子树内所有贡献。
树上启发式合并是
现在我们就知道了哪些边断掉是会有新答案的。
那
在求子树的众数出现次数也是可以用树上启发式合并的,我们维护
求联通块
删边任然是是联通的那这条边的答案就是
#include <bits/stdc++.h>
bool AT;
#define vec vector
#define p_b push_back
#define pii pair <int, int>
#define rep(i, a, b) for(int i = a; i <= b; i++)
namespace IO {
template <typename type>
inline void rd(type &x) {
x = 0; int f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
x *= f;
}
template <typename type>
inline type rd() {
type x = 0; int f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
return x * f;
}
template <typename type>
inline void wr(type x) {
if(x < 0) x = -x, putchar('-');
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
inline void wr(char s) { putchar(s); }
inline void wr(const char *s) { std::cout << s; }
template <typename type>
inline void wbg(type x) { std::cerr << x; }
template <typename type, typename ...Args>
inline void rd(type &x, Args &...args) { rd(x); rd(args...); }
template <typename type, typename ...Args>
inline void wr(type x, Args ...args) { wr(x); wr(args...); }
template <typename type, typename ...Args>
inline void wbg(type x, Args ...args) { wbg(x); wbg(args...); }
} using namespace IO;
const int N = 1e5 + 10;
int st_time;
int T, n, m, a[N];
bool vis[N];
int idx, sz[N], dfn[N], rev[N], son[N], mde[N];
int ans, ANS[N];
std::map <std::pii, bool> edge;
std::vec <int> rt;
struct EDGE {
int tot, head[N], to[N << 1], id[N << 1], nex[N << 1];
inline void add(int u, int v, int i) {
to[++tot] = v, id[tot] = i, nex[tot] = head[u];
head[u] = tot;
}
} e[3]; // e[0] 是输入建的图,e[1] 是对图遍历形成的树,e[2] 是每个点的“无用边”
namespace OPERATOR {
int MODE; // 全局众数出现次数
bool Q[N]; // i 与父节点断边是不是脱离联通块
std::map <int, int> freq, cnt; // 如上所写
inline void add(int i) {
freq[cnt[a[i]]]--;
cnt[a[i]]++;
freq[cnt[a[i]]]++;
MODE = std::max(MODE, cnt[a[i]]);
}
inline void del(int i) {
freq[cnt[a[i]]]--;
if(MODE == cnt[a[i]] && !freq[cnt[a[i]]]) MODE--;
cnt[a[i]]--;
freq[cnt[a[i]]]++;
}
} using namespace OPERATOR;
namespace BIT {
int tr[N];
inline void add(int i, int v) {
for(; i <= n; i += i & (-i)) tr[i] += v;
}
inline int faq(int i, int res = 0) {
for(; i; i -= i & (-i)) res += tr[i];
return res;
}
inline int query(int l, int r) {
return l <= r ? faq(r) - faq(l - 1) : 0;
}
inline void update(int u, int w) {
for(int i = e[2].head[u]; i; i = e[2].nex[i]) add(dfn[e[2].to[i]], w);
}
} using namespace BIT;
void dfs0(int u, int fa) {
vis[u] = true;
for(int i = e[0].head[u]; i; i = e[0].nex[i]) {
int v = e[0].to[i], id = e[0].id[i];
if(v == fa) continue;
if(!vis[v]) {
e[1].add(u, v, id), e[1].add(v, u, id);
dfs0(v, u);
}
else {
if(edge.find({u, v}) == edge.end()) {
e[2].add(u, v, id), e[2].add(v, u, id);
edge[{u, v}] = edge[{v, u}] = true;
}
}
}
}
void dfs1(int u, int fa) {
sz[u] = 1, rev[dfn[u] = ++idx] = u;
for(int i = e[1].head[u]; i; i = e[1].nex[i]) {
int v = e[1].to[i];
if(v == fa) continue;
dfs1(v, u), sz[u] += sz[v];
if(sz[v] > sz[son[u]]) son[u] = v;
}
}
void dfs2(int root, int u, int fa, bool cl) {
for(int i = e[1].head[u]; i; i = e[1].nex[i]) {
int v = e[1].to[i];
if(v != fa && v != son[u]) dfs2(root, v, u, true);
}
if(son[u]) dfs2(root, son[u], u, false);
add(u), update(u, 1);
for(int i = e[1].head[u]; i; i = e[1].nex[i]) {
int v = e[1].to[i];
if(v != fa && v != son[u])
rep(j, dfn[v], dfn[v] + sz[v] - 1) add(rev[j]), update(rev[j], 1);
}
int l = dfn[u], r = dfn[u] + sz[u] - 1;
if(query(1, l - 1) == 0 && query(r + 1, n) == 0 && u != root) Q[u] = true;
mde[u] = MODE;
if(cl)
rep(i, l, r) del(rev[i]), update(rev[i], -1);
}
void dfs3(int root, int u, int fa, bool cl) {
int id = 0;
for(int i = e[1].head[u]; i; i = e[1].nex[i]) {
int v = e[1].to[i];
if(v == fa) id = e[1].id[i];
if(v != fa && v != son[u]) dfs3(root, v, u, true);
}
if(son[u]) dfs3(root, son[u], u, false);
del(u);
for(int i = e[1].head[u]; i; i = e[1].nex[i]) {
int v = e[1].to[i];
if(v != fa && v != son[u])
rep(j, dfn[v], dfn[v] + sz[v] - 1) del(rev[j]);
}
if(Q[u]) ANS[id] = ans - mde[root] + mde[u] + MODE;
if(cl)
rep(i, dfn[u], dfn[u] + sz[u] - 1) add(rev[i]);
}
inline void clear() {
edge.clear(), freq.clear(), cnt.clear(), rt.clear();
MODE = idx = ans = 0;
rep(i, 1, n) tr[i] = son[i] = vis[i] = Q[i] = false;
rep(i, 0, 2) {
e[i].tot = 0;
rep(j, 1, n) e[i].head[j] = 0;
}
}
inline void sol() {
clear(), rd(n, m);
rep(i, 1, n) rd(a[i]);
rep(i, 1, m) {
int u = rd<int>(), v = rd<int>();
e[0].add(u, v, i), e[0].add(v, u, i);
}
rep(i, 1, n)
if(!vis[i]) dfs0(i, 0), rt.p_b(i);
for(auto i : rt) dfs1(i, 0);
for(auto i : rt) dfs2(i, i, 0, true), ans += mde[i];
rep(i, 1, m) ANS[i] = ans;
// wbg("Use time: ", (clock() - st_time) / 1000.0, "s\n");
for(auto i : rt) {
MODE = 0;
freq.clear(), cnt.clear();
rep(j, dfn[i], dfn[i] + sz[i] - 1) add(rev[j]);
dfs3(i, i, 0, true);
}
rep(i, 1, m)
wr(ANS[i], (i != m) ? " " : (T ? "\n" : ""));
}
bool ED;
signed main() {
st_time = clock();
// wbg((&ED - &AT) / 1024.0 / 1024, "\n");
for(T = rd<int>(); T--; sol());
return 0;
}