D - Mr. Kitayuta's Colorful Graph

思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙。。

对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于大于sqrt(n)的块,我们暴力枚举答案。

这样就能做到严格sqrt(n) * n

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg using namespace std; const int N = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int n, m, cnt, fa[N], a[N], b[N], c[N];
unordered_map<int, bool> mp[N];
map<PII, int> ans;
vector<PII> vec[N];
int getRoot(int x) {
return fa[x] == x ? x : getRoot(fa[x]);
} int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i++) {
scanf("%d%d%d", &a[i], &b[i], &c[i]);
vec[c[i]].push_back(mk(a[i], b[i]));
}
for(int i = ; i <= n; i++) fa[i] = i;
for(int i = ; i <= m; i++) {
map<int, int> ma;
for(PII t : vec[i]) {
int x = getRoot(t.fi), y = getRoot(t.se);
if(x != y) fa[x] = y;
}
for(PII t : vec[i]) {
int x = getRoot(t.fi), y = getRoot(t.se);
if(ma.find(x) != ma.end()) {
mp[t.fi][ma[x]] = true;
} else {
cnt++;
mp[t.fi][cnt] = true;
ma[x] = cnt;
}
if(ma.find(y) != ma.end()) {
mp[t.se][ma[y]] = true;
} else {
cnt++;
mp[t.se][cnt] = true;
ma[y] = cnt;
}
}
for(PII t : vec[i]) {
fa[t.fi] = t.fi;
fa[t.se] = t.se;
}
} int q; scanf("%d", &q);
while(q--) {
int u, v;
scanf("%d%d", &u, &v);
if(mp[u].size() > mp[v].size()) swap(u, v);
if(ans.find(mk(u, v)) != ans.end()) {
printf("%d\n", ans[mk(u, v)]);
} else {
int num = ;
for(auto t : mp[u]) {
if(mp[v].find(t.fi) != mp[v].end()) num++;
}
printf("%d\n", num);
ans[mk(u, v)] = num;
}
}
return ;
} /*
*/

Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph的更多相关文章

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

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

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

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

  4. Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)

    数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...

  5. 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift

    题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...

  6. CF 286(div 2) B Mr. Kitayuta's Colorful Graph【传递闭包】

    解题思路:给出n个点,m条边(即题目中所说的两点之间相连的颜色) 询问任意两点之间由多少种不同的颜色连接 最开始想的时候可以用传递闭包或者并查集来做,可是并查集现在还不会做,就说下用传递闭包来做的这种 ...

  7. Codeforces Round #286 (Div. 1) B. Mr. Kitayuta&#39;s Technology (强连通分量)

    题目地址:http://codeforces.com/contest/506/problem/B 先用强连通判环.然后转化成无向图,找无向图连通块.若一个有n个点的块内有强连通环,那么须要n条边.即正 ...

  8. Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP

    题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...

  9. Codeforces Round #286 (Div. 2)A. Mr. Kitayuta's Gift(暴力,string的应用)

    由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一 ...

随机推荐

  1. epoll的一些细节和注意事项

    epoll_event结构 struct epoll_event { uint32_t events; /* Epoll events */ epoll_data_t data; /* User da ...

  2. redis 字符串的管理的一点理解

    redis字符串可以实现通过地址偏移找到所在结构体的首地址,struct sdshdr *sh = (void *)(s - (sizeof(struct sdshdr))) 也就是通过buf地址可以 ...

  3. python中如何优雅续行和换行

    http://note.youdao.com/noteshare?id=8dbcb93991a89a6cfcd95580ed2198f0

  4. Bootstrap 按钮下拉菜单

    向下拉 <div class="dropdown"> <button class="btn btn-default" data-toggle= ...

  5. LightOJ 1244 - Tiles 猜递推+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...

  6. Python学习笔记(四十三)virtualenv (创建一套“隔离”的Python运行环境)

    摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432712108 ...

  7. 20155117王震宇 2006-2007-2 《Java程序设计》第一周学习总结

    20155117王震宇 2006-2007-2 <Java程序设计>第一周学习总结 教材学习内容总结 尽量简单的总结一下本周学习内容 尽量不要抄书,浪费时间 看懂就过,看不懂,学习有心得的 ...

  8. NYOJ 163 Phone List (字符串处理 字典树)

    题目链接 描述 Given a list of phone numbers, determine if it is consistent in the sense that no number is ...

  9. shell中$*与$@的区别

    $*所有的位置参数,被作为一个单词 注意:"$*"必须被""引用 $@ 与$*同义,但是每个参数都是一个独立的""引用字串,这就意味着参数被 ...

  10. [转载]理解Tomcat的Classpath-常见问题以及如何解决

    摘自: http://www.linuxidc.com/Linux/2011-08/41684.htm 在很多Apache Tomcat用户论坛,一个问题经常被提出,那就是如何配置Tomcat的cla ...