The size of the data is small, so it's OK to enumerate the edges of each color with brute force.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<cmath>#include<map>#include<Set>#include<vector>#include<algorithm>#include<stack>#include<queue>#include<cctype>#include<sstream>using namespacestd;#definePII pair<int,int>#defineLL Long Long intConst inteps=1e-8;Const intinf=1000000000;Const intmaxn= -+Ten;intN,m,a,b,c,q,u,v;vector<pii>VV[MAXN];intFLAG[MAXN];voidDfsintSintCO) { if(Flag[s])return; Else{Flag[s]=1; intsiz=vv[s].size (); for(intI=0; i<siz;i++) { if(vv[s][i].second==CO) {DFS (VV[S][I].FIRST,CO); } } return; }}intMain () {//freopen ("In2.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);scanf"%d%d",&n,&m); for(intI=0; i<m;i++) {scanf ("%d%d%d",&a,&b,&c); Vv[a].push_back (Make_pair (b,c)); Vv[b].push_back (Make_pair (a,c)); } scanf ("%d",&q); for(intI=0; i<q;i++) {scanf ("%d%d",&u,&v); intans=0; for(intj=1; j<= -; j + +)//enumerate edges of each color{memset (flag,0,sizeof(flag)); DFS (U,J); Ans+=Flag[v]; } printf ("%d\n", ans); } //fclose (stdin); //fclose (stdout); return 0;}
Codeforces Round #286 (Div. 2) b. Mr Kitayuta's colorful Graph (DFS, violence)