CodeForces 506D Mr. Kitayuta's Colorful Graph
brute force ? 其实是平方分解。很容易想到的是每一个颜色建一个图,然后并查集维护一下连通性。
问题在于颜色有O(m)种,每种颜色的图点数都是O(n)的,因此并查集的空间只能重复利用。
但是可以把以O(m)的空间把有用的连通块信息保留下来。
之后的处理可以借鉴分块的思想。
记点v属于的连通块数量为b(v),对于询问x,y ,根据点所在的连通块信息,可以以O(max(b(x),b(y)))的时间回答出来。
设置一个阀值B,对于b(v)>B,提前预处理,小于B的就暴力回答。
因为一条边最多增加两个b(v)值,所有b(v)的和是O(m)的。 最多有m/B个v满足b(v)大于B,对于每个这样的v,O(m)历遍,O(m)的空间记录答案。
两部分的复杂度是O(m/B*m + B*q),类似分块的取法,取B = m/sqrt(q),复杂度为O(m*sqrt(q))。
/*********************************************************
* --------------Alfheim-------------- *
* author AbyssalFish *
**********************************************************/
#include<bits/stdc++.h>
using namespace std; typedef long long ll; const int maxn = 1e5+, maxm = 1e5+; int pa[maxn], rak[maxn]; int fd(int x)
{
return pa[x]? pa[x] = fd(pa[x]): x;
} inline void joint(int a,int b)
{
int x = fd(a), y = fd(b);
if(x != y){
if(rak[x] < rak[y]){
pa[x] = y;
}
else {
pa[y] = x;
if(rak[x] == rak[y]) rak[x]++;
}
}
} int a[maxm],b[maxm];
bool vis[maxn]; int hd_c[maxn], nx_e[maxm]; inline void add_e(int cl,int i)
{
nx_e[i] = hd_c[cl];
hd_c[cl] = i;
} typedef vector<int> Block;
typedef vector<int> v_int; vector<Block> blc;
Block tmp[maxn];
v_int in_blk[maxn]; inline void add_blc(int x)
{
if(!vis[x]){
vis[x] = true;
tmp[fd(x)].push_back(x);
}
} inline void dump(int x)
{
if(tmp[x].size() > ){
blc.push_back(tmp[x]);
tmp[x].clear();
}
if(vis[x]){
vis[x] = false;
rak[x] = pa[x] = ;
}
} const uint32_t MAXB = ; int ans[MAXB+][maxn];
int id[maxn]; //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
//cout<<(2*maxm/sqrt(2*maxm));
int n,m,q;
scanf("%d%d",&n,&m);
uint32_t B = floor(*m/sqrt(*m));
int i,j;
for(i = ; i <= m; i++){
scanf("%d%d%d",a+i,b+i,&j);
add_e(j,i);
} for(i = ; i <= m; i++){
for(j = hd_c[i]; j; j = nx_e[j]){
joint(a[j],b[j]);
}
for(j = hd_c[i]; j; j = nx_e[j]){
add_blc(a[j]);
add_blc(b[j]);
}
for(j = hd_c[i]; j; j = nx_e[j]){
dump(a[j]);
dump(b[j]);
}
} for(i = ; i < (int)blc.size(); i++){
for(auto v: blc[i]){
in_blk[v].push_back(i);
}
} int id_cnt = ;
for(i = ; i <= n; i++){
if(in_blk[i].size() > B){
id[i] = ++id_cnt;
for(auto b_id: in_blk[i]){
for(auto v: blc[b_id]){
ans[id_cnt][v]++;
}
}
}
} scanf("%d",&q);
while(q--){
int x,y; scanf("%d%d",&x,&y);
int res;
if(id[x]) res = ans[id[x]][y];
else if(id[y]) res = ans[id[y]][x];
else {
res = i = j = ;
v_int &X = in_blk[x], &Y = in_blk[y];
n = X.size(); m = Y.size();
while(i < n && j < m){
if(X[i] == Y[j]){
res++; i++; j++;
}
else {
X[i] < Y[j] ? i++ : j++;
}
}
}
printf("%d\n",res);
} return ;
}
CodeForces 506D Mr. Kitayuta's Colorful Graph的更多相关文章
- Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)
题目链接 Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$ 涉及的点的个数 $<= ...
- CodeForces 505B Mr. Kitayuta's Colorful Graph
Mr. Kitayuta's Colorful Graph Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- codeforces 505B Mr. Kitayuta's Colorful Graph(水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...
- CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集
Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集
D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph
D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...
- B. Mr. Kitayuta's Colorful Graph
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second Mr. Kitayuta has just bought an undi ...
随机推荐
- Paw —— 比Postman更舒服的API利器
特点: 颜值高本地应用,流畅有收藏夹,管理请求可使用环境变量.比如用来一键切换开发环境请求和线上环境请求.即不同环境的同个接口只有host不一样,其它都是一样的,所以就把host抽离出来弄成一个环境变 ...
- NETCore 调试
https://www.cnblogs.com/MingQiu/p/8227644.html https://www.cnblogs.com/shumin/p/9967854.html 前言 core ...
- ubuntu hadoop集群 master免密码登陆到slave节点
1. 在master节点上安装ssh client,在slave节点上安装ssh server sudo apt-get install openssh-client sudo apt-get ins ...
- Zookeeper---初识
1.Zookeeper是 Apache开源 的 分布式应用程序 服务治理: 在分布式环境中 协调和管理服务 是一个复杂的过程: ZooKeeper通过其简单的架构和API解决了这个问题: Zo ...
- Not so Mobile UVA - 839
题目链接:https://vjudge.net/problem/UVA-839 题目大意:输入一个树状天平,根据力矩相等原则,判断是否平衡. 如上图所示,所谓力矩相等,就是Wl*Dl=Wr*Dr. ...
- 在SQL Server中创建用户角色及授权(使用SQL语句)
1. 首先在 SQL Server 服务器级别,创建登陆帐户(create login) --创建登陆帐户(create login) create login dba with password=' ...
- [转]FireFox与IE 下js兼容触发click事件的代码
本文转自:http://www.jb51.net/article/16549.htm FireFox与IE 下js兼容触发click事件 ,对于需要兼容这两者的朋友,就需要参考下下面的代码了<a ...
- Numpy的那些事儿
2 NumPy-快速处理数据 标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针.这样为了保存一个简单的[1, ...
- Kure讲HTML_HTML界面结构
1.HTML界面结构: 通常通过html开发的网页,它有一个自己固定的书写格式(类似于写信的时候也有固定的格式) <!-- DOCTYPE用来告诉浏览器用当前html文档是用html的哪个版本编 ...
- CBoard数据分析实战
介绍 CBoard由上海楚果信息技术有限公司主导开源, 它不仅仅是一款自助BI数据分析产品, 还是开放的BI产品开发平台: 用户只需简单妥妥拽拽就能自助完成数据多维分析与报表设计 开发者能够简单扩展连 ...