Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8.  
  9.  
  10. bool dfs(int node,char* c,vector<int>* gr)
  11. {
  12.     c[node] = 'o';
  13.     bool f = false;
  14.     for(int i : gr[node])
  15.     {
  16.         if(c[i] == 'o')
  17.         {
  18.             return true;
  19.         }
  20.         else if(c[i] == 0 && dfs(i,c,gr))
  21.         {
  22.             return true;
  23.         }
  24.     }
  25.     c[node] = 'c';
  26.     return false;
  27. }
  28.  
  29. int main()
  30. {
  31.     std::ios_base::sync_with_stdio(false);
  32.     cin.tie(nullptr);
  33.     int t,n,m,from,to,w;
  34.     cin >> t;
  35.     for(int j = 0;j<t;j++)
  36.     {
  37.     vector<int> gr[210000];
  38.     cin >> n >> m;
  39.     for(int i = 0;i<m;i++)
  40.     {
  41.         cin >> from >> to >> w;
  42.         gr[from].push_back(to);
  43.     }
  44.     char color[210000];
  45.     bool c = false;
  46.         for(int p = 0;p<=n;p++)
  47.         {
  48.             color[p] = 0;
  49.         }
  50.     for(int k = 1;k<=n;k++)
  51.     {
  52.        
  53.         if(color[k] == 0 && dfs(k,color,gr))
  54.         {
  55.             c = true;
  56.             break;
  57.         }
  58.     }
  59.         if(c)
  60.         {
  61.             cout << "true ";
  62.         }
  63.         else
  64.         {
  65.             cout << "false ";
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement