Mr. Kitayuta's Colorful Graph

PCB工程师必看:别再搞错1078玻纤布的Dk了,手把手教你算等效介电常数 本文为PCB工程师提供了1078玻纤布等效介电常数(Dk)的详细计算指南,帮助解决高速设计中的信号完整性问题。通过解析玻纤布结构、混合介质理论和实战计算方法,指导工程师准确设置EDA工具参数,避免常见的Dk计算误区,提升设计精度和仿真准确性。 阅读详情

题目:

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.

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

Input

4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4

Output

2
1
0

Input

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

Output

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.

题意:

有n个点和m条边,每条边都有各自的颜色,多次询问两点之间有多少种不同的颜色相连。

例子看Note;

 

思路:

开一个三维数组,其中一维表示路的颜色,其他两维表示两个点的编号,如d[c][a][b]表示点a与点b之间有一条颜色为c的路。

利用floyd算法即可。

 

代码:

#include<stdio.h>
#include<string.h>
int d[110][110][110];

int main()
{
    int n,m;
    while(~scanf("%d%d", &n, &m))
    {
        int sum=0;
        memset(d, 0, sizeof(d));
        for(int i=1; i<=m; i++)
        {
            int a,b,c;
            scanf("%d%d%d", &a, &b, &c);
            d[c][a][b] = d[c][b][a] = 1;
        }
        for(int c = 1; c <= m; c++)
            for(int k = 1; k <= n; k++)
                for(int i = 1; i <= n; i++)
                    for(int j = 1; j <= n; j++)
                        if(d[c][i][j]==0)
                            d[c][i][j]=d[c][j][i]=(d[c][i][k]==1&&d[c][k][j]==1);
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int a,b;
            sum = 0;
            scanf("%d%d", &a, &b);
            for(int c = 1; c <= m; c++)
                if(d[c][a][b]) sum++;
            printf("%d\n", sum);
        }
    }
    return 0;
}

 

SAP-PP MD04详解一 MD04是SAP运行MRP后的结果查询事务码,其他功能强大,标准功能比如:查询/更改物料主数据、转换计划订单到生产订单/采购申请、查询/更改各个MRP元素对应的单据等等。而且还可以附加标准的菜单或者自定义添加事务码上去。老铁将做一个MD04详细说明系列,本篇为第一篇之MRP元素说明。 我们来看看MD04基本界面 可以看到主要的数据显示界面,有日期、MRP元素、MRP元素数据、再计划日期、收货/需求、可用数量、工厂、库存地点等栏位。光标定位到MRP元素列,按F1即可看到各类MRP元素的说... 阅读详情

相关推荐

如何在NVIDIA CX6 网卡和华为CE交换机上为RDMA协议使能DCQCN和PFC

【代码】如何在NVIDIA CX6 网卡和华为CE交换机上使能DCQCN和PFC。

Markchen7788的博客 5090

Mr. Kitayuta's Colorful Graph(二维并查集)

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

自渡的博客 332

自动驾驶仿真(二)—— 基于PreScan与Simulink的联合仿真

自动驾驶仿真软件介绍二—— PreScan与Simulink联合仿真1. Prescan仿真软件2. Prescan与Simulink联合仿真2. 1 Prescan和matlab软件安装2. 2 联合仿真配置3. 联合仿真实例3.1 一辆不按法规行驶的小车3.2 另一辆的正常行驶的小车3.3 视觉辅助摄像机3.4 传感器检测4. 系列学习视频教程5. 参考学习的书目教材 上一篇博文简单介绍了Carsim与Simulink联合仿真,接着这篇就再继续简单介绍另一款自动驾驶仿真软件Prescan,并且简单做个P

zhudj08的博客 1万+

Mr. Kitayuta's Colorful Graph CodeForces - 505B(Floyed||并查集)

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 c i, connecting vertex ...

starlet_kiss的博客 460

windows11下pycharm怎么完全卸载?完整卸载pycharm编辑器的教程

在卸载pycharm时都有遇到卸载不干净不完全的问题,那么具体怎样做才能完全卸载pycharm呢?退出所有的操作,重新启动电脑,会发现相关的软件已经被删除了。步骤1:卸载软件并删除安装目录。步骤2:删除相关的注册表。

拥抱具身智能,昇腾Ascend C 1万+

QML教程04 - Image:搞定界面上的所有图片

本文是QML教程系列的第四篇,重点讲解Image控件的实战用法。文章从图片显示常见的变形、卡顿等问题入手,详细介绍了Image的核心属性和使用技巧。主要内容包括:图片路径的三种写法(相对路径、qrc资源、网络URL)、最重要的fillMode属性解析(PreserveAspectFit、PreserveAspectCrop、Stretch的适用场景)、性能优化方法(sourceSize设置)、异步加载(asynchronous)以及圆形头像的实现方案。

BenBierBa的博客 1609

D. Mr. Kitayuta's Colorful Graph

题目链接 题目大意是:给一个无向图,n个点m条边,q次查询,每次查询给两个点u ,v,图中的边带有颜色,每次询问回答仅加入某种颜色的边可以从u到v,回答颜色的个数. 代码以后补:先说一下思路吧。对于每种颜色 我们可以直接找出来 是这种颜色的边然后跑bfs或者dfs确定uv是否相连,这样的话最坏的情况为O(m)而对每种颜色都这么跑一定会超时,所以我们可以想着优化一下,其实对于每对u,v来说我们只...

qq_41650771的博客 332

智能车图像处理11-进阶篇3--中线累积差值的统计

中线累积差值的统计在后面判断小S,以及长直道的时候都有用到,关键点在于中线的跳变 第12 - 13 行:comulants1 和comlants2代表的是两种不同的累差,取的行数不同 第14-17 行:comulants2取6到25行,其实就是My_Abs(LCenter[i], LCenter[i - 1])的累加 第18-21 行:comulants1去第1到40行 第27- 行: 计算了一下右线的开始于最终结束行,这里判断R_black是否为0,是和中线的提取中g

taiyuezyh的博客 3825

根号分治 ---- D. Mr. Kitayuta‘s Colorful Graph(根号均摊复杂度 + 数据结构维护)

题目链接 题目大意: 给你一个nnn个点mmm条边的无向图,每条边上面有个颜色cic_ici​,每次询问给你一个(u,v)(u,v)(u,v),问你有多少种颜色使得至少有一条路径从(u→v)(u\rightarrow v)(u→v)是同一种颜色的? n,m,q∈[1,1e5]n,m,q\in[1,1e5]n,m,q∈[1,1e5] 解题思路: 首先我们先边排序,按照颜色分块,每次我们只对用一种颜色的边进行建图用并查集维护连通性 假设每次我们加入的边是xxx个,那么循环的轮次是mx\frac{m}{x}

weixin_45630722的博客 273

3、硬件工程师笔试面试学习汇总——器件篇目录(2025版本)

硬件工程师笔试面试学习

XU157303764的博客 7620

OpenCV实战系列——图像线条提取

基于内容的图像分析,需要从构成图像的像素集合中提取有意义的特征。轮廓、线条、区域等是基本的图像基元,可用于描述图像中包含的元素。本节将介绍如何提取重要的图像线条特征。

m0_58523831的博客 1万+

B - Mr. Kitayuta's Colorful Graph

Description Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 ton. Each edge, namely edge i, has a color ci, c

ACM_hades的梦想之路 869

WorkBuddy CAD 审图救命课:约束、单位、尺寸链一次查清,打样前拦住返工

最危险的 CAD 文件不是打不开,而是看起来完整却混用了毫米与英寸、草图欠约束、关键尺寸没有来源。审图任务必须先列规则,再输出问题位置。 本课不会用“AI 一键完成”制造错觉,而是把 WorkBuddy、FreeCAD 0.21.2、OpenCascade 7.7 与人工审阅组成一条可复现链路。你会从“一份模拟 STEP 文件、参数表、装配基准与审图标准”出发,得到“单位报告、约束清单、尺寸链风险表与人工复核标记”,并学会用真实命令、错误信号和停止条件判断成果能不能进入下一阶段。

yueyeguzhuo的专栏 264

CF505B Mr. Kitayuta's Colorful Graph 题解

题目:CF505B Mr. Kitayuta’s Colorful Graph 图论 - dfs - 并查集 刚开始想了一个很暴力的 dfs 做法,不知道行不行,看到标签之后才开始往并查集的方向想 若 有至少一条 uuu 到 vvv 的路径,使得路径上所有的边颜色都是 ccc,我们则称这个颜色 ccc 为 u,vu,vu,v 的连通色 我们开一个二维并查集,f[i][j]f[i][j]f[i][j...

Brian_Pan_的博客 335

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

506D Mr. Kitayuta's Colorful Graph

D. Mr. Kitayuta's Colorful Graph time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Mr. Kitayuta has just bought an undi

lilili0709的专栏 400

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Codeforces 505B. Mr. Kitayuta's Colorful Graph 并查集

B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Mr. Kitayuta has just bough

码代码的猿猿 1807

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

B. Mr. Kitayuta's Colorful Graph

看数据范围觉得可以暴力搜索,高效的我也不会= = #include #include #include #include #include using namespace std; int n,m,q; int u,v,c; int vis[110]; vector > vt[110]; void Init(){ for(int i=1; i<=m; i++)

思维迷宫 582

CodeForces 506D Mr. Kitayuta's Colorful Graph

Problem Link : CF 506D Solution:这题一眼并查集,想不出啥骚操作或者高效的数据结构去维护这个东西,最初想法是对于每一个点开一个集合,用于维护各个颜色的相连关系,但是这个不好直接实现,空间时间都过不去。后来想到了可以用map代替数组进行维护,因为空间复杂度取决于边的个数,可以动态开。但是还是不会写二维的并查集。。。就百度学习了一下。 对于这题,我们用fa[u][c] =...

swunHJ的博客 384

505B - Mr. Kitayuta's Colorful Graph 【并查集】

传送门 题意:给你m条无向边,两个端点之间有一个颜色c,询问两点之间有几条用同种颜色连接的道路。 方法: ①:最开始我的想法是Floyd,用maps[i][j][k]三维数组表示从 i 到 j 是否能够通过 k 颜色连接。 (复杂度为(n^3*m+q*m)居然还过了,难道因为跑的是纯循环嘛。。。) ②:利用并查集来进行合并,我们开二维数组p[i][j]来表示,点 i 用颜色 j 的祖先是谁。...

qq_42211531的博客 238

codeforces 505B——DFS——Mr. Kitayuta's Colorful Graph

Mr. Kitayuta has just bought an undirected graph consisting ofnvertices andmedges. The vertices of the graph are numbered from 1 ton. Each edge, namely edgei, has a colorci, connecting ...

dgjm4087的博客 217

B. Mr. Kitayuta's Colorful Graph(并查集)

B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Mr. Kitayuta has just bought an undir

Sirius_han的博客 492
上一篇: Tree Recovery
下一篇: Functions again
admin__
博客等级 码龄8年 3粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值