Codeforces Round #286 (Div. 2) b. Mr Kitayuta's colorful Graph +foyd algorithm application

Source: Internet
Author: User

B. Mr Kitayuta ' s colorful graphtime limit per test1 secondmemory limit per test256 megabytesinputstandard Inputoutputstan Dard output

Mr. Kitayuta have just bought an undirected graph consisting ofNVertices andmEdges. The vertices of the graph is numbered from 1 toN. Each edge, namely edgeI, 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 and Integers-  u i  and  v i .

Find the number of the colors that satisfy the following condition:the edges of this color connect vertex u C2>i and vertex vi directly or indirectly.

Input

The first line of the input contains space-separated, integers- n and m (2?≤? n? ≤?100,?1?≤? m≤?100), denoting the number of the vertices and the number of the edges, respectively.

The nextmLines contain space-separated three integers- ai , bi (1?≤? a i? < b i? ≤? N ) and Ci (1?≤? C i? ≤? m ). Note that there can is multiple edges between the vertices. However, there is no multiple edges of the same color between-vertices, that's, 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 followsQLines, containing space-separated, integers- ui and vi (1?≤? u i,? v i? ≤? N ). It's Guaranteed that ui? ≠? v I .

Output

For each query, print the answer-a separate line.

Sample Test (s) 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
Note

Let ' s consider the first sample.

The figure above shows the first sample.
  • vertex 1  and 
  • Vertex 3 and Vertex 4 is connected by Color 3.

    • Vertex 1 and Vertex 4 is not connected by any single color.
Solution: The problem is to create a diagram, there are many different colors of the edge, each asked two sides of the number of different colors of the path. By this associative Floyd algorithm, you can enumerate the colors of the edges, ask for the shortest path for each color, and then count the shortest number of different colors. Code
<pre name= "code" class= "CPP" > #include <iostream> #include <cstdio> #include <cstring>using    Namespace Std;int mat[102][102][102];int Main () {int n,m;        while (~SCANF ("%d%d", &n,&m)) {memset (mat,0,sizeof (Mat));            for (int i=1; i<=m; i++) {int a,b,c;            scanf ("%d%d%d", &a,&b,&c);            Mat[a][b][c]=1;        Mat[b][a][c]=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 (Mat[i][k][c]&&mat[k][j][c]) {Mat[i][j]                        [C]=1;                    Mat[j][i][c]=1;        }}}} int q;        scanf ("%d", &q);            for (int i=0;i<q;i++) {int A, B;            int sum=0;            scanf ("%d%d", &a,&b); for (int i=1;i<=m;i++) {sum+=mat[a][b][i];            } printf ("%d\n", sum); }} return 0;}


Codeforces Round #286 (Div. 2) b. Mr Kitayuta's colorful Graph +foyd algorithm application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.