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的更多相关文章

  1. Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)

    题目链接  Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$    涉及的点的个数 $<= ...

  2. CodeForces 505B Mr. Kitayuta's Colorful Graph

    Mr. Kitayuta's Colorful Graph Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  3. codeforces 505B Mr. Kitayuta's Colorful Graph(水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...

  4. CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集

    Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...

  5. 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/ ...

  6. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  7. 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 ...

  8. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph

    D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...

  9. 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 ...

随机推荐

  1. How to Setup MySQL (Master-Slave) Replication in CentOS

    The following tutorial aims to provide you a simple step-by-step guide for setting up MySQL (Master- ...

  2. NETCORE 之 openSUSE docker 安装

    openSUSE docker 安装https://www.jianshu.com/p/c725a06447d5 http://www.importnew.com/24684.htmlSuse安装Do ...

  3. linux下lua运行环境安装

    1.下载安装包: [root@H0f ~]# wget  http://www.lua.org/ftp/lua-5.2.4.tar.gz    http://www.lua.org/ftp/lua-5 ...

  4. firewall 端口转发

    centos 7 使用背景:某次新购阿里云服务器安装nginx后配置80转8080的内部转发 systemctl status firewalld ---查看守护进程状态systemctl start ...

  5. HandlerInterceptor拦截器使用总结

    简介 SpringMVC的处理器拦截器,类似于Servlet开发中的过滤器Filter,用于对请求进行拦截和处理. 常见应用场景 1.权限检查:如检测请求是否具有登录权限,如果没有直接返回到登陆页面. ...

  6. python迭代器、生成器、列表推倒式

    创建迭代器: iter( ): 创建迭代器 next( ): 返回迭代器的下一个element(元素) 实例题: >>> list = [1,2,3,4] >>> ...

  7. Oracle基础篇--01数据库控制语言DCL

    数据库控制语言,是用户对数据的权限控制语言. 通过GRANT语句进行赋权,通过REVOKE撤回权限.数据库的权限包括2种,一种是数据库系统权限,一种是数据库对象权限.在控制语言里面,存在2个概念, 1 ...

  8. tcp的半连接与完全连接队列(三)源码分析

    TCP 协议中的 SYN queue 和 accept queue 处理 若要理解本文意图说明的问题,可能需要以下知识背景: listen 系统调用的 backlog 参数含义,以及与 net.cor ...

  9. Perf -- Linux下的系统性能调优工具,第 2 部分 特殊用法以及内核调优示例

    https://www.ibm.com/developerworks/cn/linux/l-cn-perf2/ 本文内容简介 之前介绍了 perf 最常见的一些用法,关注于 Linux 系统上应用程序 ...

  10. [转]JS跨域总结

    本文转自:http://www.cnblogs.com/qixuejia/archive/2012/08/29/2662220.html javascript跨域有两种情况: 1.基于同一父域的子域之 ...