fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define go ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
  4. using namespace std;
  5.  
  6. int const N = 120;
  7. int n;
  8. vector<pair<int, int>> adj[N];
  9. bool visitd[N];
  10. void clearVisited()
  11. {
  12. for (int i = 0; i < N; i++)
  13. {
  14. visitd[i] = false;
  15. }
  16. }
  17. bool dfs(int s, int t, int c)
  18. {
  19. if (visitd[s])
  20. return false;
  21. visitd[s] = true;
  22. if (s == t)
  23. return true;
  24. if (s > n)
  25. return false;
  26. bool ans = false;
  27. for (auto i : adj[s])
  28. {
  29. if (i.second == c)
  30. {
  31. ans = ans || dfs(i.first, t, c);
  32. }
  33. }
  34. return ans;
  35. }
  36. void solve()
  37. {
  38. int m;
  39. cin >> n >> m;
  40. for (int i = 0; i < m; i++)
  41. {
  42. int s, d, c;
  43. cin >> s >> d >> c;
  44. adj[s].push_back({d, c});
  45. adj[d].push_back({s, c});
  46. }
  47. int q;
  48. cin >> q;
  49. for (int i = 0; i < q; i++)
  50. {
  51.  
  52. int u, v;
  53. cin >> u >> v;
  54. int ans = 0;
  55. for (int i = 1; i <= 100; i++)
  56. {
  57. clearVisited();
  58. if (dfs(u, v, i))
  59. ans++;
  60. }
  61. cout << ans << endl;
  62. }
  63. }
  64.  
  65. int main()
  66. {
  67. int t = 1;
  68. // cin>>t;
  69. // go;
  70. while (t--)
  71. {
  72. solve();
  73. cout << "\n";
  74. }
  75. return 0;
  76. }
Success #stdin #stdout 0.01s 5428KB
stdin
5 7
1 5 1
2 5 1
3 5 1
4 5 1
1 2 2
2 3 2
3 4 2
5
1 5
5 1
2 5
1 5
1 4
stdout
1
1
1
1
2