Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph
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的更多相关文章
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- 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/ ...
- 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. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)
数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...
- 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift
题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...
- CF 286(div 2) B Mr. Kitayuta's Colorful Graph【传递闭包】
解题思路:给出n个点,m条边(即题目中所说的两点之间相连的颜色) 询问任意两点之间由多少种不同的颜色连接 最开始想的时候可以用传递闭包或者并查集来做,可是并查集现在还不会做,就说下用传递闭包来做的这种 ...
- Codeforces Round #286 (Div. 1) B. Mr. Kitayuta's Technology (强连通分量)
题目地址:http://codeforces.com/contest/506/problem/B 先用强连通判环.然后转化成无向图,找无向图连通块.若一个有n个点的块内有强连通环,那么须要n条边.即正 ...
- Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP
题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...
- Codeforces Round #286 (Div. 2)A. Mr. Kitayuta's Gift(暴力,string的应用)
由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一 ...
随机推荐
- 2.UiSelector API 详细介绍
一.UiSelector类介绍 //通过各种属性与节点关系定位组件 简单实例: public void testDemo2() throws UiObjectNotFoundException{ Ui ...
- [DeeplearningAI笔记]卷积神经网络4.11一维和三维卷积
4.4特殊应用:人脸识别和神经网络风格转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 4.11一维和三维卷积 二维和一维卷积 对于2D卷积来说,假设原始图像为\(14*14*3\)的三通 ...
- JavaScript之RegExp
分为字面量和RegExp构造函数 1.正则表达式的匹配模式支持3个标志 g:表示全局模式,模式将被应用于所有字符串,而非在发现第一个匹配项时立即停止 i:表示不区分大小写模式,即在确定匹配项时忽略模式 ...
- Why are Eight Bits Enough for Deep Neural Networks?
Why are Eight Bits Enough for Deep Neural Networks? Deep learning is a very weird technology. It evo ...
- Flexbox兼容性
.flex-container{ display: -webkit-box; /* Chrome 4+, Safari 3.1, iOS Safari 3.2+ */ display: -moz-bo ...
- Hadoop 面试总结
1.简要描述如何安装配置一个开源的hadoop,只描述即可,列出完整步骤. a.创建一个用户和用户组,用来管理hadoop项目 b.修改确定ip地址:vim /etc/sysconfig/networ ...
- 面向对象 ( OO ) 的程序设计——继承
本文地址:http://www.cnblogs.com/veinyin/p/7608282.html 仅支持实现继承,且主要依靠原型链来实现,不过一般会混合构造函数一起实现继承 1 原型链 继承使用 ...
- 在Unity中实现屏幕空间阴影(2)
参考文章: https://www.imgtec.com/blog/implementing-fast-ray-traced-soft-shadows-in-a-game-engine/ 完成的工程: ...
- 使用Burpsuite爆破弱口令教工号
使用Burpsuite爆破弱口令教工号 发表于 2015-11-18 | 分类于 Burpsuite | 1条评论 | 26次阅读 准备 所谓工欲善其事,必先利其器,首先当然是要下 ...
- 关于parse_str变量覆盖分析
这个漏洞有两个姿势.一个是不存在的时候一个是存在的时候. 经过测试该漏洞只在php5.2中存在,其余均不存在. 倘若在parse_str函数使用的代码上方未将其定义那么即存在变量覆盖漏洞否则不行. 还 ...