fork(6) 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. struct uf{
  46. static const int MAXN=105;
  47. int par[MAXN];
  48. int size[MAXN];
  49. void init(){
  50. memset(par,-1,sizeof(par));
  51. REP(i,MAXN) size[i]=1;
  52. }
  53. int root(int a){
  54. if(par[a]==-1) return a;
  55. return par[a]=root(par[a]);
  56. }
  57. void unite(int a,int b){
  58. a=root(a);b=root(b);
  59. if(a==b) return;
  60. if(size[a]<size[b]) swap(a,b);
  61.  
  62. par[b]=a;
  63. size[a]+=size[b];
  64. }
  65. bool same(int a,int b){
  66. return root(a)==root(b);
  67. }
  68. };
  69. uf u_[105];
  70.  
  71.  
  72. int main(){
  73. REP(i,100) u_[i].init();
  74. cin>>n>>m;
  75. REP(i,m){
  76. int a,b,c;cin>>a>>b>>c;--a;--b;--c;
  77. u_[c].unite(a,b);
  78. }
  79. int q;cin>>q;
  80. REP(hoge,q){
  81. int res=0;
  82. int u,v;cin>>u>>v;--u;--v;
  83. REP(i,100) if(u_[i].same(u,v)) ++res;
  84. cout<<res<<endl;
  85. }
  86.  
  87.  
  88. return 0;
  89. }
  90.  
  91.  
  92.  
Success #stdin #stdout 0s 3184KB
stdin
Standard input is empty
stdout
Standard output is empty