codeforces-505B
题目连接:http://codeforces.com/contest/505/problem/B
1 second
256 megabytes
standard input
standard output
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.
Mr. Kitayuta wants you to process the following q queries.
In the i-th query, he gives you two integers — ui and vi.
Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.
The first line of the input contains space-separated two integers — n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100), denoting the number of the vertices and the number of the edges, respectively.
The next m lines contain space-separated three integers — ai, bi (1 ≤ ai < bi ≤ n) and ci (1 ≤ ci ≤ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i ≠ j,(ai, bi, ci) ≠ (aj, bj, cj).
The next line contains a integer — q (1 ≤ q ≤ 100), denoting the number of the queries.
Then follows q lines, containing space-separated two integers — ui and vi (1 ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi.
For each query, print the answer in a separate line.
4 51 2 11 2 22 3 12 3 32 4 331 23 41 4
210
5 71 5 12 5 13 5 14 5 11 2 22 3 23 4 251 55 12 51 51 4
11112
Let's consider the first sample.
The figure above shows the first sample.
- Vertex 1 and vertex 2 are connected by color 1 and 2.
- Vertex 3 and vertex 4 are connected by color 3.
- Vertex 1 and vertex 4 are not connected by any single color.
题目大意:给定一个图,可能存在重边,各边有不同权值,给定任意两点,求两点之间有多少种相同权值的边相连。解题思路:题目很直白,有多种解法,因为数据范围很小,所以可以直接对于每种颜色dfs遍历一遍,因为有多次查询,所以综合考虑为节省时间直接全部dfs一遍,再将结果储存在一个二维数组中,每次查询输出数组中的结果即可。因为可能存在重边,所以使用vector存边。dfs很简单,每次寻找一种颜色即可。代码如下:
#include<bits/stdc++.h>
using namespace std;
vector <][];
int n;
]= {};
bool dfs(int now,int color,int e)
{
if(now==e)
return true;
; i<=n; i++)
{
)
continue;
; j<g[now][i].size(); j++)
{
if(g[now][i][j]!=color)
continue;
else
{
vis[i]=;
if(dfs(i,color,e))
return true;
}
}
}
return false;
}
int main()
{
int m,u,v,c,q,ans;
][];
scanf("%d%d",&n,&m);
; i<m; i++)
{
scanf("%d%d%d",&u,&v,&c);
g[u][v].push_back(c);
g[v][u].push_back(c);
}
; i <= n; i++)
{
; j <= n; j++)
{
ans = ;
; k <= m; k++)
{
memset(vis,,sizeof(vis));
if (dfs(i, k, j))
ans++;
mp[i][j] = mp[j][i] = ans;
}
}
}
scanf("%d",&q);
; i<q; i++)
{
scanf("%d%d",&u,&v);
cout<<mp[u][v]<<endl;
}
}
此题还可以用并查集来解,构造一个二维并查集,每个颜色分别记录,更简单而且更快,但是第一时间想到的就是dfs,以后做题思维应当更灵活,不能定式思维,要熟悉各个算法可以实现的各种功能,再多加思考选用最优解。
codeforces-505B的更多相关文章
- CodeForces 505B Mr. Kitayuta's Colorful Graph
Mr. Kitayuta's Colorful Graph Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- codeforces 505B Mr. Kitayuta's Colorful Graph(水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...
- codeforces#505--B Weakened Common Divisor
B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input ...
- CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集
Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- asp.net 常用几种下载方式
protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目1
2014-03-20 02:55 题目:小朋友跳台阶,每次跳1层或2层,那么跳N层总共有多少种跳法. 解法:斐波那契数列. 代码: // 9.1 A child can run up the stai ...
- 《Cracking the Coding Interview》——第2章:链表——题目5
2014-03-18 02:32 题目:给定两个由单链表表示的数字,返回它们的和.比如(9->9) + (1->2) = 0->2->1,99 + 21 = 120. 解法:逐 ...
- oom 和 jvm crash的问题
很多次生产环境jvm进程无故消失的时候都留下了hs_err[pid].log文件 然后通过mat分析大多数情况是oom导致的 所以以前一直认为OOM一定会导致jvm crash 也就是说java ...
- java 用Arrays.binarySearch解读 快速定位数字范围
在一些时候,需要用给一个数字找到适合的区间,Arrays.binarySearch可达到这个目的. static int binarySearch(int[] a, int key) ...
- hdu 1714 RedField
RedField Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- jQuery静态分页功能
分页功能在做项目的过程中是常常用到的,下面是我常用的一款分页效果: 1.分页的CSS样式(page.css) #setpage { margin: 15px auto; text-align: cen ...
- [BZOJ4205][FJ2015集训] 卡牌配对 [建图+最大流]
题面 这是bzoj权限题,题面可以去下面的离线题库找 离线4205,只有题面,不能提交 思路 二分图匹配 这道题模型显然就是个二分图匹配嘛 那我们两两判断一下然后连边匹配.....就只有30分了 因为 ...
- hdu 6126 Give out candies
hdu 6126 Give out candies(最小割) 题意: 有\(n\)个小朋友,标号为\(1\)到\(n\),你要给每个小朋友至少\(1\)个且至多\(m\)个的糖果.小朋友们共提出\(k ...
- JavaScript的团队编程规范
本规范是针对javascript函数式编程风格与公司严重依赖于jQuery进行编码的现实制定出来. 禁止使用eval,with与caller(ecma262 v5 的use strict要求).eva ...