-
Notifications
You must be signed in to change notification settings - Fork 0
/
B.cpp
54 lines (50 loc) · 1010 Bytes
/
B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Took help from editorial
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
vector<pair<int,int> > adj[105];
int visited[105];
bool dfs(int u,int c,int v)
{
visited[u]=1;
if(u==v)
return 1;
for(int i=0;i<adj[u].size();i++)
{
pair<int,int> x=adj[u][i];
if(x.second==c && !visited[x.first])
{
if(dfs(x.first,c,v))
return true;
}
}
return false;
}
int main()
{
fastio;
int n,m,i,a,b,c,q,u,v;
cin>>n>>m;
for(i=0;i<m;i++)
{
cin>>a>>b>>c;
a--; b--; c--;
adj[a].push_back(make_pair(b,c));
adj[b].push_back(make_pair(a,c));
}
cin>>q;
while(q--)
{
cin>>u>>v;
u--; v--;
int ans=0;
for(i=0;i<100;i++)
{
memset((visited),0,sizeof(visited));
if(dfs(u,i,v))
ans++;
}
cout<<ans<<"\n";
}
return 0;
}