forked from it-robot/apcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
q2.c
59 lines (47 loc) · 943 Bytes
/
q2.c
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
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Created on: 2017¦~8¤ë20¤é
* ¤j¾Çµ{¦¡³]p¥ý×ÀË´ú¡]APCS¡^
* Author: ¦ãîà¾Ç°|
* http://www.ittraining.com.tw/ittraining/it-elearning/apcs
*
*/
#include <stdio.h>
#define TEST 1
int find_group(int *a,int *b,int index);
int main()
{
int i;
int g=0;
int n;
int y[1000]={0};
#if TEST
int x[]={4,7,2,9,6,0,8,1,5,3};
//int x[]={0,2,1};
n=sizeof(x)/sizeof(x[0]);
#else
int x[1000]={0};
//get_input
scanf("%d",&n); //for n
for (i=0;i<n;i++) { //for relation-ship
scanf("%d",&x[i]);
}
#endif
for (i=0;i<n;i++)
if (y[i]==0) {
g++;
find_group(x,y,i);
}
printf("%d\n",g);
system("PAUSE");
return 0;
}
int find_group(int *a,int *b,int index)
{
int i=index;
int my_friend;
my_friend=a[i];
while (my_friend!=i) {
b[my_friend]=1; //visited
my_friend=a[my_friend];
}
}