Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs
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 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
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.
题意:给你一个图n个点,m条边;
每条边有个颜色,每次u,v只能走一条颜色的路径;
求有多少条路;
思路:暴力dfs;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14http://v.ifeng.com/program/lyyy/
const int N=2e5+,M=1e6+,inf=1e9+,MOD=;
const ll INF=1e18+;
struct edge
{
int v,w;
int nex;
}edge[N];
int head[N],edg;
void init()
{
memset(head,-,sizeof(head));
edg=;
}
void add(int u,int v,int w)
{
++edg;
edge[edg].v=v;
edge[edg].w=w;
edge[edg].nex=head[u];
head[u]=edg;
}
int now,to,ans;
int flag[N];
int hh[N];
void dfs(int u,int pre)
{
//cout<<u<<" "<<pre<<endl;
if(u==to)
{
if(!hh[pre]&&pre!=-)
{
ans++; hh[pre]=;
}
return;
}
for(int i=head[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
if(flag[v])continue;
int w=edge[i].w;
if(pre==-||w==pre)
{
flag[v]=;
dfs(v,w);
flag[v]=;
}
}
}
int main()
{
init();
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
int q;
scanf("%d",&q);
while(q--)
{
memset(hh,,sizeof(hh));
ans=;
scanf("%d%d",&now,&to);
flag[now]=;
dfs(now,-);
flag[now]=;
printf("%d\n",ans);
}
return ;
}
Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs的更多相关文章
- Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)
数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- 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暴力枚举, 对于 ...
- 水题 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的应用)
由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一 ...
随机推荐
- css经典布局学习
. 布局 布局是css的重头戏,每个系统的布局都有其各自的特点.无好无坏,肯定是各有优缺点,不妨拿出几个比较典型的例子来一起分析一下.例如: 经典三列布局 Bootstrap栅格布局 百度首页布局 微 ...
- Java生产者消费者模型
在Java中线程同步的经典案例,不同线程对同一个对象同时进行多线程操作,为了保持线程安全,数据结果要是我们期望的结果. 生产者-消费者模型可以很好的解释这个现象:对于公共数据data,初始值为0,多个 ...
- C# 插件
1.EsFrameWork框架 http://www.oraycn.com/ESFramework_download.aspx
- c# XML省市联动
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Unity-Animator深入系列---StateMachineBehaviour状态机脚本学习
回到 Animator深入系列总目录 首先这个脚本必须继承自StateMachineBehaviour public class MySMB : StateMachineBehaviour { pub ...
- 制作动画平滑过渡效果:《CSS3 Transition》
W3C标准中对css3的transition这是样描述的:“css的transition允许css的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发, ...
- linux 文件操作命令
目录操作命令 命令格式 命令 [-选项] [-参数] ls -la /etc 有多个选项时可以合并 ls命令: -a (all) 显示所有文件,包括隐藏文件,那是用.开头的文件 为什么要隐藏(这是个系 ...
- 2016 Al-Baath University Training Camp Contest-1 I
Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...
- Android LayoutInflater原理分析
相信接触Android久一点的朋友对于LayoutInflater一定不会陌生,都会知道它主要是用于加载布局的.而刚接触Android的朋友可能对LayoutInflater不怎么熟悉,因为加载布局的 ...
- html与表格知识
<html> --开始标签 <head> 网页上的控制信息 <title>页面标题</title> </head> <body& ...