题目传送门

 /*
题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条
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的更多相关文章

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

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

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

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

  4. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph

    D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...

  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. 修复Xcode升级错误 — PCH File Error

    http://www.rockia.net/2013/03/fix-xcode-update-pch-file-error Error:PCH File Built From A Different ...

  2. 解决gradle多模块依赖在Idea中能运行,gradle build失败的问题。

    最近需要初始化一个SpringBoot新项目遇到一个问题就是:项目中有多个子模块,使用gradle依赖管理成功. 项目结构如下: project --module1     --module2我的mo ...

  3. js modify local file

    https://stackoverflow.com/questions/4561157/is-it-possible-to-modify-a-html-file-from-which-the-scri ...

  4. Deep Learning 33:读论文“Densely Connected Convolutional Networks”-------DenseNet 简单理解

    一.读前说明 1.论文"Densely Connected Convolutional Networks"是现在为止效果最好的CNN架构,比Resnet还好,有必要学习一下它为什么 ...

  5. ERROR: cannot start Android Studio. No JDK found. Please validate either ANDROID_STUDIO_JDK, JDK_HOME + Unrecognized VM option '+UseCodeCacheFlushing

    想学下android,在本来想用myeclipse安装下sdk和adt,谁知在官网看到http://developer.android.com/sdk/index.html Google I/O 20 ...

  6. mysql优化-----多列索引的左前缀规则

    索引优化策略 :索引类型 .1B-tree索引 关注的是:Btree索引的左前缀匹配规则,索引在排序和分组上发挥的作用. 注:名叫btree索引,大的方面看都用的二叉树.平衡树.但具体的实现上,各引擎 ...

  7. CSS中的那点事儿(一)--- CSS中的单位1

    单位主要用在规定元素的数值性css属性的,这里主要讨论应用的比较多的是width height  padding margin font-size,而单位中应用最广泛就是%.px.em 百分比 百分比 ...

  8. 使用sql compare生成的sql语句

    创建表以及主键 判断表是否存在 OBJECT_ID 判断主键是否存在 SELECT 1 FROM sys.indexes WHERE name = N'PK_LISA_NoUseWebpartRepl ...

  9. MYSQL进阶学习笔记四:MySQL存储过程之定义条件,处理过程及存储过程的管理!(视频序号:进阶_11,12)

    知识点五:MySQL存储过程之定义条件和处理过程及存储过程的管理(11,12) 定义条件和处理: 条件的定义和处理可以用来定义在处理过程中遇到的问题时相应的处理步骤. DECLARE CONTINUE ...

  10. String bulit-in function

    tip: 和tuple一样,字符串也是不可变的类型,字符串的内建函数有非常多,我们一一举例来看看他们的作用 下面是用dir(str) 输出的内容: ['__add__', '__class__', ' ...