1032 Sharing

代码如下:

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<stdio.h>
 5 #include<memory.h>
 6 #include<limits>
 7 using namespace std;
 8 int main(){
 9     int *m = new int[100001];
10     int *exist = new int[100001];
11     memset(m,-1,sizeof(m));
12     memset(exist,-1,sizeof(exist));
13     int sum;
14     int a,b;
15     scanf("%d %d %d",&a,&b,&sum);
16     m[a] = -1;
17     m[b] = -1;
18     for(int i = 0; i < sum; i++){
19         char tem[10];
20         int pre,next;
21         scanf("%d %s %d",&pre,tem,&next);
22         m[pre] = next;
23     }
24     int head = a;
25     while(head != -1){
26         //把第一个链表存进哈希表exist中
27         exist[head] = 1;
28         head = m[head];
29     }
30     head = b;
31     while(head != -1){
32         if(exist[head] == 1){
33             break;
34         }
35         head = m[head];
36     }
37     if(head == -1) printf("%d\n",head);
38     else printf("%05d\n",head);
39     return 0;
40 }

 

posted @ 2018-10-08 16:39  琥琥笙威  阅读(144)  评论(0编辑  收藏  举报