Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Those who cannot remember the past are
- // condemned to repeat it (use DP -_-)
- // - George Santayana
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- #define all(v) v.begin(),v.end()
- #define rall(v) v.rbegin(),v.rend()
- #define ll long long
- #define ull unsigned long long
- #define MOD 1000000007
- #define PI 3.14159265
- #define ceil(a, b) ((a / b) + (a % b ? 1 : 0))
- #define imin INT_MIN
- #define imax INT_MAX
- #define inf 2000000000
- #define nl '\n'
- #define modulo(a, b, mod) ((((a) % mod) * ((b) % mod)) % mod)
- #define debug(x) cout << "x: " << x << nl;
- #define debug2(x, y) cout << "x: " << x << " y: " << y << nl;
- #define ordered_set tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>
- #define ordered_map tree<int, int, less<>, rb_tree_tag, tree_order_statistics_node_update>
- //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
- //vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
- template<typename T = int> istream& operator>>(istream& in, vector<pair<int, int>>& v){
- for (auto& [x, y] : v) in >> x >> y;
- return in;
- }
- template<typename T = int> istream& operator>>(istream& in, vector<T>& v){
- for (T& i : v) in >> i;
- return in;
- }
- template<typename T = int> ostream& operator<<(ostream& out, const vector<T>& v){
- for (const T& x : v)
- out << x << ' ';
- return out;
- }
- template<typename T = pair<int, int>> ostream& operator << (ostream& out, const vector<pair<int, int>>& v){
- for(auto& [x, y] : v){
- out << x << ' ' << y << nl;
- }
- return out;
- }
- void Start_Crushing() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
- struct vertex{
- int to, color;
- vertex(){
- to = color = 0;
- }
- vertex(int to, int color){
- this -> to = to;
- this -> color = color;
- }
- };
- vector<vector<vertex>> adj;
- vector<bool> visited;
- void add_edge(int u, int v, int color){
- adj[u].emplace_back(vertex(v, color));
- adj[v].emplace_back(vertex(u, color));
- }
- bool dfs(int src, int target, int curr){
- if(src == target){
- return true;
- }
- visited[src] = true;
- bool found = false;
- for(auto& [to, color] : adj[src]){
- if(not visited[to] and color == curr){
- found |= dfs(to, target, color);
- }
- }
- return found;
- }
- void solve(){
- int n, m; cin >> n >> m;
- adj.assign(n + 1, vector<vertex>());
- set<int> colors;
- while(m--){
- int u, v, c; cin >> u >> v >> c;
- add_edge(u, v, c);
- colors.insert(c);
- }
- int q; cin >> q;
- while(q--){
- int u, v; cin >> u >> v;
- int ans = 0;
- for(auto& color : colors){
- visited.assign(n + 5, false);
- ans += dfs(u, v, color);
- }
- cout << ans << nl;
- }
- }
- int main(){
- Start_Crushing();
- int t = 1;
- // /*Multiple test cases?*/ cin >> t;
- while (t--) {
- solve();
- if(!t) break;
- cout << nl;
- }
- // for(int tc = 1; tc <= t; tc++){
- // cout << "Case #" << tc << ": ";
- // solve();
- // if(tc != t)
- // cout << nl;
- // }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement