-
Notifications
You must be signed in to change notification settings - Fork 0
/
邻接表形式存储的图.cpp
56 lines (53 loc) · 974 Bytes
/
邻接表形式存储的图.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
int data;
struct node *next;
};
int main() {
int n, u, j, i, v;
struct node *p, *a[5050];
while(~scanf("%d", &n)) {
for(i = 0; i < n; i++)
a[i] = NULL;
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &u);
if(u == 1) {
if(a[i] == NULL) {
p = (struct node *)malloc(sizeof(struct node));
p -> data = j;
p -> next = NULL;
a[i] = p;
} else {
p = (struct node *)malloc(sizeof(struct node));
p -> next = a[i] -> next;
p -> data = j;
a[i] -> next = p;
}
}
}
}
//
int q;
scanf("%d", &q);
while(q--) {
scanf("%d%d", &u, &v);
p = a[u];
int flag = 0;
while(p) {
if(p -> data == v) {
flag = 1;
break;
}
p = p -> next;
}
if(flag)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}