1 A colorful run Mr Brdrcrbri is a fitness coach of 2I1T Bhagyanagar. He wants his…
- User
1 A colorful run Mr Brdrcrbri is a fitness coach of 2I1T Bhagyanagar. He wants his students to remain healthy, but the“divoc” pandemic has made this difficult for him. Since he is a man of science, he has decided to separate the students into q different groups and tell them to maintain one hand distance while running wearing a mask. He has come up with a map of 2I1T Bhagyanagar. There are n checkpoints, and m roads connecting these checkpoints. He has assigned every road a color c. He plans on making group i run from a checkpoint ui to vi using roads of a single color. After drawing the map he gave you and your friend, “raseaC”, the task to count the number of colors using which group i can go from ui to vi using roads of a single color, but in typical 2I1T Bhagyanagar custom, you shifted the work by 17 days to do it on the final day. Quickly come up with the answers to the q queries Mr Brdrcrbri asks you. 1.1 Input format The first line contains two integers, n(2 ≤ n ≤ 100) and m(1 ≤ m ≤ 1000), denoting the number of checkpoints and the number of roads respectively. The next m lines contain three integers, ai , bi(1 ≤ ai < bi ≤ n) and ci(1 ≤ c ≤ m), denoting a road joining the checkpoints ai and bi with color ci . The roads are undirected, you can travel from ai to bi and from bi to ai using the same road. The next line contains a integer q(1 ≤ q ≤ 100), the number of groups. Then the next q lines contain two integers ui and vi(1 ≤ ui , vi ≤ n, ui ̸= vi), the starting and ending checkpoint of group i. Note, it is not necessary for all checkpoints to be connected by roads. 1.2 Output format Output q integers, where the i th line denotes the number of colors such that it is possible to travel from ui to vi using only a single color. 1 1.3 Examples 1.3.1 1 Input Output 7 7 2 1 2 1 1 1 7 2 0 2 3 1 0 2 4 3 2 5 3 2 7 2 3 5 3 4 1 2 3 4 1 4 2 6
give code in c. use adjascency list
- Assistant
- User
roaDS ARE UNDIRECTED RIGHT
- Assistant
Yes, the roads are undirected, as mentioned in the initial description of your problem. In the code I provided, this undirected relationship is handled by adding edges in both directions within the
addEdgefunction:cvoid addEdge(Node* graph[MAX_N][MAX_M], int a, int b, int c) { Node* newNode = createNode(b); newNode->next = graph[a][c]; graph[a][c] = newNode; newNode = createNode(a); // This adds the reverse connection, making the road undirected newNode->next = graph[b][c]; graph[b][c] = newNode; }For each road connecting checkpoints
aandbwith a colorc, the code adds an edge fromatoband another frombtoa, ensuring that you can travel both ways on the same road. This effectively treats the roads as undirected in the graph representation.Thus, during the search (like in
dfs), it's possible to traverse from to and from to using the same colored path, adhering to the undirected nature of the roads as specified. - User
USE vector<vector<pair<int, int>>> adj(v + 1); FOR ADJASCENCY LIST
- Assistant
- User
#include <bits/stdc++.h> using namespace std;
void add_edge(vector<vector<pair<int, int>>> &adj, int u, int v, int c) { adj[u].push_back(make_pair(v, c)); adj[v].push_back(make_pair(u, c)); }
bool dfs(vector<vector<pair<int, int>>> &adj, int v, int color, vector &visited, int dest) { visited[v] = true; if (v == dest) return true; for (auto &edge : adj[v]) { int next = edge.first; int edgeColor = edge.second; if (!visited[next] && edgeColor == color) { if (dfs(adj, next, color, visited, dest)) return true; } } return false; }
int main() { int n, m, q; cin >> n >> m; vector<vector<pair<int, int>>> adj(n); adj.clear(); adj.resize(n + 1);
codefor (int i = 0; i < m; i++) { int u, v, c; cin >> u >> v >> c; addEdge(adj, u, v, c); } cin >> q; while (q-- != 0) { int u, v; cin >> u >> v; int count = 0; vector<bool> visited(n + 1, false); for (int color = 1; color <= m; color++) { fill(visited.begin(), visited.end(), false); if (dfs(adj, u, color, visited, v)) { count++; } } cout << count << endl; } return 0; - Assistant