kyoo0000

Graph2

Sep 17th, 2021 (edited)
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | None | 0 0
  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5.  
  6. //======================LIST
  7. typedef struct {
  8.   void**  values;
  9.   size_t  len, capacity;
  10.   void*   minimum;
  11.   int     (*compare)(const void*, const void*);
  12. } ArrList;
  13.  
  14. ArrList* init_list(size_t capacity, int (*compare)(const void*, const void*)) {
  15.   ArrList *l = (ArrList*) calloc(1, sizeof(ArrList));
  16.   l->capacity = capacity;
  17.   l->len = 0;
  18.   l->values = (void**) calloc(capacity + 1, sizeof(void*));
  19.   l->values[capacity] = NULL;
  20.   l->compare = compare;
  21.   return l;
  22. }
  23.  
  24. void add_value(ArrList *l, void *value) {
  25.   if(l->len == l->capacity) {
  26.     l->capacity += 100;
  27.     l->values = realloc(l->values, 2*l->capacity);
  28.   }
  29.   if(l->minimum == NULL || l->compare(l->minimum, value) > 0)
  30.     l->minimum = value;
  31.   l->values[l->len++] = value;
  32. }
  33.  
  34. bool is_empty(ArrList *list) { return list->len == 0;}
  35. //======================END LIST
  36.  
  37. //======================GRAPH
  38. typedef enum { WHITE = 1, GREY = 2, BLACK = 3 } COLOR;
  39. typedef struct {
  40.   int   value;
  41.   bool  enabled;
  42.   ArrList *adj_list;
  43.   COLOR color;
  44. } Vertex;
  45.  
  46. int compare_vertices(const void* v, const void* u) {
  47.   Vertex *a = (Vertex*) v;
  48.   Vertex *b = (Vertex*) u;
  49.   return (a->value - b->value);
  50. }
  51.  
  52. Vertex* init_vertex(int value) {
  53.   Vertex *v = (Vertex*) calloc(1, sizeof(Vertex));
  54.   v->value = value;
  55.   v->enabled = false;
  56.   v->adj_list = init_list(100, compare_vertices);
  57.   v->color = WHITE;
  58.   return v;
  59. }
  60.  
  61. typedef struct {
  62.   ArrList *vertices;
  63. } Graph;
  64.  
  65. Graph* init_graph(size_t size) {
  66.   Graph *g = (Graph*) calloc(1, sizeof(Graph));
  67.   g->vertices = init_list(size, compare_vertices);
  68.   return g;
  69. }
  70.  
  71. void add_vertex(Graph* g, int value) {
  72.   g->vertices->values[value] = init_vertex(value);
  73. }
  74.  
  75. void add_edge(Graph* g, int u, int v) {
  76.   Vertex* a = (Vertex*) g->vertices->values[u];
  77.   add_value(a->adj_list, g->vertices->values[v]);
  78. }
  79.  
  80. void custom_bfs(Vertex* root) {
  81.   ArrList *enabled = init_list(100, compare_vertices);
  82.   ArrList *disabled = init_list(100, compare_vertices);
  83.  
  84.   root->color = GREY;
  85.  
  86.   for(int i = 0; i < root->adj_list->len; i++) {
  87.     Vertex *tmp = root->adj_list->values[i];
  88.     if(tmp->color != WHITE) continue;
  89.  
  90.     if(tmp->enabled)
  91.       add_value(enabled, tmp);
  92.     else
  93.      add_value(disabled, tmp);
  94.   }
  95.  
  96.   if(!is_empty(enabled)) {
  97.     printf("%d\n", ((Vertex*) enabled->minimum)->value);
  98.     return;
  99.   } else
  100.     for(int i = 0; i < disabled->len; i++)
  101.       custom_bfs(disabled->values[i]);
  102.  
  103.   root->color = BLACK;
  104.   return;
  105. }
  106.  
  107. int main(void){
  108.   int n;
  109.   scanf("%d", &n);
  110.  
  111.   Graph *g = init_graph(n + 1);
  112.   for(int i = 1; i <=n; i++)
  113.     add_vertex(g, i);
  114.  
  115.   for(int i = 0; i < n - 1; i++) {
  116.     int a, b;
  117.     scanf("%d %d", &a, &b);
  118.     add_edge(g, a, b);
  119.   }
  120.   scanf("%d", &n);
  121.   for(int i = 0; i < n; i++) {
  122.     int v;
  123.     scanf("%d", &v);
  124.     ((Vertex *) g->vertices->values[v])->enabled = true;
  125.   }
  126.   custom_bfs(g->vertices->values[1]);
  127. }
  128.  
  129.  
Add Comment
Please, Sign In to add comment