//using the breadth-first search to find the result int search_link(int a, int b,struct node* array[], int city){ struct node* temp = array[a]; //find this linked list while(temp->next !=NULL){ temp=temp->next; //if find the answer, return 1 if(temp->num==b){ return 1; } } //move the pointer to the head of the linkedlist temp = array[a]; while(temp->next!=NULL){ temp=temp->next; //start to find another linkedlist that start from the next node in this linkedlist if(search_link(temp->num, b, array, city)==0){ }else{ return 1; } } return 0; }
int main(){ //city and link is the integer that user input int city; int link; //store the pair of two city int a,b; //store the answer of search int result; scanf("%d", &city); scanf("%d", &link); //construct array to store the cities struct node* array[city]; int k; //create all the cities //each city is the head of a linkedlist for(k=1;k<=city;k++){ array[k]=create_node(k); } int i; //add the lines between cities for(i=0;i<link;i++){ scanf("%d %d",&a,&b); //only add the larger number(city) of pair to the small number(city) //like add node to the linkedlist if(a<=b){ struct node* temp=create_node(b); add_node(array[a],temp); }else{ struct node* temp=create_node(a); add_node(array[b],temp); } } //input the query scanf("%d %d",&a,&b); //get the answer by method "search_link" result=search_link(a,b,array,city); if(result==1){ printf("Yes"); } if(result==0){ printf("No"); } //release memory to avoid memory leak for(k=1;k<=city;k++){ while(array[k]->next!=NULL){ array[k]=array[k]->next; free(array[k]); } } return 0; }
/* * Routine to confirm if catalog directory /var/opt/SUNWsamfs/catalog * in standard location is replaced with a link to a directory in * cluster filesystem. * * Return: success = B_TRUE [or] failure = B_FALSE */ boolean_t check_catalog_dir(void) { char *link_name = SAM_CATALOG_DIR; char *mntpt_dir; /* * Check if catalog location is correctly configured on * cluster filesystem */ mntpt_dir = search_link(link_name); if (mntpt_dir == NULL) { scds_syslog(LOG_ERR, "Error in catalog configuration %s !", link_name); dprintf(stderr, "Error in catalog configuration %s !\n", link_name); return (B_FALSE); } /* * Compare if the mount point we got is same as that * supplied by catalog filesystem extended property * in HA-SAM resource type file */ if (strcmp(mntpt_dir, catalogfs_mntpt) != 0) { scds_syslog(LOG_DEBUG, "Catalog %s is not configured on cluster FS %s", link_name, mntpt_dir); dprintf(stderr, "Catalog %s is on not configured cluster FS %s\n", link_name, mntpt_dir); return (B_FALSE); } dprintf(stderr, "Catalog directory %s for link %s configured\n", mntpt_dir, link_name); return (B_TRUE); }
batch_decoder_t * batch_decoder_init_argv(int argc, char *argv[]) { batch_decoder_t *bd; char const *str; bd = ckd_calloc(1, sizeof(*bd)); bd->config = cmd_ln_parse_r(NULL, ms_args_def, argc, argv, FALSE); if ((str = cmd_ln_str_r(bd->config, "-ctl")) == NULL) { E_ERROR("-ctl argument not present, nothing to do in batch mode!\n"); goto error_out; } if ((bd->ctlfh = fopen(str, "r")) == NULL) { E_ERROR_SYSTEM("Failed to open control file '%s'", str); goto error_out; } if ((str = cmd_ln_str_r(bd->config, "-align")) != NULL) { if ((bd->alignfh = fopen(str, "r")) == NULL) { E_ERROR_SYSTEM("Failed to open align file '%s'", str); } } if ((str = cmd_ln_str_r(bd->config, "-hyp")) != NULL) { if ((bd->hypfh = fopen(str, "w")) == NULL) { E_ERROR_SYSTEM("Failed to open hypothesis file '%s'", str); } } if ((bd->sf = search_factory_init_cmdln(bd->config)) == NULL) goto error_out; if ((str = cmd_ln_str_r(bd->config, "-fwdtreelm")) != NULL) { if ((bd->fwdtree = search_factory_create(bd->sf, NULL, "fwdtree", "-fwdtreelm", str, NULL)) == NULL) goto error_out; if ((bd->fwdflat = search_factory_create(bd->sf, NULL, "fwdflat", NULL)) == NULL) goto error_out; } else { if ((bd->fwdtree = search_factory_create(bd->sf, NULL, "fwdtree", NULL)) == NULL) goto error_out; if ((bd->fwdflat = search_factory_create(bd->sf, bd->fwdtree, "fwdflat", NULL)) == NULL) goto error_out; } if ((str = cmd_ln_str_r(bd->config, "-vm")) != NULL) { vocab_map_t *vm = vocab_map_init(search_factory_d2p(bd->sf)->dict); FILE *vmfh; if (vm == NULL) goto error_out; if ((vmfh = fopen(str, "r")) == NULL) { vocab_map_free(vm); goto error_out; } if (vocab_map_read(vm, vmfh) < 0) { vocab_map_free(vm); goto error_out; } fclose(vmfh); fwdflat_search_set_vocab_map(bd->fwdflat, vm); } //if ((bd->latgen = search_factory_create(bd->sf, "latgen", NULL)) == NULL) //goto error_out; search_link(bd->fwdtree, bd->fwdflat, "fwdtree", FALSE); // search_link(bd->fwdflat, bd->latgen, "fwdflat", TRUE); search_set_cb(bd->fwdtree, search_cb, bd); search_set_cb(bd->fwdflat, search_cb, bd); bd->hypfiles = hash_table_new(0, FALSE); if ((str = cmd_ln_str_r(bd->config, "-hypprefix"))) { char *hypfile; FILE *hypfh; hypfile = string_join(str, ".fwdtree.hyp", NULL); hypfh = fopen(hypfile, "w"); if (hypfh == NULL) { E_ERROR_SYSTEM("Could not open %s", hypfile); } else { hash_table_enter(bd->hypfiles, "fwdtree", hypfh); } ckd_free(hypfile); hypfile = string_join(str, ".fwdflat.hyp", NULL); hypfh = fopen(hypfile, "w"); if (hypfh == NULL) { E_ERROR_SYSTEM("Could not open %s", hypfile); } else { hash_table_enter(bd->hypfiles, "fwdflat", hypfh); } ckd_free(hypfile); } return bd; error_out: return NULL; }