题目传送门

  1. /*
  2. 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条
  3. DFS:暴力每个颜色,以u走到v为结束标志,累加条数
  4. 注意:无向图
  5. */
  6. #include <cstdio>
  7. #include <iostream>
  8. #include <algorithm>
  9. #include <cstring>
  10. #include <string>
  11. #include <vector>
  12. using namespace std;
  13. const int MAXN = 1e2 + ;
  14. const int INF = 0x3f3f3f3f;
  15. int n, m;
  16. bool vis[MAXN];
  17. vector<pair<int, int> > V[MAXN];
  18. bool DFS(int u, int v, int c)
  19. {
  20. vis[u] = true;
  21. if (u == v) return true;
  22. for (int i=; i<V[u].size (); ++i)
  23. {
  24. pair<int, int> p = V[u][i];
  25. if (p.second == c && !vis[p.first])
  26. {
  27. if (DFS (p.first, v, c)) return true;
  28. }
  29. }
  30. return false;
  31. }
  32. int main(void) //Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
  33. {
  34. //freopen ("B.in", "r", stdin);
  35. while (scanf ("%d%d", &n, &m) == )
  36. {
  37. for (int i=; i<=m; ++i)
  38. {
  39. int u, v, c;
  40. scanf ("%d%d%d", &u, &v, &c);
  41. V[u].push_back (make_pair (v, c));
  42. V[v].push_back (make_pair (u, c));
  43. }
  44. int q; scanf ("%d", &q);
  45. while (q--)
  46. {
  47. int u, v; scanf ("%d%d", &u, &v);
  48. int ans = ;
  49. for (int i=; i<=m; ++i)
  50. {
  51. memset (vis, false, sizeof (vis));
  52. if (DFS (u, v, i)) ans++;
  53. }
  54. printf ("%d\n", ans);
  55. }
  56. }
  57. return ;
  58. }

  1. /*
  2. 并查集:开在结构体的并查集,进行如下的两个操作
  3. uf[c].Union (u, v); uf[i].same (u, v)
  4. */
  5. #include <cstdio>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <cstring>
  9. #include <vector>
  10. using namespace std;
  11. const int MAXN = 1e2 + ;
  12. const int INF = 0x3f3f3f3f;
  13. struct UF
  14. {
  15. int rt[MAXN];
  16. void init(void) {memset (rt, -, sizeof (rt));}
  17. int Find(int x) {return (rt[x] == -) ? x : rt[x] = Find (rt[x]);}
  18. void Union(int x, int y)
  19. {
  20. int tx = Find (x);
  21. int ty = Find (y);
  22. if (tx > ty) rt[ty] = tx;
  23. else if (tx < ty) rt[tx] = ty;
  24. }
  25. bool same(int x, int y)
  26. {
  27. return (Find (x) == Find (y));
  28. }
  29. }uf[MAXN];
  30. int n, m;
  31. int main(void) //Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
  32. {
  33. //freopen ("B.in", "r", stdin);
  34. while (scanf ("%d%d", &n, &m) == )
  35. {
  36. for (int i=; i<=m; ++i) uf[i].init ();
  37. for (int i=; i<=m; ++i)
  38. {
  39. int u, v, c;
  40. scanf ("%d%d%d", &u, &v, &c);
  41. uf[c].Union (u, v);
  42. }
  43. int q; scanf ("%d", &q);
  44. while (q--)
  45. {
  46. int u, v; scanf ("%d%d", &u, &v);
  47. int ans = ;
  48. for (int i=; i<=m; ++i)
  49. {
  50. if (uf[i].same (u, v)) ans++;
  51. }
  52. printf ("%d\n", ans);
  53. }
  54. }
  55. return ;
  56. }

并查集

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. homebrew -v 或homebrew -doctor报错请检查 .bash_profile是否有误

    homebrew -doctor报错: /usr/local/Library/Homebrew/global.rb:109:in `split': invalid byte sequence in U ...

  2. HTTP请求中带有特殊字符"|",返回400错误

    Java平台,服务器是Tomcat8,前端ajax访问服务器时,F12返回400错误,经分析,URL地址中get传参值里面含有“|“, Invalid character found and RFC ...

  3. Velocity模板引擎笔记

    模板引擎中判断对象是否为空: #if(!${jsonObj.data.buyerName} || ${jsonObj.data.buyerName} == '')         <p>采 ...

  4. REST的本质,就是用户操作某个网络资源(具有独一无二的识别符URI),获得某种服务,也就是动词+资源(都是HTTP协议的一部分)

    REST的名称”表现状态转化”中,省略了主语.”表现”其实指的是资源的表现. 资源就是网络上的一个数据实体,或者说是一个具体信息.它可以是一段文本.一张图片.一首歌曲.一种服务.你可以用一个URI(统 ...

  5. /dev下添加设备节点的方法步骤(通过device_create)

    将自己开发的内核代码加入到Linux内核中,需要3个步骤: 1.确定把自己开发代码放入到内核合适的位置 将demo_chardev.c文件拷贝到.../drivers/char/目录下. demo_c ...

  6. HDU 1257:最少拦截系统

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  7. DEDECMS织梦自定义表单中必填项、电话邮箱过滤以及验证码规则

    织梦自定义表单必填项规则--->(wwwshu-acca.com网站表单) 1. 在plus/diy.php 的第 40行下加如下代码: 1 2 3 4 5 6 7 8 9 10 11 12 1 ...

  8. vue中使用axios post上传头像/图片并实时显示到页面

    在前端开发中,为了更好的用户体验,在头像上传时会先将图片显示到页面然后点击保存按钮 完成图片的上传成功 代码部分有参考他人的写法. html代码:   <div id="myPhoto ...

  9. poj 1274 The Perfect Stall 解题报告

    题目链接:http://poj.org/problem?id=1274 题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 sta ...

  10. DP专辑之线性DP

    POJ1390 题目链接:http://poj.org/problem?id=1390 分类:记忆化搜索 dp[i][j][k] 表示,从i到j块且j后面有k块与第j块的颜色一样.dp[l][r][k ...