一道将启发式合并和哈希结合的好题,不过由于自己写的太差,提交了十多发才过(手写哈希表真的比 unordered_map 快好多)。
看到题目,我们发现,如果 两点要成为点对,那么它们在所有图上都连通,而图的连通性问题我们能想到用并查集来解决。那么这个描述就可以改进为两点的并查集祖先相同。我们知道,如果每次修改暴力枚举所有点,寻找它们每个图并查集祖先都相同那肯定会 T 飞,但是每次修改的答案可以基于上一次的答案做修改——对于每个修改的点,减去原来和它是点对的点的个数乘二(因为是点对),加上现在和它是点对的点的个数乘二就是答案。现在的问题是如何快速找到所有和它是点对的点,我们想到 hash,这里有个巧妙的做法,我们给每个图每个点都随机一个点权,每个点在每个图的所有祖先的点权加到一起,就是它判断与其他点是否是点对的条件。但是这个方法还是有些坑的,rand() 生成的随机数质量不足,总是会有几个点 WA 掉,所以这里我们使用 mt19937 生成质量高的随机数(甚至比 rand() 还快),具体的使用方法可以看 OI Wiki ,这里不多讲述(其实是不会)。当然坑点不止一个,取模的数最好不要太小,范围比 int 大也是可以的;一次随机可能不够保险,你可以随机两次再相乘。
我们把一个点在每个图的祖先(最开始是它自己)的点权加在一起,存在 unordered_map 或者哈希表里(前者写得少,后者跑得快),使其初始值为 1,(因为图最开始都没连通),答案初值设为点数,然后就可以开始执行操作。
将读入的两个点()的父亲作比较,这里不用调用函数而是直接用父亲的原因是之后点的修改都会让它的父亲直接指向它并查集深度最小的祖先,相当于已经路径压缩好了,它的父亲就是并查集的祖先。相等直接输出答案跳到下一个,否则就让 的点集(这里用 vector 存储)的所有点修改贡献之后将父亲改为 的父亲(其实这里的“父亲”指的是并查集的数组 fa,它的值就是这个点的“祖先”),然后加入 的点集,最后清空 的点集防止 MLE。
我们先不提如何修改贡献,而是观察一下,这个方法是会超时的,原因是你可能将一个很大的点集全部修改了而不去修改较小的点集。这个问题我们可以使用启发式合并来解决,这样可以保证时间复杂度是 的。原因是你这个点每次被枚举后所在的点集至少会增大一倍,点集大小:,所以是 的。所以我们在修改点集之前让被修改的点集大小更小就可以了。
最后谈一谈怎么修改贡献,其实这个很简单。原来的哈希值对应的点的个数先减一,再让答案减去其的两倍,修改哈希值后,让答案加上新的哈希值对应的点的个数的两倍,在让个数加一就完成了。
这里的数组除了哈希值和 unordered_map 或者哈希标的下标,其他都可以开 int,不然可能爆空间。
最后给出代码:
使用了 unordered_map 的代码:
//Just Sayori
#pragma GCC diagnostic ignored "-Wnarrowing"
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <random>
#include <vector>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define rnt register int
#define gr getchar
#define pr putchar
#define N 500005
#define M 1000000007
using namespace std;
inline ll read()
{
ll x = 0, f = 1;
char ch = gr();
while (ch < '0' || ch > '9') ch == '-' ? f = -1, ch = gr() : ch = gr();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = gr();
return x * f;
}
inline void write(ll x)
{
static int top = 0, sta[39];
if (x < 0) pr('-'), x = -x;
do sta[++top] = x % 10, x /= 10;
while (x);
while (top) pr(sta[top--] ^ 48);
}
int d, n, m, a, b, k, ans;
ll g[5005];
int w[5005][205], fa[5005][205], sz[5005][205];
vector<int> v[5005][205];
unordered_map<ll, int> mp;
int main()
{
mt19937 myrand(time(0));
d = read(), n = read(), m = read(), ans = n;
for (rnt i = 1; i <= n; mp[g[i]]++, i++)
for (rnt j = 1; j <= d; j++)
{
w[i][j] = (ll)myrand() * (ll) myrand() % 1145141919810;
fa[i][j] = i;
sz[i][j] = 1;
v[i][j].push_back(i);
g[i] += w[i][j];
}
for (rnt i = 1; i <= m; i++)
{
a = read(), b = read(), k = read();
int fx = fa[a][k], fy = fa[b][k];
if (fx == fy)
{
write(ans), pr(10);
continue;
}
if (sz[fx][k] < sz[fy][k]) swap(fx, fy);
fa[fy][k] = fx;
sz[fx][k] += sz[fy][k];
for (rnt j : v[fy][k])
{
ans -= (--mp[g[j]]) * 2;
g[j] -= w[fy][k];
g[j] += w[fx][k];
fa[j][k] = fx;
ans += (mp[g[j]]++) * 2;
v[fx][k].push_back(j);
}
v[fy][k].clear();
write(ans), pr(10);
}
return 0;
}使用了丑陋写法的哈希表的代码:
//Just Sayori
#pragma GCC diagnostic ignored "-Wnarrowing"
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <random>
#include <vector>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define rnt register int
#define gr getchar
#define pr putchar
#define N 500005
#define M 10000005
using namespace std;
inline ll read()
{
ll x = 0, f = 1;
char ch = gr();
while (ch < '0' || ch > '9') ch == '-' ? f = -1, ch = gr() : ch = gr();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = gr();
return x * f;
}
inline void write(ll x)
{
static int top = 0, sta[39];
if (x < 0) pr('-'), x = -x;
do sta[++top] = x % 10, x /= 10;
while (x);
while (top) pr(sta[top--] ^ 48);
}
struct edge
{
ll x;
int y, next;
} e[M << 1];
int head[M], cnt;
int d, n, m, a, b, k, ans;
ll g[5005];
int w[5005][205], fa[5005][205], sz[5005][205];
vector<int> v[5005][205];
inline ll get_map(ll x)
{
if (x < 0) x = -x;//原来的负数太大,这里随便写了一句就 A 了,所以没有修改。
return (x + M) % M;
}
int query(ll x)
{
for (rnt i = head[get_map(x)]; i; i = e[i].next)
if (e[i].x == x)
return e[i].y;
return 0;
}
void modify(ll x, int w)
{
for (rnt i = head[get_map(x)]; i; i = e[i].next)
if (e[i].x == x)
return e[i].y += w, void();
e[++cnt] = {x, w, head[get_map(x)]}, head[get_map(x)] = cnt;
}
int main()
{
mt19937 myrand(time(0));
d = read(), n = read(), m = read(), ans = n;
for (rnt i = 1; i <= n; modify(g[i], 1), i++)
for (rnt j = 1; j <= d; j++)
{
w[i][j] = (ull)myrand() * (ull) myrand() % 1145141919810;
fa[i][j] = i;
sz[i][j] = 1;
v[i][j].push_back(i);
g[i] += w[i][j];
}
for (rnt i = 1; i <= m; i++)
{
a = read(), b = read(), k = read();
int fx = fa[a][k], fy = fa[b][k];
if (fx == fy)
{
write(ans), pr(10);
continue;
}
if (sz[fx][k] < sz[fy][k]) swap(fx, fy);
fa[fy][k] = fx;
sz[fx][k] += sz[fy][k];
for (rnt j : v[fy][k])
{
modify(g[j], -1);
ans -= query(g[j]) * 2;
g[j] -= w[fy][k];
g[j] += w[fx][k];
fa[j][k] = fx;
ans += query(g[j]) * 2;
modify(g[j], 1);
v[fx][k].push_back(j);
}
v[fy][k].clear();
write(ans), pr(10);
}
return 0;
}