题目连接:http://codeforces.com/contest/505/problem/B

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 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 51 2 11 2 22 3 12 3 32 4 331 23 41 4
output
210
input
5 71 5 12 5 13 5 14 5 11 2 22 3 23 4 251 55 12 51 51 4
output
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的更多相关文章

  1. CodeForces 505B Mr. Kitayuta's Colorful Graph

    Mr. Kitayuta's Colorful Graph Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  2. codeforces 505B Mr. Kitayuta's Colorful Graph(水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...

  3. codeforces#505--B Weakened Common Divisor

    B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input ...

  4. CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集

    Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...

  5. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  10. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. ubuntu上通用解压方式

    ubuntu上通用解压方式 tar xvf *.bin.tar.gz,gz,tar.tgz

  2. 遍历两个自定义列表来实现字典(key:value)

    #自定义key    ${keys}    create list    key1    key2    key3 #自定义value    ${values}    create list    v ...

  3. java IO小结

    package 字符与字节转换; import java.io.*; public class char_byte { public static void main(String[] args) { ...

  4. lshw

    https://linux.die.net/man/1/lshw lshw(Hardware Lister)是另外一个可以查看硬件信息的工具,不仅如此,它还可以用来做一些硬件的benchmark. 这 ...

  5. Ubuntu16.04 问题汇总

    Ubuntu16.04安装wps并解决系统缺失字体问题 http://www.cnblogs.com/liutongqing/p/6388160.html

  6. leetcode_day02

    任务二:删除排序数组中的重复项 原文链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ 最开始的解决思路: ...

  7. PHP面向对象 封装与继承

    知识点: PHP封装三个关键词: 一.public 公有的,被public修饰的属性和方法,对象可以任意访问和调用 二.private 私有的,被private修饰的属性和方法,只能在类内部的方法可以 ...

  8. Extjs 4 小记

    ////////////////////////////////////---Ajax 等待提示消息---/////////////////////////////////////////////// ...

  9. 研华 FWA-3231 单路E3平台

    研华 FWA-3231 单路E3平台 服务器主板上芯片组与处理器的适配参考

  10. [CF1045C]Hyperspace Highways

    题目大意:给一张$n$个点$m$条边的图,保证若有一个环,一定是完全子图,多次询问两个点之间的最短路径长度 题解:把完全子图缩成一个点,圆方树,方点权值设成$1$,圆点设成$0$即可. 卡点:数组开小 ...