fork(12) download
  1. #define DEB
  2.  
  3. #include<bits/stdc++.h>
  4. #include<unistd.h>
  5. #define REP(i,m) for(int i=0;i<(m);++i)
  6. #define REPN(i,m,in) for(int i=(in);i<(m);++i)
  7. #define ALL(t) (t).begin(),(t).end()
  8. #define CLR(a) memset((a),0,sizeof(a))
  9. #define pb push_back
  10. #define mp make_pair
  11. #define fr first
  12. #define sc second
  13.  
  14. using namespace std;
  15.  
  16.  
  17. #ifdef DEB
  18. #define dump(x) cerr << #x << " = " << (x) << endl
  19. #define prl cerr<<"called:"<< __LINE__<<endl
  20. template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
  21. #else
  22. #define dump(x) ;
  23. #define prl ;
  24. template<class T> void debug(T a,T b){ ;}
  25. #endif
  26.  
  27. template<class T> void chmin(T& a,const T& b) { if(a>b) a=b; }
  28. template<class T> void chmax(T& a,const T& b) { if(a<b) a=b; }
  29.  
  30. typedef long long int lint;
  31. typedef pair<int,int> pi;
  32.  
  33. namespace std{
  34. template<class S,class T>
  35. ostream &operator <<(ostream& out,const pair<S,T>& a){
  36. out<<'('<<a.fr<<','<<a.sc<<')';
  37. return out;
  38. }
  39. }
  40.  
  41. //const int INF=5e8;
  42.  
  43. int n,m;
  44.  
  45. vector<pi> g[105];
  46.  
  47. int vis[105];
  48.  
  49. bool dfs(int v,int col,int dst){
  50. vis[v]=1;
  51. if(v==dst) return 1;
  52. REP(i,g[v].size()){
  53. pi e=g[v][i];
  54. if(e.sc==col && !vis[e.fr]){
  55. if(dfs(e.fr,col,dst)) return true;
  56. }
  57. }
  58. return false;
  59. }
  60. int main(){
  61. cin>>n>>m;
  62. REP(i,m){
  63. int a,b,c;cin>>a>>b>>c;--a;--b;--c;
  64. g[a].pb(mp(b,c));
  65. g[b].pb(mp(a,c));
  66. }
  67. int q;cin>>q;
  68. REP(hoge,q){
  69. int res=0;
  70. int u,v;cin>>u>>v;--u;--v;
  71. REP(i,100){
  72. CLR(vis);
  73. if(dfs(u,i,v)) ++res;
  74. }
  75. cout<<res<<endl;
  76. }
  77.  
  78.  
  79. return 0;
  80. }
  81.  
  82.  
  83.  
Success #stdin #stdout 0s 3144KB
stdin
Standard input is empty
stdout
Standard output is empty