chaosagent

USACO 2015 Dec Platinum 1

Dec 16th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. #include <assert.h>
  5.  
  6. using namespace std;
  7.  
  8. struct Stall {
  9.     Stall *parent = NULL;
  10.     vector<Stall *> nexts;
  11.     int dist = -1;
  12.     int flow = 0;
  13.     int startflow = 0;
  14.     int stopflow = 0;
  15.     int children = 0;
  16.     vector<Stall *> aboves;
  17. };
  18.  
  19. int log2(int n) {
  20.     int result = -1;
  21.     while (n) {
  22.         result++;
  23.         n >>= 1;
  24.     }
  25.     return result;
  26. }
  27.  
  28. Stall *get2nabove(Stall *stall, int n) {
  29.     Stall *result;
  30.     if (n < stall->aboves.size()) {
  31.         result = stall->aboves[n];
  32.     } else {
  33.         if (n == 0) {
  34.             result = stall->parent;
  35.         } else {
  36.             result = get2nabove(get2nabove(stall, n - 1), n - 1);
  37.         }
  38.         stall->aboves.push_back(result);
  39.     }
  40.     return result;
  41. }
  42.  
  43. Stall *LCA(Stall *a, Stall *b) {
  44.     if (b->dist < a->dist)
  45.         return LCA(b, a);
  46.     int afromb = b->dist - a->dist;
  47.     for (int i = 0; afromb; afromb >>= 1, i++) {
  48.         if (afromb % 2 == 0) continue;
  49.         b = get2nabove(b, i);
  50.     }
  51.     assert(a->dist == b->dist);
  52.     int rbegin = 0;
  53.     while (a->dist - rbegin > 1) {
  54.         int toshift = log2(a->dist - rbegin) - 1;
  55.         Stall *na = get2nabove(a, toshift);
  56.         Stall *nb = get2nabove(b, toshift);
  57.         assert(na->dist == nb->dist);
  58.         if (na == nb)
  59.             rbegin = na->dist + 1;
  60.         else
  61.             a = na, b = nb;
  62.     }
  63.     while (a != b) {
  64.         a = a->parent;
  65.         b = b->parent;
  66.     }
  67.     assert(a == b);
  68.     return a;
  69. }
  70.  
  71. int main() {
  72.     FILE *fin = fopen("maxflow.in", "r");
  73.     FILE *fout = fopen("maxflow.out", "w");
  74.  
  75.     int n, k;
  76.     fscanf(fin, "%d %d", &n, &k);
  77.     vector<Stall> stalls(n);
  78.  
  79.     for (int i = 0; i < n - 1; i++) {
  80.         int x, y;
  81.         fscanf(fin, "%d %d", &x, &y);
  82.         x -= 1;
  83.         y -= 1;
  84.         stalls[x].nexts.push_back(&stalls[y]);
  85.         stalls[y].nexts.push_back(&stalls[x]);
  86.     }
  87.  
  88.     stalls[0].dist = 0;
  89.     queue<Stall *> bfs;
  90.     bfs.push(&stalls[0]);
  91.     Stall *laststall = &stalls[0]; // Dummy value; should never be used.
  92.     while (bfs.size()) {
  93.         Stall *currstall = bfs.front();
  94.         bfs.pop();
  95.  
  96.         for (Stall *nextstall : currstall->nexts) {
  97.             if (nextstall->dist == -1) {
  98.                 nextstall->dist = currstall->dist + 1;
  99.                 bfs.push(nextstall);
  100.             }
  101.         }
  102.  
  103.         if (!bfs.size()) {
  104.             laststall = currstall;
  105.         }
  106.     }
  107.  
  108.     laststall->dist = 0;
  109.     bfs.push(laststall);
  110.     Stall *laststall2 = laststall; // Dummy value; should never be used.
  111.     while (bfs.size()) {
  112.         Stall *currstall = bfs.front();
  113.         bfs.pop();
  114.  
  115.         for (Stall *nextstall : currstall->nexts) {
  116.             if (nextstall->parent == NULL) {
  117.                 nextstall->parent = currstall;
  118.                 nextstall->dist = currstall->dist + 1;
  119.                 bfs.push(nextstall);
  120.             }
  121.         }
  122.  
  123.         if (!bfs.size()) {
  124.             laststall2 = currstall;
  125.         }
  126.     }
  127.     assert(n == 1 || (laststall != laststall2));
  128.  
  129.     Stall *root = laststall2;
  130.     for (int i = 0; i < laststall2->dist / 2; i++) {
  131.         root = root->parent;
  132.     }
  133.  
  134.     for (int i = 0; i < n; i++) {
  135.         stalls[i].dist = -1;
  136.     }
  137.  
  138.     root->parent = NULL;
  139.     root->dist = 0;
  140.     bfs.push(root);
  141.     while (bfs.size()) {
  142.         Stall *currstall = bfs.front();
  143.         bfs.pop();
  144.  
  145.         for (Stall *nextstall : currstall->nexts) {
  146.             if (nextstall->dist == -1) {
  147.                 nextstall->parent = currstall;
  148.                 nextstall->dist = currstall->dist + 1;
  149.                 bfs.push(nextstall);
  150.             }
  151.         }
  152.     }
  153.  
  154.     for (int ki = 0; ki < k; ki++) {
  155.         int s, t;
  156.         fscanf(fin, "%d %d", &s, &t);
  157.         stalls[s - 1].startflow++;
  158.         stalls[t - 1].startflow++;
  159.         Stall *lca = LCA(&stalls[s - 1], &stalls[t - 1]);
  160.         lca->stopflow++;
  161.     }
  162.  
  163.     queue<Stall *> q;
  164.     for (int i = 0; i < n; i++) {
  165.         if (stalls[i].nexts.size() == 1) {
  166.             q.push(&stalls[i]);
  167.         }
  168.     }
  169.  
  170.     while (q.size()) {
  171.         Stall *currstall = q.front();
  172.         q.pop();
  173.         currstall->flow += currstall->startflow - currstall->stopflow;
  174.         if (currstall != root) {
  175.             currstall->parent->flow += currstall->flow - currstall->stopflow;
  176.             currstall->parent->children++;
  177.             if (currstall->parent->children == currstall->parent->nexts.size() - 1) {
  178.                 q.push(currstall->parent);
  179.             }
  180.         }
  181.     }
  182.  
  183.     /*for (int ki = 0; ki < k; ki++) {
  184.         int s, t;
  185.         fscanf(fin, "%d %d", &s, &t);
  186.  
  187.         Stall *currstall1 = &stalls[s - 1], *currstall2 = &stalls[t - 1];
  188.         currstall1->flow--;
  189.         currstall2->flow--;
  190.  
  191.         while (currstall1->dist > currstall2->dist) {
  192.             currstall1 = currstall1->parent;
  193.             currstall1->flow++;
  194.         }
  195.  
  196.         while (currstall2->dist > currstall1->dist) {
  197.             currstall2 = currstall2->parent;
  198.             currstall2->flow++;
  199.         }
  200.  
  201.         while (currstall1 != currstall2) {
  202.             if (currstall1 != root) {
  203.                 currstall1 = currstall1->parent;
  204.                 currstall1->flow++;
  205.             }
  206.             if (currstall2 != root) {
  207.                 currstall2 = currstall2->parent;
  208.                 currstall2->flow++;
  209.             }
  210.         }
  211.         currstall1->flow--;
  212.     }*/
  213.  
  214.     int maxflow = 0;
  215.     for (int i = 0; i < n; i++) {
  216.         maxflow = max(maxflow, stalls[i].flow);
  217.     }
  218.  
  219.     fprintf(fout, "%d\n", maxflow);
  220.     return 0;
  221. }
Add Comment
Please, Sign In to add comment