Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <list>
  4. using namespace std;
  5.  
  6. class Graph
  7. {
  8.     int V;
  9.     list<int> *adj;
  10.     list<int> *lead;
  11. public:
  12.     Graph(int V);
  13.     void add_edge(int v, int w);
  14.     bool others_leading(int u, int v, int y);
  15.     bool is_reachable(int s, int d);  
  16. };
  17.  
  18. Graph::Graph(int V)
  19. {
  20.     this->V = V;
  21.     adj = new list<int>[V];
  22.     lead = new list<int>[V];
  23. }
  24.  
  25. void Graph::add_edge(int v, int w)
  26. {
  27.     adj[v].push_back(w);
  28.     lead[w].push_back(v);
  29. }
  30.  
  31. bool Graph::others_leading(int u, int v, int y)
  32. {
  33.     list<int>::iterator i;
  34.     for (i = lead[v].begin(); i != lead[v].end(); ++i) if (*i != u) return true;
  35.     for (i = lead[y].begin(); i != lead[y].end(); ++i) if (*i != u && *i != v) return true;
  36.     return false;
  37. }
  38.  
  39. bool Graph::is_reachable(int s, int d)
  40. {
  41.  
  42.     bool *visited = new bool[V];
  43.     for (int i = 0; i < V; i++)
  44.         visited[i] = false;
  45.  
  46.     list<int> queue;
  47.  
  48.     visited[s] = true;
  49.     queue.push_back(s);
  50.  
  51.     list<int>::iterator i;
  52.  
  53.     while (!queue.empty())
  54.     {
  55.         s = queue.front();
  56.         queue.pop_front();
  57.  
  58.         for (i = adj[s].begin(); i != adj[s].end(); ++i)
  59.         {
  60.             if (*i == d)
  61.                 return true;
  62.  
  63.             if (!visited[*i])
  64.             {
  65.                 visited[*i] = true;
  66.                 queue.push_back(*i);
  67.             }
  68.         }
  69.     }
  70.      
  71.     return false;
  72. }
  73.  
  74. int main() {
  75.     int n, m, u, v, y;
  76.     scanf("%d %d", &n, &m);
  77.     Graph g(1000000);
  78.     for (int i = 0; i < m; i++) {
  79.         scanf("%d %d", &u, &v);
  80.         g.add_edge(u, v);
  81.     }
  82.     for (int i = 0; i < n; i++) {
  83.         scanf("%d %d %d", &u, &v, &y);
  84.         if (g.is_reachable(u, y) && g.is_reachable(v, y) && !g.others_leading(u, v, y)) printf("honest\n");
  85.         else printf("liar\n");
  86.     }
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement