-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents-in-graph.c
68 lines (58 loc) · 1.45 KB
/
components-in-graph.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
#include <stdio.h>
#include <stdlib.h>
#define MAX 30001
struct node {
int number;
struct node *next;
};
void push_element(struct node **head, int data) {
struct node* new_node = malloc(sizeof(struct node));
new_node->number = data;
new_node->next = *head;
*head = new_node;
}
void clear(struct node *head) {
if (head) {
clear(head->next);
free(head);
}
}
void dfs(struct node **tab, char *visited, int start, int *number) {
visited[start] = 1;
++(*number);
struct node *child = tab[start];
while (child) {
if (!visited[child->number]) {
dfs(tab, visited, child->number, number);
}
child = child->next;
}
}
int main() {
struct node *tab[MAX]={};
char visited[MAX]={};
int n;
int node_1, node_2;
scanf("%d", &n);
for (int a1 = 0; a1 < n; a1++) {
scanf("%d %d", &node_1, &node_2);
push_element(&tab[node_1], node_2);
push_element(&tab[node_2], node_1);
}
int max = 0;
int min = n * 2;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
int number_of_nodes = 0;
dfs(tab, visited, i, &number_of_nodes);
if (number_of_nodes > max) {
max = number_of_nodes;
}
if (number_of_nodes < min && number_of_nodes > 1) {
min = number_of_nodes;
}
}
}
printf("%d %d", min, max);
return 0;
}