【CF#505B】Mr. Kitayuta's Colorful Graph (并查集或Floyd或BFS)

题干:

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 — aibi (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.

解题报告:

       

ac代码1:(并查集)(15ms,0.1MB)

   原来的代码加上了ra数组,至今不明白是干啥的。

#include <iostream>
using namespace std;
#define MAXN 110
int pa[110][110],ra[110][110];
void init() {for(int i=0; i<MAXN; i++) {for(int j=0; j<MAXN; j++) {pa[i][j]=j;ra[i][j]=0;}}
}
int getf(int x,int c) {if(pa[c][x]!=x)pa[c][x]=getf(pa[c][x],c);return pa[c][x];
}
void merge(int x,int y,int c) {int t1=getf(x,c);int t2=getf(y,c);if(t1==t2) return;else {pa[c][t2]=pa[c][t1];}
//	if(ra[c][t1]<ra[c][t2]) {
//		pa[c][t1]=t2;
//	} else {
//		pa[c][t2]=t1;
//		if(ra[c][t1]==ra[c][t2])ra[c][t1]++;
//	}
}
bool same(int x,int y,int c) {return getf(x,c)==getf(y,c);
}int main() {ios::sync_with_stdio(false);int n,m;init();cin>>n>>m;int u,v,c;for(int i=0; i<m; i++) {cin>>u>>v>>c;u--,v--,c--;merge(u,v,c);}int q;cin>>q;for(int i=0; i<q; i++) {cin>>u>>v;u--;v--; int ans=0;for(int i=0; i<m; i++) {if(same(u,v,i))ans++;}cout<<ans<<endl;}return 0;
}

ac代码2:(广搜bfs)(31ms,0.3MB)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#define MAX 107using namespace std;int n,m,c,x,y,q;
vector<int> e[MAX][MAX];
bool vis[MAX];void add ( int u , int v , int w )
{e[w][u].push_back ( v );e[w][v].push_back ( u );
}bool bfs ( int c )
{memset ( vis , 0 , sizeof ( vis ) );queue<int> q;q.push ( x );while ( !q.empty() ){int u = q.front();if ( u == y ) return true;q.pop();for ( int i = 0 ; i < e[c][u].size() ; i++ ){int v = e[c][u][i];if ( vis[v] ) continue;vis[v] = true;q.push ( v );}}return false;
}int main ( )
{while ( ~scanf ( "%d%d" , &n , &m ) ){for ( int i = 0 ; i < MAX ; i++ )for ( int j = 0 ; j < MAX ; j++ )e[i][j].clear();for ( int i = 0 ; i < m ; i++ ){scanf ( "%d%d%d" , &x , &y , &c );add ( x , y , c );}scanf ( "%d" , &q );while ( q-- ){int ans = 0;scanf ( "%d%d" , &x , &y );for ( int i = 1 ; i <= m ; i++ )if ( bfs ( i ) ) ans++;printf ( "%d\n" , ans );}}
}

ac代码3:(Floyd)

#include<bits/stdc++.h>using namespace std;
const int MAX = 100 + 5;int maze[MAX][MAX][MAX];
bool bk[MAX];
int main()
{int n,m;int a,b,c;int q,cnt;scanf("%d %d",&n,&m);while(m--) {scanf("%d %d %d",&a,&b,&c);maze[a][b][c]=1;maze[b][a][c]=1;bk[c]=1;}for(int c = 1; c<=MAX; c++) {//看看这个颜色有没有出现过 //他确实是输入m条路所以最多有m个颜色,但是你不能搜索到m啊!! if(bk[c]==0) continue;for(int k = 1; k<=n; k++) {for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if( maze[i][k][c]==1&&maze[k][j][c]==1) {maze[i][j][c]=1;}}}}}scanf("%d",&q);while(q--) {cnt=0;scanf("%d %d",&a,&b);for(int c = 1; c<=MAX; c++) {if(bk[c]==1 && maze[a][b][c] == 1) {cnt++;}}printf("%d\n",cnt);}return 0 ;
}

总结:

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440535.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java data jpa_Spring Data JPA(一)简介

Spring Data JPA介绍可以理解为JPA规范的再次封装抽象&#xff0c;底层还是使用了Hibernate的JPA技术实现&#xff0c;引用JPQL(Java Persistence Query Language)查询语言&#xff0c;属于Spring整个生态体系的一部分。随着Spring Boot和Spring Cloud在市场上的流行&#xff0…

【CodeForces - 438D】The Child and Sequence(线段树区间取模操作)

题干&#xff1a; At the childrens day, the child came to Pickss house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks. Fortunately, Picks remembers how to repair the sequ…

java 自定义xml_6.1 如何在spring中自定义xml标签

dubbo自定义了很多xml标签&#xff0c;例如&#xff0c;那么这些自定义标签是怎么与spring结合起来的呢&#xff1f;我们先看一个简单的例子。一 编写模型类1 packagecom.hulk.testdubbo.model;23 public classHero {4 privateString name;5 private intage;67 publicString ge…

java 模块设计模式_Java9模块化学习笔记二之模块设计模式

模块设计的原则:1、防止出现编译时循环依赖(主要是编译器不支持)&#xff0c;但运行时是允许循环依赖的&#xff0c;比如GUI应用2、明确模块的边界几种模块设计:API模块&#xff0c;聚合模块(比如java.base)可选依赖两种方式:1、可选的编译时依赖(类似于maven的provided scope)…

java 手写签名_Android 自定义View手写签名并保存图片

GIF压缩有问题&#xff0c;运行很顺滑&#xff01;&#xff01;&#xff01;1.自定义View——支持设置画笔颜色&#xff0c;画笔宽度&#xff0c;画板颜色&#xff0c;清除画板&#xff0c;检查是否有签名&#xff0c;保存画板图片(复制粘贴可直接使用)/*** Created by YyyyQ o…

【2019第十届蓝桥杯省赛C/C++B组题解】(非官方题解)

A。 数数题。 答案&#xff1a;490 B。 26进制模拟。 答案&#xff1a;BYQ C。 类似fib数列求值&#xff0c;递推一下就好。 答案&#xff1a;4659 D。 注意两个坑点&#xff0c;一个是正整数&#xff0c;所以枚举要从1开始。第二个坑点是互不相同的&#xff0c;为了达到这…

java对象模型 指令_深入理解多线程(二)—— Java的对象模型

上一篇文章中简单介绍过synchronized关键字的方式&#xff0c;其中&#xff0c;同步代码块使用monitorenter和monitorexit两个指令实现&#xff0c;同步方法使用ACC_SYNCHRONIZED标记符实现。后面几篇文章会从JVM源码的角度更加深入&#xff0c;层层剥开synchronized的面纱。在…

java 学生课程成绩_Java课设--学生成绩管理系统一

写在前面这个项目是Java课程的课设&#xff0c;一共花了5天的时间去完成它&#xff0c;在这期间感谢一些博主的帮助&#xff0c;让我了解到了一些新的技术知识&#xff0c;所以打算写这一系列博客来介绍一整个课设项目&#xff0c;也为了帮助之后的人&#xff0c;如有错误&…

java调用cplex案例_【CPLEX教程03】java调用cplex求解一个TSP问题模型

前面我们已经搭建好cplex的java环境了&#xff0c;相信大家已经跃跃欲试&#xff0c;想动手写几个模型了。今天就来拿一个TSP的问题模型来给大家演示一下吧~CPLEX系列教程可以关注我们的公众号哦&#xff01;获取更多精彩消息&#xff01;01 TSP寤烘ā关于TSP建模&#xff0c;就…

java 自动启动监听_Spring Boot 启动事件和监听器,太强大了!

大家都知道&#xff0c;在 Spring 框架中事件和监听无处不在&#xff0c;打通了 Spring 框架的任督二脉&#xff0c;事件和监听也是 Spring 框架必学的核心知识之一。一般来说&#xff0c;我们很少会使用到应用程序事件&#xff0c;但我们也不要忘了它们的存在&#xff0c;比如…

java day_Java_Day7(上)

Java learning_Day7(上)内容常用类枚举类型常用类String 类java.lang.String 类代表不可变的字符序列。String 类的常见构造方法&#xff1a;String(String original)创建一个 String 对象为 original 的拷贝。String(char[] value)用一个字符数组创建一个 String 对象。String…

java关键字 valotile_Java内存模型-jsr133规范介绍,java中volatile关键字的含义

最近在看《深入理解Java虚拟机&#xff1a;JVM高级特性与最佳实践》讲到了线程相关的细节知识&#xff0c;里面讲述了关于java内存模型&#xff0c;也就是jsr 133定义的规范。系统的看了jsr 133规范的前面几个章节的内容&#xff0c;觉得受益匪浅。废话不说&#xff0c;简要的介…

Java重载和重写6_深入理解Java中的重写和重载

深入理解Java中的重写和重载重载(Overloading)和重写(Overriding)是Java中两个比较重要的概念。但是对于新手来说也比较容易混淆。本文通过两个简单的例子说明了他们之间的区别。定义重载简单说&#xff0c;就是函数或者方法有同样的名称&#xff0c;但是参数列表不相同的情形&…

python 协程池gevent.pool_进程池\线程池,协程,gevent

目录1. 进程池与线程池2. 协程3. gevent4. 单线程下实现并发的套接字通信首先写一个基于多线程的套接字服务端:from socket import *from threading import Threaddef comunicate(conn):while True: # 通信循环try:data conn.recv(1024)if len(data) 0: breakconn.send(data.…

java poi 3.13_Java 读取Excl文件 (poi-3.13)

最近做项目遇到了读取Excel数据到数据库做数据的初始化。于是找一个。发现(poi-3.13)可以解决问题。可以解析两种格式(xlsx和xls)以下是实现的步骤1.下载poi3.13包&#xff0c;地址(http://poi.apache.org/download.html#POI-3.13)2.学习APi。接下来是还是demo来说明问题吧&…

【CodeChef - CLIQUED 】Bear and Clique Distances(建图,缩点技巧,思维)

题干&#xff1a; 解题报告&#xff1a; 主要就是在于怎么处理那个前K个点&#xff1a;组成一个团。换句话说&#xff0c;缩成一个点。先直接当成每个点多了k条边来处理&#xff0c;T了。想想也是啊&#xff0c;要是K1e5&#xff0c;那就是1e10条边了。。刚开始尝试了半天缩点&…

【HDU - 5649】DZY Loves Sorting(线段树,区间更新区间查询,思维,01缩数变换,线段树分割)

题干&#xff1a; DZY has a sequence a[1..n]a[1..n]. It is a permutation of integers 1∼n1∼n. Now he wants to perform two types of operations: 0lr0lr: Sort a[l..r]a[l..r] in increasing order. 1lr1lr: Sort a[l..r]a[l..r] in decreasing order. After doin…

php错误403_phpstudy访问文件报错403/Forbidden解决办法

使用phpstudy访问WWW目录下的文件时&#xff0c;浏览器提示Forbidden错误&#xff0c;没有访问权限。我在网上搜索了喝多资料以及本人亲自尝试过后&#xff0c;总结了一下两种方法。方法一&#xff1a;打开phpStudy&#xff0c;点击按键“其他选项菜单”>找到phpStudy配置&g…

【CodeForces - 294B】Shaass and Bookshelf(枚举,贪心,思维,组内贪心组间dp)

题干&#xff1a; Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelfs dimensions to be as small as possible. The thickness of the i-th book is ti and its pages width is equal to wi. The thickness of each book is eith…

mac php开发集成环境,MAC OS X下php集成开发环境mamp

之前苦于mac上搭建本地服务器之艰辛&#xff0c;找寻好久都没找到一款类似windows上集成的本地服务器环境&#xff0c;诸如phpstudy&#xff0c;xampp,appserv,虽说xampp也有mac版&#xff0c;但不知为何不是Apache启动不了&#xff0c;这里小编为大家分享了MAC OS X 下php集成…