-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path补充题目:1076 Forwards on Weibo.c
194 lines (168 loc) · 3.75 KB
/
补充题目:1076 Forwards on Weibo.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 1001
/* 无向图的邻接表定义——开始 */
typedef int Vertex;
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode {
Vertex AdjV;
PtrToAdjVNode Next;
};
typedef struct Vnode {
PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];
typedef struct GNode *PtrToGNode;
struct GNode {
int Nv;
AdjList G;
};
typedef PtrToGNode LGraph;
LGraph CreateGraph( int VertexNum )
{
Vertex V;
LGraph Graph;
Graph = (LGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
for (V = 1; V <= Graph->Nv; ++V)
Graph->G[V].FirstEdge = NULL;
return Graph;
}
void DestoryGraph( LGraph Graph )
{
Vertex V;
PtrToAdjVNode Node;
for (V = 1; V <= Graph->Nv; ++V) {
while (Graph->G[V].FirstEdge) {
Node = Graph->G[V].FirstEdge;
Graph->G[V].FirstEdge = Node->Next;
free(Node);
}
}
free(Graph);
}
void InsertEdge(LGraph Graph, Vertex V, Vertex W)
{
PtrToAdjVNode NewNode;
NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
NewNode->AdjV = W;
NewNode->Next = Graph->G[V].FirstEdge;
Graph->G[V].FirstEdge = NewNode;
}
/* 无向图的邻接表定义——结束 */
/* 队列定义开始 */
#define MaxSize 1001
#define ERROR -1
typedef int Position;
struct QNode {
int *Data; /* 存储元素的数组 */
Position Front, Rear; /* 队列的头、尾指针 */
};
typedef struct QNode *Queue;
Queue CreateQueue()
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (int *)malloc(MaxSize * sizeof(int));
Q->Front = Q->Rear = 0;
return Q;
}
void DestoryQueue( Queue Q )
{
if (Q->Data) free(Q->Data);
free(Q);
}
int IsFull( Queue Q )
{
return ((Q->Rear+1)%MaxSize == Q->Front);
}
void Enqueue( Queue Q, int X )
{
if ( IsFull(Q) ) return;
else {
Q->Rear = (Q->Rear+1)%MaxSize;
Q->Data[Q->Rear] = X;
}
}
int IsEmpty( Queue Q )
{
return (Q->Front == Q->Rear);
}
int Dequeue( Queue Q )
{
if ( IsEmpty(Q) ) return ERROR;
else {
Q->Front =(Q->Front+1)%MaxSize;
return Q->Data[Q->Front];
}
}
/* 队列定义结束 */
int L;
Vertex visited[MaxVertexNum] = {0};
void setZero(int VertexNum)
{
Vertex V;
for (V = 1; V <= VertexNum; ++V)
visited[V] = 0;
}
LGraph BuildGraph( int VertexNum )
{
LGraph Graph;
Vertex V, W;
int m, i;
Graph = CreateGraph(VertexNum);
for (V = 1; V <= Graph->Nv; ++V) {
scanf("%d", &m); // user[V] follows m people
for (i = 0; i < m; ++i) {
scanf("%d", &W);
InsertEdge(Graph, W, V);
}
}
return Graph;
}
int BFS(LGraph Graph, Vertex V, int levelNum)
{
int count, level;
Vertex tail, last; Queue Q;
PtrToAdjVNode Node;
Q = CreateQueue();
setZero(Graph->Nv);
visited[V] = 1; count = 0;
level = 0; last = V; tail = V;
Enqueue(Q, V);
while(!IsEmpty(Q)) {
V = Dequeue(Q);
for (Node = Graph->G[V].FirstEdge; Node; Node = Node->Next) {
if (!visited[Node->AdjV]) {
visited[Node->AdjV] = 1;
Enqueue(Q, Node->AdjV); ++count;
tail = Node->AdjV;
}
}
if (V == last) {
++level; last = tail;
}
if (level == levelNum) break;
}
DestoryQueue(Q);
return count;
}
void CalForwards(LGraph Graph, int levelNum)
{
int i, k, count;
Vertex V;
scanf("%d", &k);
for (i = 0; i < k; ++i) {
scanf("%d", &V);
count = BFS(Graph, V, levelNum);
printf("%d\n", count);
}
}
int main()
{
int userNum, levelNum;
LGraph Graph;
scanf("%d %d", &userNum, &levelNum);
Graph = BuildGraph(userNum);
CalForwards(Graph, levelNum);
DestoryGraph(Graph);
return 0;
}