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的应用)
由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一 ...
随机推荐
- 理解和使用WPF 验证机制
博客 学院 下载 更多 写博客 发布Chat 登录注册 理解和使用WPF 验证机制 原创 2013年06月20日 11:15:37 7404 首先建立一个demo用以学习和实验WPF Data Val ...
- spark通信原理
https://github.com/apache/spark/tree/master/core/src/main/scala/org/apache/spark/network https://git ...
- Deep Learning 31: 不同版本的keras,对同样的代码,得到不同结果的原因总结
一.疑问 这几天一直纠结于一个问题: 同样的代码,为什么在keras的0.3.3版本中,拟合得比较好,也没有过拟合,验证集准确率一直高于训练准确率. 但是在换到keras的1.2.0版本中的时候,就过 ...
- 树状数组(二叉索引树 BIT Fenwick树) *【一维基础模板】(查询区间和+修改更新)
刘汝佳:<训练指南>Page(194) #include <stdio.h> #include <string.h> #include <stdlib.h&g ...
- 一步一步学Silverlight 2系列文章
概述 由TerryLee编写的<Silverlight 2完美征程>一书,已经上市,在该系列文章的基础上补充了大量的内容,敬请关注.官方网站:http://www.dotneteye.cn ...
- 第四届蓝桥杯C++B组省赛
1.高斯日记 2.马虎的算式 3.第39级台阶 4.黄金连分数 5.前缀判断 6.三部排序 7.错误票据 8.翻硬币 9.带分数 10.连号区间数
- LibSVM学习详细说明
代码文件主要针对Matlab进行说明,但个人仍觉得讲解的支持向量机内容非常棒,可以做为理解这一统计方法的辅助资料; LibSVM是台湾林智仁(Chih-Jen Lin)教授2001年开发的一套支持向量 ...
- luogu 4782【模板】 2-SAT 问题
2-SAT就是给出$m$个限制表示$x==val_x || y==val_y$ 求出满足的解 每个点拆成两个点,如果$x$不满足则$y$一定满足,$y$不满足同理.这样我们连边,然后$tarjan$即 ...
- phpstorm 10 初体验
一:安装phpstorm 10 去phpstorm 10官网下载,安装 https://www.jetbrains.com/phpstorm/ 按照提示安装,最后注册步骤,选择“License ser ...
- DDK编写64位驱动时加入x64汇编的方法
上篇讲了如何在编写x64应用程序时加入x64汇编,这里来说说如何在编写x64驱动时加入x64汇编. 一.在asm文件中单独编写功能函数 比如要实现一个64位的加法函数,原型如下: ULONG64 my ...