CodeForces - 505B-Mr. Kitayuta's Colorful Graph(暴力)
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and medges. 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.
Input
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.
Output
For each query, print the answer in a separate line.
Examples
4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4
2
1
0
5 7
1 5 1
2 5 1
3 5 1
4 5 1
1 2 2
2 3 2
3 4 2
5
1 5
5 1
2 5
1 5
1 4
1
1
1
1
2
Note
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.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> const int maxn=1e5+;
typedef long long ll;
using namespace std;
struct node
{
int v;
int val;
};
int ans=;
int vis[];
vector<node>vec[];
void bfs(int x,int y)
{
node start;
for(int j=;j<=;j++)
{
queue<node>q;
memset(vis,,sizeof(vis));
vis[x]=;
for(int t=;t<vec[x].size();t++)
{
if(vec[x][t].val==j&&vis[vec[x][t].v]==)
{
q.push(vec[x][t]);
vis[vec[x][t].v]=;
}
}
while(!q.empty())
{
node now=q.front();
q.pop();
if(now.v==y)
{
ans++;
}
for(int t=;t<vec[now.v].size();t++)
{
if(vec[now.v][t].val==j&&vis[vec[now.v][t].v]==)
{
vis[vec[now.v][t].v]=;
q.push(vec[now.v][t]);
}
}
}
}
}
int main()
{
int n,m;
cin>>n>>m; int a,b,c;
set<int>s;
for(int t=;t<m;t++)
{
scanf("%d%d%d",&a,&b,&c);
s.insert(c);
node st;
st.v=b;
st.val=c;
vec[a].push_back(st);
st.v=a;
st.val=c;
vec[b].push_back(st);
}
int q;
cin>>q;
int uu,vv;
for(int t=;t<q;t++)
{
scanf("%d%d",&uu,&vv);
ans=;
bfs(uu,vv);
printf("%d\n",ans);
} return ;
}
CodeForces - 505B-Mr. Kitayuta's Colorful Graph(暴力)的更多相关文章
- 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 - 505B Mr. Kitayuta's Colorful Graph 二维并查集
Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...
- Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)
题目链接 Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$ 涉及的点的个数 $<= ...
- CodeForces 506D Mr. Kitayuta's Colorful Graph
brute force ? 其实是平方分解.很容易想到的是每一个颜色建一个图,然后并查集维护一下连通性. 问题在于颜色有O(m)种,每种颜色的图点数都是O(n)的,因此并查集的空间只能重复利用. 但是 ...
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- 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. 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. 1) D. Mr. Kitayuta's Colorful Graph
D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...
- Mr. Kitayuta's Colorful Graph 多维并查集
Mr. Kitayuta's Colorful Graph 并查集不仅可以用于一维,也可以用于高维. 此题的大意是10W个点10W条边(有多种颜色),10W个询问:任意两个节点之间可以由几条相同颜色的 ...
随机推荐
- fastjson JSON.toJavaObject() 实体类首字母大写属性无法解析问题
fastjson JSON.toJavaObject() 实体类首字母大写属性无法解析问题
- Java线程生命周期与状态切换
前提 最近有点懒散,没什么比较有深度的产出.刚好想重新研读一下JUC线程池的源码实现,在此之前先深入了解一下Java中的线程实现,包括线程的生命周期.状态切换以及线程的上下文切换等等.编写本文的时候, ...
- 标星7000+,这个 Python 艺术二维码生成器厉害了!
微信二维码,相信大家也并不陌生,为了生成美观的二维码,许多用户都会利用一些二维码生成工具. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手 ...
- PXE安装与配置
PXE 安装与配置 实验环境 VMware Fusion 虚拟机 node1有两块网卡, ens33(172.100.16.10)-->bridge, ens37-->vmnet4(192 ...
- java_数组的定义与操作
数组定义和访问 数组概念 数组概念: 数组就是存储多个数据的容器,数组的长度固定,多个数据的数据类型要一致. 数组的定义 方式一 数组存储的数据类型[] 数组名字 = new 数组存储的数据类型[长度 ...
- 面向对象的js编程 Call和apply方法
JavaScript中有一个call和apply方法,其作用基本相同,但也有略微的区别. 一.方法定义 1.call 方法 语法:call([thisObj[,arg1[, arg2[, [,.arg ...
- Ubuntu18.04安装Nautilus-actions自定义文件管理器鼠标右键列表
sudo add-apt-repository ppa:daniel-marynicz/filemanager-actions #需要添加源 sudo apt-get install filemana ...
- vue a标签下载图片文档显示下载失败
解决:把所要下载的文件放到static文件下,具体原因-静态文件放在static内,否则webpack会打包.
- 蓝牙bluez命令
记录一下自己平时调试蓝牙的命令,后续学习到再添加 sdptool命令: sdptool add SP - 添加SPP: sdptool add --channel=1 DID SP DUN LA ...
- Vue管理系统前端系列一vue-cli4.x 初始化项目
目录 项目介绍 技术基础 开发环境 安装工具 快速原型开发 创建项目 配置相关说明 目录结构 项目介绍 lion-ui 是一个基于 RBAC 的管理系统前端项目,采用 vue 和 element-ui ...