Advertisement
El_GEMMY

colorful graph (dfs)

Mar 28th, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. // Those who cannot remember the past are
  2. // condemned to repeat it (use DP -_-)
  3. // - George Santayana
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8.  
  9. using namespace std;
  10. using namespace __gnu_pbds;
  11.  
  12. #define all(v) v.begin(),v.end()
  13. #define rall(v) v.rbegin(),v.rend()
  14. #define ll long long
  15. #define ull unsigned long long
  16. #define MOD 1000000007
  17. #define PI 3.14159265
  18. #define ceil(a, b) ((a / b) + (a % b ? 1 : 0))
  19. #define imin INT_MIN
  20. #define imax INT_MAX
  21. #define inf 2000000000
  22. #define nl '\n'
  23. #define modulo(a, b, mod) ((((a) % mod) * ((b) % mod)) % mod)
  24. #define debug(x) cout << "x: " << x << nl;
  25. #define debug2(x, y) cout << "x: " << x << " y: " << y << nl;
  26. #define ordered_set tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>
  27. #define ordered_map tree<int, int, less<>, rb_tree_tag, tree_order_statistics_node_update>
  28.  
  29. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  30. //vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
  31.  
  32. template<typename T = int> istream& operator>>(istream& in, vector<pair<int, int>>& v){
  33.     for (auto& [x, y] : v) in >> x >> y;
  34.     return in;
  35. }
  36.  
  37. template<typename T = int> istream& operator>>(istream& in, vector<T>& v){
  38.     for (T& i : v) in >> i;
  39.     return in;
  40. }
  41.  
  42. template<typename T = int> ostream& operator<<(ostream& out, const vector<T>& v){
  43.     for (const T& x : v)
  44.         out << x << ' ';
  45.     return out;
  46. }
  47.  
  48. template<typename T = pair<int, int>> ostream& operator << (ostream& out, const vector<pair<int, int>>& v){
  49.     for(auto& [x, y] : v){
  50.         out << x << ' ' << y << nl;
  51.     }
  52.     return out;
  53. }
  54.  
  55. void Start_Crushing() {
  56.     ios_base::sync_with_stdio(false);
  57.     cin.tie(nullptr);
  58.     cout.tie(nullptr);
  59. #ifndef ONLINE_JUDGE
  60.     freopen("input.txt", "r", stdin);
  61.     freopen("output.txt", "w", stdout);
  62. #endif
  63. }
  64.  
  65. struct vertex{
  66.    int to, color;
  67.    vertex(){
  68.        to = color = 0;
  69.    }
  70.    vertex(int to, int color){
  71.        this -> to = to;
  72.        this -> color = color;
  73.    }
  74. };
  75.  
  76. vector<vector<vertex>> adj;
  77. vector<bool> visited;
  78.  
  79. void add_edge(int u, int v, int color){
  80.     adj[u].emplace_back(vertex(v, color));
  81.     adj[v].emplace_back(vertex(u, color));
  82. }
  83.  
  84. bool dfs(int src, int target, int curr){
  85.     if(src == target){
  86.         return true;
  87.     }
  88.  
  89.     visited[src] = true;
  90.  
  91.     bool found = false;
  92.     for(auto& [to, color] : adj[src]){
  93.         if(not visited[to] and color == curr){
  94.             found |= dfs(to, target, color);
  95.         }
  96.     }
  97.     return found;
  98. }
  99.  
  100. void solve(){
  101.     int n, m; cin >> n >> m;
  102.     adj.assign(n + 1, vector<vertex>());
  103.  
  104.  
  105.     set<int> colors;
  106.     while(m--){
  107.         int u, v, c; cin >> u >> v >> c;
  108.         add_edge(u, v, c);
  109.         colors.insert(c);
  110.     }
  111.  
  112.     int q; cin >> q;
  113.     while(q--){
  114.         int u, v; cin >> u >> v;
  115.         int ans = 0;
  116.  
  117.         for(auto& color : colors){
  118.             visited.assign(n + 5, false);
  119.             ans += dfs(u, v, color);
  120.         }
  121.         cout << ans << nl;
  122.     }
  123.  
  124. }
  125.  
  126. int main(){
  127.     Start_Crushing();
  128.  
  129.     int t = 1;
  130. //    /*Multiple test cases?*/ cin >> t;
  131.     while (t--) {
  132.         solve();
  133.         if(!t) break;
  134.         cout << nl;
  135.     }
  136.  
  137. //    for(int tc = 1; tc <= t; tc++){
  138. //        cout << "Case #" << tc << ": ";
  139. //        solve();
  140. //        if(tc != t)
  141. //            cout << nl;
  142. //    }
  143.  
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement