rendered paste body#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fin, *fout;
fin = fopen("s3.in", "r");
fout = fopen("s3.out", "w");
int list[10000] = { 0 }; // x must be friends with list[x]
int nstudents;
fscanf(fin, "%d", &nstudents);
for (; nstudents > 0; nstudents--) {
int x, y;
fscanf(fin, "%d %d", &x, &y);
list[x] = y;
}
while (1) {
int from, to;
fscanf(fin, "%d %d", &from, &to);
if (from == 0 && to == 0)
break;
int counter = 0, start = from, counting = 1;
printf("%d %d\n", from, to);
while (1) {
// printf("%d is friends with %d\n", from, list[from]);
from = list[from];
if (from == to)
counting = 0;
if (from == start) {
if (counting == 0)
fprintf(fout, "Yes %d\n", counter);
else
fprintf(fout, "No\n");
break;
}
if (!list[from]) {
fprintf(fout, "No\n");
break;
}
if (counting)
counter++;
}
}
return 0;
}