█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 5626

Problema 0x1c - Decir si un nodo es ancestro de otro en un árbol

  • Autor: nivalderramas
  • Fecha: 2022-01-28 00:58:40 UTC (Hace alrededor de 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.001 s 0 KBi
#2
Correcto
0.001 s 0 KBi
#3
Correcto
0.004 s 29 KBi
#4
Correcto
0.003 s 0 KBi
#5
Correcto
0.004 s 2 KBi
#6
Correcto
0.005 s 1 KBi
#7
Correcto
0.003 s 0 KBi
#8
Correcto
0.002 s 0 KBi
#9
Incorrecto
0.004 s 0 KBi
#10
Incorrecto
0.001 s 0 KBi
#11
Correcto
0.004 s 1 KBi
#12
Incorrecto
0.098 s 13 KBi
#13
Incorrecto
0.262 s 13 KBi
#14
Incorrecto
0.095 s 13 KBi
#15
Correcto
0.508 s 73 KBi
#16
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  ./a.out
0.531 s 125 KBi
#17
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  ./a.out
0.341 s 125 KBi
#18
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  ./a.out
0.379 s 125 KBi
#19
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  ./a.out
0.381 s 125 KBi
#20
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  ./a.out
0.385 s 125 KBi
Puntos totales: 50 / 100

Código

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define ff first
#define ss second
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define lli long long int
#define fast_io                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
ostream &operator<<(ostream &os, const vector<int> &v) {
  for (auto const &i : v) {
    os << i << " ";
  }
  os << endl;
  return os;
}
const lli mod = (1e9) + 7;
const int N = 1e4 + 1;
int main() {
  fast_io;
  int n;
  cin >> n;
  vector<set<int>> g(n); // just save father of node
  int p, h;
  REP(i, n - 1) {
    cin >> p >> h;
    g[h] = g[p];
    g[h].insert(p);
  }
  int c;
  cin >> c;
  int a, b;
  while (c--) {
    cin >> a >> b; // check if a is ancestor of b
    bool encountered = false;
    if( a == b )encountered = true;
    if(g[b].find(a) != g[b].end()){
      encountered = true;
    }
    cout<<(encountered ? "Yes" : "No")<<endl;
  }
  return 0;
}