#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<vector<int>> wektor;
vector<int> odwiedzone;
vector<int> komponent; // Przechowuje numer komponentu spójności dla każdego wierzchołka
void BFS(int V, int componentId) {
queue<int> q;
q.push(V);
odwiedzone[V] = 1;
komponent[V] = componentId; // Przypisanie wierzchołka do komponentu
while (!q.empty()) {
int p = q.front();
q.pop();
for (int j : wektor[p]) {
if (odwiedzone[j] == 0) {
odwiedzone[j] = 1;
komponent[j] = componentId; // Przypisanie wierzchołka do komponentu
q.push(j);
}
}
}
}
int main() {
int a {0}, b {0};
int ile_v {0};
int ile_e {0};
// Wczytaj liczbę plemion i dróg
cin >> ile_v >> ile_e;
wektor.resize(ile_v + 1);
odwiedzone.resize(ile_v + 1, 0);
komponent.resize(ile_v + 1, 0);
// Wczytaj drogi
for (int i = 0; i < ile_e; i++) {
cin >> a >> b;
wektor[a].push_back(b);
wektor[b].push_back(a);
}
// Znajdź komponenty spójności
int componentId = 0;
for (int i = 1; i <= ile_v; i++) {
if (odwiedzone[i] == 0) {
componentId++;
BFS(i, componentId);
}
}
// Wczytaj liczbę zapytań
int ile_q {0};
cin >> ile_q;
// Odpowiedz na zapytania
for (int i = 0; i < ile_q; i++) {
int p, k;
cin >> p >> k;
if (komponent[p] == komponent[k]) {
cout << "TAK\n";
} else {
cout << "NIE\n";
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8cXVldWU+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2ZWN0b3I8dmVjdG9yPGludD4+IHdla3RvcjsKdmVjdG9yPGludD4gb2R3aWVkem9uZTsKdmVjdG9yPGludD4ga29tcG9uZW50OyAvLyBQcnplY2hvd3VqZSBudW1lciBrb21wb25lbnR1IHNww7Nqbm/Fm2NpIGRsYSBrYcW8ZGVnbyB3aWVyemNob8WCa2EKCnZvaWQgQkZTKGludCBWLCBpbnQgY29tcG9uZW50SWQpIHsKICAgIHF1ZXVlPGludD4gcTsKICAgIHEucHVzaChWKTsKICAgIG9kd2llZHpvbmVbVl0gPSAxOwogICAga29tcG9uZW50W1ZdID0gY29tcG9uZW50SWQ7IC8vIFByenlwaXNhbmllIHdpZXJ6Y2hvxYJrYSBkbyBrb21wb25lbnR1CgogICAgd2hpbGUgKCFxLmVtcHR5KCkpIHsKICAgICAgICBpbnQgcCA9IHEuZnJvbnQoKTsKICAgICAgICBxLnBvcCgpOwoKICAgICAgICBmb3IgKGludCBqIDogd2VrdG9yW3BdKSB7CiAgICAgICAgICAgIGlmIChvZHdpZWR6b25lW2pdID09IDApIHsKICAgICAgICAgICAgICAgIG9kd2llZHpvbmVbal0gPSAxOwogICAgICAgICAgICAgICAga29tcG9uZW50W2pdID0gY29tcG9uZW50SWQ7IC8vIFByenlwaXNhbmllIHdpZXJ6Y2hvxYJrYSBkbyBrb21wb25lbnR1CiAgICAgICAgICAgICAgICBxLnB1c2goaik7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KCmludCBtYWluKCkgewogICAgaW50IGEgezB9LCBiIHswfTsKICAgIGludCBpbGVfdiB7MH07CiAgICBpbnQgaWxlX2UgezB9OwoKICAgIC8vIFdjenl0YWogbGljemLEmSBwbGVtaW9uIGkgZHLDs2cKICAgIGNpbiA+PiBpbGVfdiA+PiBpbGVfZTsKCiAgICB3ZWt0b3IucmVzaXplKGlsZV92ICsgMSk7CiAgICBvZHdpZWR6b25lLnJlc2l6ZShpbGVfdiArIDEsIDApOwogICAga29tcG9uZW50LnJlc2l6ZShpbGVfdiArIDEsIDApOwoKICAgIC8vIFdjenl0YWogZHJvZ2kKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgaWxlX2U7IGkrKykgewogICAgICAgIGNpbiA+PiBhID4+IGI7CiAgICAgICAgd2VrdG9yW2FdLnB1c2hfYmFjayhiKTsKICAgICAgICB3ZWt0b3JbYl0ucHVzaF9iYWNrKGEpOwogICAgfQoKICAgIC8vIFpuYWpkxboga29tcG9uZW50eSBzcMOzam5vxZtjaQogICAgaW50IGNvbXBvbmVudElkID0gMDsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IGlsZV92OyBpKyspIHsKICAgICAgICBpZiAob2R3aWVkem9uZVtpXSA9PSAwKSB7CiAgICAgICAgICAgIGNvbXBvbmVudElkKys7CiAgICAgICAgICAgIEJGUyhpLCBjb21wb25lbnRJZCk7CiAgICAgICAgfQogICAgfQoKICAgIC8vIFdjenl0YWogbGljemLEmSB6YXB5dGHFhAogICAgaW50IGlsZV9xIHswfTsKICAgIGNpbiA+PiBpbGVfcTsKCiAgICAvLyBPZHBvd2llZHogbmEgemFweXRhbmlhCiAgICBmb3IgKGludCBpID0gMDsgaSA8IGlsZV9xOyBpKyspIHsKICAgICAgICBpbnQgcCwgazsKICAgICAgICBjaW4gPj4gcCA+PiBrOwogICAgICAgIGlmIChrb21wb25lbnRbcF0gPT0ga29tcG9uZW50W2tdKSB7CiAgICAgICAgICAgIGNvdXQgPDwgIlRBS1xuIjsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBjb3V0IDw8ICJOSUVcbiI7CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiAwOwp9