/
EN1032.cpp
72 lines (64 loc) · 1.26 KB
/
EN1032.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
//
// main.cpp
// PAT1032
//
// Created by 吕 宗胜 on 7/10/13.
// Copyright (c) 2013 吕 宗胜. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char data;
int next;
int count;
} Node;
void printresult(int a){
if(a<10){
printf("0000%d",a);
}else if(a<100){
printf("000%d",a);
}else if(a<1000){
printf("00%d",a);
}else if(a<10000){
printf("0%d",a);
}else
printf("%d",a);
}
int main(int argc, const char * argv[])
{
int begin1,begin2;
int number;
Node nodes[100000];
scanf("%d%d%d",&begin1,&begin2,&number);
for(int i=0;i<number;i++){
int flag;
scanf("%d",&flag);
scanf("%s",&nodes[flag].data);
scanf("%d",&nodes[flag].next);
nodes[flag].count = 0;
}
int address = 0;
int i=begin1;
while(i!=-1){
nodes[i].count++;
i = nodes[i].next;
}
i=begin2;
while(i!=-1){
nodes[i].count++;
i = nodes[i].next;
}
i=begin2;
while(i!=-1){
if(nodes[i].count>1){
address = i;
break;
}
i = nodes[i].next;
}
if(address!=0)
printresult(address);
else
printf("-1");
return 0;
}