#include // main rec funcion int check (int dest, int size, int friends[size][2], int checked[], int next) { // if it found dest, returns 1 if (dest == next) { return 1; } else { // otherwise, it iterates for every line of input. Grabs a number checks if its // related to another and calls this function again. Does this again till it finds // dest. If all numbers are visited and current dest can't be found it returns 0 for (int i = 0; i < size; i++) { if (checked[i] == 0) { if (friends[i][0] == next) { checked[i]++; if (check(dest, size, friends, checked, friends[i][1])) { return 1; } } else if (friends[i][1] == next) { checked[i]++; if (check(dest, size, friends, checked, friends[i][0])) { return 1; } } } } } return 0; } int main (int argc, char *argv[]) { // declaration and assigning int size, first, sec; scanf("%d %d %d", &size, &first, &sec); int friends[size][2], checked[1000]; // scans size lines of input for (int i = 0; i < size; i++) { scanf("%d %d", &friends[i][0], &friends[i][1]); } // calls main recursive funcion, prints appropiate output if (check(sec, size, friends, checked, friends[0][0])) { printf("YES\n"); } else { printf("NO\n"); } }