题目链接  Mr. Kitayuta's Colorful Graph

把每种颜色分开来考虑。

所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$    涉及的点的个数 $<= \sqrt{n}$

对于第一种颜色,并查集缩点之后对每个询问依次处理过来若两点连通则答案加一。

对于第二种颜色,并查集缩点之后对该颜色涉及的所有点两两之间判断是否连通,

若连通则另外开一个$map$记录答案。

最后把两个部分的答案加起来即可。

细节问题  由于每种颜色处理完之后并查集都要重新初始化,对于第一种颜色的做法,只要$memset$即可。

第二种颜色总数可能较多,所以把之前并查集的操作撤销即可。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second typedef pair <int, int> PII; const int N = 1e5 + 10; struct node{ int x, y, ans; } q[N]; unordered_map <int, int> mp[N];
map <PII, int> id, ret;
vector <int> v[N];
vector <node> e[N];
stack <PII> s;
int n, m, qu, cnt, line;
int father[N], c[N]; int getfather(int x){ return father[x] ? father[x] = getfather(father[x]) : x; } int gf(int x){
if (father[x]){
s.push(MP(x, father[x]));
father[x] = getfather(father[x]);
return father[x];
} else return x;
} int main(){ scanf("%d%d", &n, &m);
rep(i, 1, m){
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
if (x > y) swap(x, y);
mp[z][x] = mp[z][y] = 1;
e[z].push_back({x, y});
} scanf("%d", &qu);
rep(i, 1, qu){
scanf("%d%d", &q[i].x, &q[i].y);
if (q[i].x > q[i].y) swap(q[i].x, q[i].y);
id[MP(q[i].x, q[i].y)] = i;
} rep(i, 1, m){
int now = (int)mp[i].size();
if (now > 0) v[now].push_back(i);
} line = sqrt(n);
rep(i, 1, line){
for (auto col : v[i]){
while (!s.empty()) s.pop();
for (auto edge : e[col]){
int x = edge.x, y = edge.y;
int fx = gf(x), fy = gf(y);
if (fx != fy){
s.push(MP(fx, father[fx]));
father[fx] = fy;
}
} cnt = 0;
for (auto u : mp[col]) c[++cnt] = u.fi;
rep(j, 1, cnt - 1){
rep(k, j + 1, cnt){
int x = c[j], y = c[k];
if (x > y) swap(x, y);
if (gf(x) == gf(y)) ++ret[MP(x, y)];
}
} while (!s.empty()){
father[s.top().fi] = s.top().se;
s.pop();
} }
} rep(i, line + 1, n){
for (auto col : v[i]){
memset(father, 0, sizeof father);
for (auto edge : e[col]){
int x = edge.x, y = edge.y;
int fx = getfather(x), fy = getfather(y);
if (fx != fy) father[fx] = fy;
} rep(j, 1, qu) if (getfather(q[j].x) == getfather(q[j].y)) ++q[j].ans;
}
} rep(i, 1, qu) printf("%d\n", q[i].ans + ret[MP(q[i].x, q[i].y)]);
return 0;
}

  

Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)的更多相关文章

  1. CodeForces 506D Mr. Kitayuta's Colorful Graph

    brute force ? 其实是平方分解.很容易想到的是每一个颜色建一个图,然后并查集维护一下连通性. 问题在于颜色有O(m)种,每种颜色的图点数都是O(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. [Poj2761]Feed the dogs(主席树)

    Desciption 题意:求区间第K小(N<=100000) Solution 主席树模板题 Code #include <cstdio> #include <algorit ...

  2. LyaoutParameters作用

    当你想要动态生成布局的时候,那么就要用到这个参数了.因为那时候你在布局文件里面写的width和height都不起作用了. LinearLayout linearLayout = (LinearLayo ...

  3. ogre3D学习基础5 -- 阴影与动画

    五.阴影 阴影是渲染一个真实场景的重要组成部分,它可以给场景中的物体提供更加真实的感觉,同时还可以帮助用户更好的了解对象间的空间关系. 启用阴影: 缺省情况下,阴影是关闭的,开启方式如下: 1.建立场 ...

  4. Bugku杂项-convert

    一进去就发现一堆二进制数,然后考虑怎么才能把这个和隐写扯上关系.首先,二进制我们肉眼就是看不懂再说什么的,这里就想到了转换,再联想上hex将原始数据转化为16进制.我们可以先把2进制转化为16进制,然 ...

  5. Python+Selenium练习篇之19-多窗口之间切换

    本文来介绍如何处理driver在多窗口之间切换,想一下这样的场景,在页面A点击一个连接,会触发在新Tab或者新窗口打开页面B,由于之前的driver实例对象在页面A,但是你接下来的脚本是操作页面B的元 ...

  6. Python+Selenium练习篇之4-利用link text定位元素

    本文介绍如何通过link text 来定位页面元素,我们打开网页,一些可以点击的链接跳转上面的文字,就是link text,用百度首页举例来看: 在上面图中,这一排上面的文字都是link text,例 ...

  7. [oldboy-django][1初始django]后台管理页面的布局 + djano母版(继承html)

    完善学员管理系统 - bootstrap fontawesome - 分页,路径导航,表格(class样式),消息图标(i标签),邮件图标(i标签) - 响应式导航 @media(min-width, ...

  8. CSS简单的四种引入方式

    CSS一共有四种引入方式 (1)最简单的两种方式是直接在html标签里面引入,或者在html文件前面声明,以下是简单的代码示例 <!DOCTYPE html> <html lang= ...

  9. Linux互斥锁、条件变量和信号量

    Linux互斥锁.条件变量和信号量  来自http://kongweile.iteye.com/blog/1155490 http://www.cnblogs.com/qingxia/archive/ ...

  10. LINUX 常用指令学习

    目录 0 查找find 1 别名alias 2 变量的设置 3 常用的系统变量 4 通配符及组合按键 5 指令之间的分隔符(;&||) 6 输出重定向(>,>>,1>, ...