Advertisement
cmiN

graf

Mar 18th, 2011
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3.  
  4. const int nMax = 7501, mMax = 14000, inf = 0x3fFF;
  5. FILE* fin, * fout;
  6. int nodes, edges, length[nMax], start, end, cmax = -1, count = -1, usedNode[nMax];
  7. bool vizNode[nMax];
  8. int* list[nMax];
  9.  
  10. void read()
  11. {
  12.     int i, x, y;
  13.     fin = fopen("graf.in", "rt");
  14.     fout = fopen("graf.out", "wt");
  15.     fscanf(fin, "%d %d %d %d", &nodes, &edges, &start, &end);
  16.     for (i = 1; i <= nodes; i++) {
  17.         list[i] = (int*) malloc(sizeof(int));
  18.         list[i][0] = 0;
  19.     }
  20.     for (i = 0; i < edges; i++) {
  21.         fscanf(fin, "%d %d", &x, &y);
  22.         list[x][0]++;
  23.         list[y][0]++;
  24.         list[x] = (int*) realloc(list[x], (list[x][0] + 1) * sizeof(int));
  25.         list[y] = (int*) realloc(list[y], (list[y][0] + 1) * sizeof(int));
  26.         list[x][list[x][0]] = y;
  27.         list[y][list[y][0]] = x;
  28.     }
  29. }
  30.  
  31. void bfs()
  32. {
  33.     int queue[nMax], lo, hi, node;
  34.     lo = hi = 0;
  35.     queue[0] = start;
  36.     vizNode[start] = 1;
  37.     while (lo <= hi && !vizNode[end]) {
  38.         node = queue[lo++];
  39.         for (int i = 1; i <= list[node][0] && !vizNode[end]; i++) {
  40.             if (!vizNode[list[node][i]]) {
  41.                 vizNode[list[node][i]] = 1;
  42.                 length[list[node][i]] = length[node] + 1;
  43.                 queue[++hi] = list[node][i];
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49. void search_back(int node)
  50. {
  51.     int ways = 0;
  52.     for (int i = 1; i <= list[node][0]; i++) {
  53.         if (length[list[node][i]] == length[node] - 1) {
  54.             ways++;
  55.             usedNode[list[node][i]]++;
  56.             search_back(list[node][i]);
  57.         }
  58.     }
  59.     if (ways == 0) {
  60.         ways = 1;
  61.     }
  62.     usedNode[node] = usedNode[node] * ways;
  63.     if (usedNode[node] > cmax) {
  64.         cmax = usedNode[node];
  65.         count = 1;
  66.     } else if (usedNode[node] == cmax) {
  67.         count++;
  68.     }
  69. }
  70.  
  71. void solve()
  72. {
  73.     for (int i = 1; i <= nodes; i++) {
  74.         length[i] = inf;
  75.     }
  76.     length[start] = 0;
  77.     bfs();
  78.     search_back(end);
  79.     usedNode[end] = cmax;
  80.     count++;
  81. }
  82.  
  83. void write()
  84. {
  85.     fprintf(fout, "%d\n", count);
  86.     for (int i = 1; i <= nodes; i++) {
  87.         if (usedNode[i] == cmax) {
  88.             fprintf(fout, "%d ", i);
  89.         }
  90.     }
  91.     fclose(fin);
  92.     fclose(fout);
  93. }
  94.  
  95. int main()
  96. {
  97.     read();
  98.     solve();
  99.     write();
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement