-
Notifications
You must be signed in to change notification settings - Fork 0
/
274.c
100 lines (86 loc) · 2.23 KB
/
274.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
// http://www.bjfuacm.com/problem/274/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 17234
int bfs(int i, int N, int **g) {
/**
* brief: breadth first search graph
* params: ith node to count, node amount and graph
* retval: node amount in graph which reaches 6 level
*/
int queue[MAXN];
int vis[MAXN];
int front;
int rear;
int count;
int level;
int last; // last level's last node
int tail; // cur level's last node
int cur; // current node
memset(vis, 0, sizeof(vis));
vis[i] = 1;
front = rear = -1;
count = 1;
level = 0;
last = i;
queue[++rear] = i;
while (front < rear) { // similar to BST level order traversal
cur = queue[++front];
for (int j = 1; j <= N; j++) {
if (!vis[j] && g[cur][j] == 1) {
/**
* condition:
* 1. this node is not visited
* 2. path from cur to this node exists
*/
queue[++rear] = j;
vis[j] = 1; // mark this is visited
count++;
tail = j; // tail can be cur level's last node
}
}
if (cur == last) {
level++;
last = tail;
}
if (level == 6) {
break;
}
}
return count;
}
void destroy(int **g, int N) {
for (int i = 0; i <= N; i++) {
free(g[i]);
g[i] = NULL;
}
free(g);
}
int main() {
int N, M;
int count;
int **g; // adjacent graph
int x; // coordinate x axis
int y; // coordinate y axis
while (1) {
scanf("%d %d", &N, &M);
if (N == 0 && M == 0) break;
g = (int **)calloc(N + 1, sizeof(int *));
if (g == NULL) exit(14);
for (int i = 0; i <= N; i++) {
g[i] = (int *)calloc(N + 1, sizeof(int));
if (g[i] == NULL) exit(14);
}
for (int i = 0; i < M; i++) {
scanf("%d %d", &x, &y);
g[x][y] = 1;
g[y][x] = 1;
}
for (int i = 1; i <= N; i++) {
count = bfs(i, N, g);
printf("%d: %.2f%%\n", i, (float)count / N * 100);
}
destroy(g, N);
}
}