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.
We need to process queries: given vertices u, v, count number of colors such that in the subgraph consisting of all edges of that color, u and v are connected (directly or indirectly). n ≤ 100, m ≤ 100, q ≤ 100. Colors are up to m, but c_i can be up to m, not necessarily continuous? But we can consider each color as an identifier.
We can precompute for each color a DSU over vertices (n up to 100) or connectivity via BFS. Since n and m are small, we can simply for each query iterate over all colors and check if u and v are connected in that color's graph. But we need to handle up to 100 queries * up to 100 colors = 10,000 checks. For each check, we need to see if there's a path using edges of that color only. We could preprocess per color adjacency list and then for each query and each color do a BFS/DFS from u restricted to that color. That would be O(q * colors * (n+m_color)). Since n small, it's fine.
But we can also precompute for each color a DSU (or connectivity matrix) that tells which vertices are connected in that color. Since n=100, colors ≤ 100, we can for each color, build a DSU of size n, union edges of that color, then for each query we just check if find(u)==find(v) for that color and count. That's simpler: O(m * α(n)) to build, and O(q * colors * α(n)) to answer, which is fine.
Alternatively, we can precompute a boolean matrix colorConn[color][u][v] maybe? But with n=100, color up to 100, that would be 100100100 = 1e6 booleans, still fine. But we can just use DSU per color.
Implementation details: Input edges: a, b, c. Colors are not necessarily from 1 to something, but we can map them. Since c ≤ m, and m ≤ 100, we can allocate an array of DSU for each color index. But colors may be numbers up to m, but some may be missing. So we could collect all distinct color values, but easier: Because m ≤ 100, we can just create DSU for each possible color from 1 to m (or up to 100). But note that color numbers can be up to m, but m ≤ 100, so we can allocate size m+1. However, if there is no edge of a certain color, that DSU will remain all vertices isolated, which is fine. So we can initialize DSU for each color i from 1 to m (or maybe to max_color) with n nodes.
But note: n is up to 100, so we can have arrays.
Edge cases: Multiple edges between same vertices with different colors are allowed. There are no multiple edges of the same color between two vertices (so for each color, at most one edge between any pair). But that doesn't matter.
We'll parse edges: for each edge, read a,b,c. Then add to DSU of color c: union(a,b). We need to consider that vertices are 1-indexed.
Then for each query (u,v): initialize count = 0. For each color from 1 to m: if DSU of that color has find(u)==find(v), increment count. Output count.
Time complexity: O(m * α(n) + q * m * α(n)) = O((m+q)mα) <= about 100*100 = 10k, trivial.
But careful: m could be up to 100, colors numbers could be up to m, but maybe some color numbers exceed m? It says ci ≤ m, so it's bounded.
Color1: unions: all connected to 5, so vertices 1,2,3,4,5 all connected (since each connects to 5). So whole graph connected.
Color2: edges: 1-2,2-3,3-4 => connects 1,2,3,4; vertex 5 isolated.
Queries:
1 5: color1: connected ->1; color2: 5 isolated -> not; count=1.
5 1: same ->1.
2 5: color1->1; count=1.
1 5: same->1.
1 4: color1: yes; color2: 1 and 4 are connected via path 1-2-3-4 -> yes; so count=2.
We need an array of DSU per color. Since max color = m, we can create a vector ds(m+1) but each DSU must be initialized with n. However, we need to call constructor for each DSU. In C++, we can allocate an array of DSU or vector, but then we need to pass n to constructor. Since n is known, we can have a DSU class that takes n in constructor and initializes parent. So we can do: vector dsu(m+1, DSU(n)). But careful: if we create vector with m+1 copies, they all share same? Actually if we do vector dsu(m+1, DSU(n)), it will create m+1 copies of a DSU constructed with n. That's fine as long as DSU copy constructor works properly (should copy parent arrays). But we might rather create each DSU separately: dsu.resize(m+1); for i=0..m: dsu[i] = DSU(n); Or we can use an array of struct.
Simpler: Since n <= 100, we can just have a 2D array parent[color][node] and use simple functions. But using DSU class is fine.
Alternative: Since m and n small, we could even precompute connectivity using Floyd-Warshall for each color? But DSU is enough.
Edge Cases: Color numbers might not be contiguous but it's fine because we allocate up to m, and for colors not appearing, the DSU will have each node isolated. In queries we iterate all colors from 1 to m. That's correct because there are at most m colors (since ci ≤ m and each color appears at least once? Actually if ci can be up to m, it's possible that some numbers in [1,m] do not appear. But that's okay: those colors contribute no connections, so they never count. So answer includes only colors that actually exist and connect u and v. So it's fine.
Potential issue: If there is a color with value > m? But constraints say ci ≤ m, so no.
Thus solution accepted.
Now let's write solution in C++.
Implementation details:
Include , .
Define DSU class with vector parent, size; find, unite, same.
In main:
Read n, m.
Initialize vector dsu(m+1, DSU(n)); // but careful: default constructor? We'll define a constructor DSU(int n) that initializes parent(n+1) and iota.
For each of m edges: read a, b, c. if c between 1 and m: dsu[c].unite(a, b); // else ignore, but won't happen.
Read q.
For each query: read u, v. int ans = 0; for (int col = 1; col <= m; col++) { if (dsu[col].same(u, v)) ans++; } print ans.
Check: DSU copy constructor: if we use vector dsu(m+1, DSU(n)), it will call DSU copy constructor for each element. We need to define or ensure it's properly copied. If we don't define copy constructor, the compiler-generated one will copy the vector member which is fine (since vector manages its own memory). However, we also need to ensure that assignment works. Better to avoid copies: we can do: vector dsu; dsu.reserve(m+1); for (int i=0; i<=m; i++) dsu.emplace_back(n); This constructs each DSU in place.
Simpler: Use array of DSU: DSU dsu[101]; But then we need to call constructor for each element. In C++ we can't call constructor for array elements after declaration unless we use placement new. But we could have DSU default constructed, and then in a loop assign. However, we can define DSU with a method init(n). Or we can store parent as vector<vector> parent for each color. Since small, we can do:
vector<vector> parent(m+1, vector(n+1));
Initialize each parent[col][i] = i.
Then union for each edge: typical DSU operations using the parent for that color. But we need find with path compression; we can write a function that takes a reference to vector& par and does find. But easier: use DSU class.
Let's implement DSU class:
class DSU {
public:
vector par;
DSU(int n) {
par.resize(n+1);
for (int i = 1; i <= n; i++) par[i] = i;
}
int find(int x) {
if (par[x] != x) par[x] = find(par[x]);
return par[x];
}
void unite(int x, int y) {
x = find(x); y = find(y);
if (x != y) par[x] = y;
}
bool same(int x, int y) {
return find(x) == find(y);
}
};
Then main:
int main() {
int n, m;
cin >> n >> m;
vector dsu;
dsu.reserve(m+1);
for (int i = 0; i <= m; i++) {
dsu.emplace_back(n);
}
// But note: dsu[0] will be unused; we have indices 1..m.
// Actually dsu[0] is also constructed, but that's okay.
code
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
dsu[c].unite(a, b);
}
int q;
cin >> q;
while (q--) {
int u, v;
cin >> u >> v;
int ans = 0;
for (int col = 1; col <= m; col++) {
if (dsu[col].same(u, v)) ans++;
}
cout << ans << endl;
}
return 0;
}
But careful: dsu.emplace_back(n) will call constructor DSU(int). We need to have DSU defined before. That works.
Alternatively, we could just create vector dsu(m+1, DSU(n)). This will also work if DSU is copyable. However, if we define a custom copy constructor that does deep copy, it's okay. But default copy constructor will copy the vector par, which is fine because it's a separate vector for each instance. But note: when we do vector dsu(m+1, DSU(n)), it constructs a temporary DSU(n), then copies it m+1 times. That's okay as long as copying works (default copy does memberwise copy). For vector, copying creates a new independent vector with same elements. So each DSU will have its own par vector. That's fine. However, we need to be careful that DSU does not have any manually allocated pointers. So it's fine.
But we must ensure that DSU has a default constructor if we use that? Actually vector dsu(m+1, DSU(n)) requires DSU to be copyable. It doesn't require default constructor. It uses the copy constructor to create m+1 elements from the provided element. That's fine.
But then we may run into the issue that DSU(n) constructs a DSU with size n. Then the copy constructor will be called to create the copies. After that, we might modify each DSU with unite. Since they are separate, it's fine.
Thus both methods are fine.
Now test with sample inputs.
Edge Cases: n up to 100, m up to 100, q up to 100. Works.
But I'd like to provide explanation: precompute DSU for each color, then answer queries.
Now produce final answer.
```cpp
#include
#include
using namespace std;
class DSU {
public:
vector par;
code
DSU(int n) {
par.resize(n + 1);
for (int i = 1; i <= n; ++i) par[i] = i;
}
int find(int x) {
if (par[x] != x) par[x] = find(par[x]);
return par[x];
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x != y) par[x] = y;
}
bool same(int x, int y) {
return find(x) == find(y);
}
};
int main() {
int n, m;
cin >> n >> m;
code
// one DSU for each possible colour (1 .. m)
vector<DSU> dsu(m + 1, DSU(n));
for (int i = 0; i < m; ++i) {
int a, b, c;
cin >> a >> b >> c;
dsu[c].unite(a, b);
}
int q;
cin >> q;
while (q--) {
int u, v;
cin >> u >> v;
int ans = 0;
for (int col = 1; col <= m; ++col) {
if (dsu[col].same(u, v))
++ans;
}
cout << ans << endl;
}
return 0;