Advertisement
Guest User

Untitled

a guest
May 5th, 2019
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #ifdef DEBUG
  2. #define _GLIBCXX_DEBUG
  3. #endif
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. typedef long double ld;
  10.  
  11. #ifdef DEBUG
  12. #define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
  13. #else
  14. #define eprintf(...) ;
  15. #endif
  16.  
  17. #define sz(x) ((int) (x).size())
  18. #define TASK "text"
  19.  
  20. const int inf = (int) 1.01e9;
  21. const long long infll = (long long) 1.01e18;
  22. const ld eps = 1e-9;
  23. const ld pi = acos((ld) -1);
  24.  
  25. #ifdef DEBUG
  26. mt19937 mrand(300);
  27. #else
  28. mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
  29. #endif
  30.  
  31. int rnd(int x) {
  32.   return mrand() % x;
  33. }
  34.  
  35. void precalc() {
  36. }
  37.  
  38. const int maxn = (int) 1e5 + 5;
  39. int n, m;
  40. vector<pair<int, int>> g[maxn];
  41.  
  42. bool read() {
  43.   if (scanf("%d%d", &n, &m) < 2) {
  44.     return false;
  45.   }
  46.   for (int i = 0; i < n; i++) {
  47.     g[i].clear();
  48.   }
  49.   for (int i = 0; i < m; i++) {
  50.     int v, u;
  51.     scanf("%d%d", &v, &u);
  52.     v--;
  53.     u--;
  54.     g[u].push_back(make_pair(v, i));
  55.   }
  56.   return true;
  57. }
  58.  
  59. int used[maxn];
  60. int p[maxn], q[maxn];
  61. int pid[maxn];
  62. int perm[maxn];
  63.  
  64. bool dfs(int v) {
  65.   used[v] = true;
  66.   for (int i = 0; i < sz(g[v]); i++) {
  67.     int u = g[v][i].first;
  68.     if (p[u] == -1) {
  69.       p[u] = v;
  70.       q[v] = u;
  71.       return true;
  72.     }
  73.   }
  74.   for (int i = 0; i < sz(g[v]); i++) {
  75.     int u = g[v][i].first;
  76.     if (!used[p[u]] && dfs(p[u])) {
  77.       p[u] = v;
  78.       q[v] = u;
  79.       return true;
  80.     }
  81.   }
  82.   return false;
  83. }
  84.  
  85. int ans[maxn];
  86.  
  87. int dfs1(int v) {
  88.   used[v] = true;
  89.   int res = 1;
  90.   for (int i = 0; i < sz(g[v]); i++) {
  91.     int u = g[v][i].first, id = g[v][i].second;
  92.     if (used[p[u]]) {
  93.       continue;
  94.     }
  95.     int cur = dfs1(p[u]);
  96.     ans[pid[u]] -= cur;
  97.     ans[id] += cur;
  98.     res += cur;
  99.   }
  100.   return res;
  101. }
  102.  
  103. void solve() {
  104.   for (int i = 0; i < n; i++) {
  105.     p[i] = -1;
  106.     q[i] = -1;
  107.     perm[i] = i;
  108.     shuffle(g[i].begin(), g[i].end(), mrand);
  109.   }
  110.   shuffle(perm, perm + n, mrand);
  111.   while (true) {
  112.     bool found = false;
  113.     for (int i = 0; i < n; i++) {
  114.       used[i] = false;
  115.     }
  116.     for (int i = 0; i < n; i++) {
  117.       int v = perm[i];
  118.       if (v == 0) {
  119.         continue;
  120.       }
  121.       if (q[v] == -1 && !used[v] && dfs(v)) {
  122.         found = true;
  123.       }
  124.     }
  125.     if (!found) {
  126.       break;
  127.     }
  128.   }
  129.   for (int i = 0; i < m; i++) {
  130.     ans[i] = 0;
  131.   }
  132.   for (int v = 1; v < n; v++) {
  133.     if (q[v] == -1) {
  134.       printf("-1\n");
  135.       return;
  136.     }
  137.     for (int i = 0; i < sz(g[v]); i++) {
  138.       int u = g[v][i].first, id = g[v][i].second;
  139.       if (u == q[v]) {
  140.         ans[id] += n;
  141.         pid[u] = id;
  142.       }
  143.     }
  144.   }
  145.   for (int i = 0; i < n; i++) {
  146.     used[i] = false;
  147.   }
  148.   dfs1(0);
  149.   for (int i = 0; i < n; i++) {
  150.     if (!used[i]) {
  151.       printf("-1\n");
  152.     }
  153.   }
  154.   for (int i = 0; i < m; i++) {
  155.     printf("%d\n", ans[i]);
  156.   }
  157. }
  158.  
  159. int main() {
  160.   precalc();
  161. #ifdef DEBUG
  162.   assert(freopen(TASK ".in", "r", stdin));
  163.   assert(freopen(TASK ".out", "w", stdout));
  164. #endif
  165.   while (read()) {
  166.     solve();
  167. #ifdef DEBUG
  168.     eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
  169. #endif
  170.   }
  171.   return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement