int main() { int schoolNum; scanf("%d", &schoolNum); int i, j; for(i = 1; i <= schoolNum; i++){ int counter = -1; do { scanf("%d", &schools[i][++counter]); } while(schools[i][counter]); } for(i = 1; i <= schoolNum; i++){ if(network[i] == 0) countLinks(i, i); } int uniqueCount = 0; visited[uniqueCount++] = network[1]; for(i = 1; i <= schoolNum; i++){ int different = 1; for(j = 0; j < uniqueCount; j++){ if(network[i] == visited[j]) different = 0; } if(different){ visited[uniqueCount++] = network[i]; } } printf("%d\n", uniqueCount); return 0; }
int getifaddrs(struct ifaddrs **ifap) { if(!ifap) { return -1; } *ifap = NULL; int l_socket = netlink_socket(); if(l_socket < 0) { return -1; } NetlinkList *l_linkResults = getResultList(l_socket, RTM_GETLINK); if(!l_linkResults) { close(l_socket); return -1; } NetlinkList *l_addrResults = getResultList(l_socket, RTM_GETADDR); if(!l_addrResults) { close(l_socket); freeResultList(l_linkResults); return -1; } unsigned l_numLinks = countLinks(l_socket, l_linkResults) + countLinks(l_socket, l_addrResults); struct ifaddrs *l_links[l_numLinks]; memset(l_links, 0, l_numLinks * sizeof(struct ifaddrs *)); if (interpret(l_socket, l_linkResults, l_links, ifap) == -1) { return -1; } if (interpret(l_socket, l_addrResults, l_links, ifap) == -1) { return -1; } freeResultList(l_linkResults); freeResultList(l_addrResults); close(l_socket); return 0; }
void countLinks(int school, int networkNum){ if(visited[school]){ return; } visited[school] = 1; int i = -1; while(1){ if(schools[school][++i] == 0){ break; } countLinks(schools[school][i], networkNum); } network[school] = networkNum; visited[school] = 0; return; }