Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <unordered_map>
- using namespace std;
- bool dfc(vector<bool>& visited, vector<bool>& pathVisited, int start, unordered_map<int, vector<int>>& graph){
- if(pathVisited[start])
- return true;
- pathVisited[start] = true;
- for(int neighbour : graph[start]){
- if(dfc(visited, pathVisited, neighbour, graph))
- return true;
- }
- pathVisited[start] = false;
- visited[start] = true;
- return false;
- }
- bool isCyclic(unordered_map<int, vector<int>>& graph, int v){
- vector<bool> visited(v, false);
- for(int i = 0; i < v; i++) {
- if(!visited[i]){
- vector<bool> pathVisited(v, false);
- if(dfc(visited, pathVisited, i, graph))
- return true;
- }
- }
- return false;
- }
- bool play(){
- int v, e;
- int a, b, c;
- cin >> v >> e;
- unordered_map<int, vector<int>> graph;
- for(int i = 0; i < e; i++){
- cin >> a >> b >> c;
- graph[a].push_back(b);
- }
- return isCyclic(graph, v);
- }
- int main() {
- int n;
- cin >> n;
- for(int i = 0; i < n; i++){
- if(play())
- cout << "true ";
- else
- cout << "false ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment