DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
/*
题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条
DFS:暴力每个颜色,以u走到v为结束标志,累加条数
注意:无向图
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
using namespace std; const int MAXN = 1e2 + ;
const int INF = 0x3f3f3f3f;
int n, m;
bool vis[MAXN];
vector<pair<int, int> > V[MAXN]; bool DFS(int u, int v, int c)
{
vis[u] = true;
if (u == v) return true;
for (int i=; i<V[u].size (); ++i)
{
pair<int, int> p = V[u][i];
if (p.second == c && !vis[p.first])
{
if (DFS (p.first, v, c)) return true;
}
} return false;
} int main(void) //Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
{
//freopen ("B.in", "r", stdin); while (scanf ("%d%d", &n, &m) == )
{
for (int i=; i<=m; ++i)
{
int u, v, c;
scanf ("%d%d%d", &u, &v, &c);
V[u].push_back (make_pair (v, c));
V[v].push_back (make_pair (u, c));
}
int q; scanf ("%d", &q);
while (q--)
{
int u, v; scanf ("%d%d", &u, &v);
int ans = ;
for (int i=; i<=m; ++i)
{
memset (vis, false, sizeof (vis));
if (DFS (u, v, i)) ans++;
}
printf ("%d\n", ans);
}
} return ;
}
/*
并查集:开在结构体的并查集,进行如下的两个操作
uf[c].Union (u, v); uf[i].same (u, v)
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std; const int MAXN = 1e2 + ;
const int INF = 0x3f3f3f3f;
struct UF
{
int rt[MAXN];
void init(void) {memset (rt, -, sizeof (rt));} int Find(int x) {return (rt[x] == -) ? x : rt[x] = Find (rt[x]);} void Union(int x, int y)
{
int tx = Find (x);
int ty = Find (y);
if (tx > ty) rt[ty] = tx;
else if (tx < ty) rt[tx] = ty;
} bool same(int x, int y)
{
return (Find (x) == Find (y));
}
}uf[MAXN];
int n, m; int main(void) //Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
{
//freopen ("B.in", "r", stdin); while (scanf ("%d%d", &n, &m) == )
{
for (int i=; i<=m; ++i) uf[i].init ();
for (int i=; i<=m; ++i)
{
int u, v, c;
scanf ("%d%d%d", &u, &v, &c);
uf[c].Union (u, v);
}
int q; scanf ("%d", &q);
while (q--)
{
int u, v; scanf ("%d%d", &u, &v);
int ans = ;
for (int i=; i<=m; ++i)
{
if (uf[i].same (u, v)) ans++;
}
printf ("%d\n", ans);
}
} return ;
}
并查集
DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph的更多相关文章
- 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. 1) D. Mr. Kitayuta's Colorful Graph
D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...
- 水题 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的应用)
由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一 ...
随机推荐
- 关于 truncate table 的一点学习札记
---下面整理笔记来之 itpub 的各位前辈的语录.这里做了一个汇总.仅供学习. truncate table后,oracle会回收表和其表中所在的索引到initial 大小,也就是初始分配的seg ...
- JAVA学习之 Model2中的Servlet与.NET一般处理程序傻傻分不清楚
时隔多日,多日合适吗,应该是时隔多月.我又想起了一般处理程序.这都是由于近期在实现的DRP系统中经经常使用到jsp+servlet达到界面与逻辑的分离.servlet负责处理从jsp传回的信息:每当这 ...
- 新产品为了效果,做的比較炫,用了非常多的图片和JS,所曾经端的性能是非常大的问题,分篇记录前端性能优化的一些小经验。
第一篇:HTTPserver 因tomcat处理静态资源的速度比較慢,所以首先想到的就是把全部静态资源(JS,CSS,image,swf) 提到单独的server,用更加高速的HTTPserver,这 ...
- Spark简单集群搭建
1. 上传spark-2.2.0-bin-hadoop2.7.tgz安装包到/home/dtouding目录下 2. 解压安装包到/bigdata/目录下,tar –zxvf spark-2.2.0- ...
- Java代理(Aop实现的原理)
经过大牛同事的一句指点立马明确的代理实现方式,Spring Aop应该也是这么去做的.直接上代码 实如今Car的run方法之前调用star方法,在run方法之后调用stop方法. Car类 packa ...
- bzoj4664: Count
是bzoj4498: 魔法的碰撞的哥哥题,我只写了一种 不一样的地方在于贡献有负数,第三维要保存的不能仅仅是0~L,这样空间会炸裂 考虑如何把贡献变成正的 假如要求最优解,那么一定是按顺序排,混乱度为 ...
- vue开发:移动端图片上传
因为最近遇到个移动端上传头像的需求,上传到后台的数据是base64位,其中为了提高用户体验,把比较大的图片用canvas进行压缩之后再进行上传.在移动端调用拍照功能时,会发生图片旋转,为了解决这个问题 ...
- java基础知识一
1.计算机基础知识概述 (1) 计算机 计算机(computer)俗称电脑,是一种用于高速计算的电子计算机器,可以进行数值计算,又可以进行逻辑计算,还具有存储记忆功能.是能够按照程序运行,自动.高速处 ...
- windows server2012之部署HTTPS安全站点
现在的互联网越来越重视网络安全方面的内容,像我们日常生活中浏览的网上银行网站等涉及安全的你都会发现有https 的标志出现,在URL前加https://前缀表明是用SSL加密的. 你的电脑与服务器之间 ...
- UIScrollView控件介绍
1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 (2)当展⽰示的内容较多,超出⼀一个屏幕时,⽤用户可通过滚动⼿手势来查看 ...