Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- bool dfs(int node,char* c,vector<int>* gr)
- {
- c[node] = 'o';
- bool f = false;
- for(int i : gr[node])
- {
- if(c[i] == 'o')
- {
- return true;
- }
- else if(c[i] == 0 && dfs(i,c,gr))
- {
- return true;
- }
- }
- c[node] = 'c';
- return false;
- }
- int main()
- {
- std::ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- int t,n,m,from,to,w;
- cin >> t;
- for(int j = 0;j<t;j++)
- {
- vector<int> gr[210000];
- cin >> n >> m;
- for(int i = 0;i<m;i++)
- {
- cin >> from >> to >> w;
- gr[from].push_back(to);
- }
- char color[210000];
- bool c = false;
- for(int p = 0;p<=n;p++)
- {
- color[p] = 0;
- }
- for(int k = 1;k<=n;k++)
- {
- if(color[k] == 0 && dfs(k,color,gr))
- {
- c = true;
- break;
- }
- }
- if(c)
- {
- cout << "true ";
- }
- else
- {
- cout << "false ";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement