-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCC '07 S3 - Friends
47 lines (42 loc) · 1.29 KB
/
CCC '07 S3 - Friends
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int n;
int stud[10000];
int i;
int a, b;
int cur;
int step;
scanf(" %d",&n);
for(i=0; i<n; i++){
scanf(" %d %d", &a, &b);
a--;
b--;
stud[a] = b; //SINCE ONE FRIEND IS ONLY ASSIGNED TO ONE OTHER FRIEND, WE CAN STORE THIS INFORMATION USING THE INDEX OF AN ARRAY AND ITS VALUE
}
while(1){
scanf(" %d %d", &a, &b);
if(a==0){
return 0;
}
a--;
b--;
cur = a; //WE START FROM THE FIRST INDEX
step = 0;
while(1){
cur = stud[cur]; //WE FOLLOW THE CIRCLE OF FRIENDS BY FOLLOWING THE VALUE OF THE INDICES
if(cur == b){ //IF THE CURRENT INDEX IS EQUAL TO THE OBJECTIVE INDEX, WE'VE REACHED OUR GOAL
printf("Yes %d\n", step);
break;
}
if(cur == a){ //IF THE CURRENT INDEX RETURNS TO THE FIRST INDEX WITHOUT REACHING THE OBJECTIVE INDEX, THEY ARE NOT IN THE SAME CIRCLE OF FRIENDS
printf("No\n");
break;
}
step++; //SINCE THE STEP COUNTER IS ALWAYS THE NUMBER OF FRIENDS IN BETWEEN TWO FRIENDS, WE START AT 0 AND INCREASE AFTER CHECKING
}
}
return 0;
}