Alex_tz307

Graf C

Sep 12th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. struct Edge {
  2.   int y, w;
  3.   Edge *next;
  4. };
  5.  
  6. struct Graph {
  7.   Edge *edge[1 << 16];
  8.   int degree[1 << 16], nodes, edges;
  9.   bool directed;
  10. };
  11.  
  12. void Initialize (Graph *G, bool directed) {
  13.   G -> nodes = G -> edges = 0;
  14.   G -> directed = directed;
  15.   for (int i = 1; i < (1 << 16); i ++) {
  16.     G -> degree[i] = 0;
  17.     G -> edge[i] = nullptr;
  18.   }
  19. }
  20.  
  21. void Insert (Graph *G, int x, int y, bool directed) {
  22.   Edge *p;
  23.   p = new Edge;
  24.   p -> w = NULL;
  25.   p -> y = y;
  26.   p -> next = G -> edge[x];
  27.   G -> edge[x] = p;
  28.   G -> degree[x] ++;
  29.   if (!directed)
  30.     Insert (G, y, x, true);
  31.   else
  32.     G -> edges ++;
  33. }
  34.  
  35. void Read (Graph *G, bool directed) {
  36.   Initialize (G, directed);
  37.   int M;
  38.   fin >> G -> nodes >> M;
  39.   while (M --) {
  40.     int x, y;
  41.     fin >> x >> y;
  42.     Insert (G, x, y, directed);
  43.   }
  44. }
  45.  
  46. void Print (Graph *G) {
  47.   Edge *p;
  48.   for (int i = 1; i <= G -> nodes; i ++) {
  49.     fout << i << " : ";
  50.     p = G -> edge[i];
  51.     while (p != nullptr) {
  52.       fout << p -> y << ' ';
  53.       p = p -> next;
  54.     }
  55.     fout << '\n';
  56.   }
  57. }
  58.  
Add Comment
Please, Sign In to add comment