题目连接: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. Windows Phone 图片扩展类

    using System.IO; using System.Text; using System.Net; using System.Threading.Tasks; using System.Win ...

  2. Pytest+allure+jenkins生成测试报告

    allure集成jenkins下载地址.下载相关版本的allure包 http://updates.jenkins-ci.org/download/plugins/allure-jenkins-plu ...

  3. [转]个人对AutoResetEvent和ManualResetEvent的理解

    仅个人见解,不对之处请指正,谢谢. 一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方 ...

  4. android studio 配置网络代理

    1.首先在vultr网站购买服务器. 然后使用shadowsocksR给服务器配置FQ,再在本地机器配置好shadowsocksR. 参考网址:https://github.com/getlanter ...

  5. HDU 4661 Message Passing ( 树DP + 推公式 )

    参考了: http://www.cnblogs.com/zhsl/archive/2013/08/10/3250755.html http://blog.csdn.net/chaobaimingtia ...

  6. 推荐系统评测指标--准确率(Precision)和召回率(Recall)、F值(F-Measure)

    转自http://bookshadow.com/weblog/2014/06/10/precision-recall-f-measure/ 1,准确率和召回率是广泛应用于信息检索和统计学分类领域的两个 ...

  7. Lettcode Kth Largest Element in an Array

    Lettcode Kth Largest Element in an Array 题意:在无序数组中,寻找第k大的数字,注意这里考虑是重复的. 一直只会简单的O(nlogn)的做法,听说这题有O(n) ...

  8. BZOJ1025 [SCOI2009]游戏 【置换群 + 背包dp】

    题目链接 BZOJ1025 题解 题意就是问一个\(1....n\)的排列在同一个置换不断重复下回到\(1...n\)可能需要的次数的个数 和置换群也没太大关系 我们只需知道同一个置换不断重复,实际上 ...

  9. BZOJ1821 [JSOI2010]Group 部落划分 Group 【最小生成树】

    题目 聪聪研究发现,荒岛野人总是过着群居的生活,但是,并不是整个荒岛上的所有野人都属于同一个部落,野人们总是拉帮结派形成属于自己的部落,不同的部落之间则经常发生争斗.只是,这一切都成为谜团了--聪聪根 ...

  10. 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond 解题报告

    P1606 [USACO07FEB]荷叶塘Lilypad Pond 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enj ...