Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2021
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef pair<int, int> pi;
  6. typedef pair<ll,ll> pl;
  7. typedef vector<int> vi;
  8. typedef vector<ll> vl;
  9.  
  10. #define FOR(i, a, b) for (int i=a; i<(b); i++)
  11. #define F0R(i, a) for (int i=0; i<(a); i++)
  12. #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
  13. #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
  14. #define trav(a,x) for (auto& a : x)
  15.  
  16. #define sz(x) (int)(x).size()
  17. #define pb push_back
  18. #define f first
  19. #define s second
  20. #define lb lower_bound
  21. #define ub upper_bound
  22. #define all(x) x.begin(), x.end()
  23. #define ins insert
  24.  
  25. void solve() {
  26. int N, M; cin >> N >> M;
  27. vector<int> graph[2*N];
  28. F0R(i, M) {
  29. int X, Y; cin >> X >> Y; X--; Y--;
  30. graph[X].pb(Y+N);
  31. graph[X+N].pb(Y);
  32. graph[Y].pb(X+N);
  33. graph[Y+N].pb(X);
  34. }
  35. int dist[2*N]; F0R(i, 2*N) dist[i] = 1000000;
  36. queue<int> q;
  37. q.push(0);
  38. dist[0] = 0;
  39. while (!q.empty()) {
  40. int x = q.front(); q.pop();
  41. trav(a, graph[x]) {
  42. if (dist[a] > dist[x] + 1) {
  43. dist[a] = dist[x] + 1; q.push(a);
  44. }
  45. }
  46. }
  47. if (dist[N] > 2*N+10) {
  48. cout << N-1 << endl; return;
  49. }
  50. int ans = 0;
  51. map<int, int> cnt[4*N+10];
  52. F0R(i, N) {
  53. int x = min(dist[i], dist[i+N]); int y = max(dist[i], dist[i+N]);
  54. cnt[x+y][x]++;
  55. }
  56.  
  57. F0Rd(m, 4*N+10) {
  58. int lst = -100;
  59. int cur = 0;
  60. trav(a, cnt[m]) {
  61. if (a.s == 0) continue;
  62. //cout << m << " " << a.f << " " << a.s << endl;
  63. if (lst < a.f-1) {
  64. if (lst * 2 + 1 == m) {
  65. ans += (cur+1)/2;
  66. } else ans += cur;
  67. cur = 0;
  68. }
  69. lst = a.f;
  70. if (m >= 2 && cnt[m-2][a.f-1]) {
  71. ans += max(cur, a.s);
  72. cur = min(cur, a.s);
  73. } else {
  74. ans += max(cur, a.s);
  75. cur = a.s;
  76. }
  77. }
  78. if (lst*2+1 == m) {
  79. ans += (cur+1)/2;
  80. } else ans += cur;
  81.  
  82. }
  83. ans--;
  84. /*sort(all(ps));
  85. trav(a, ps) {
  86. cout << a.f << " " << a.s << endl;
  87. }
  88. FOR(i, 1, 2*N+10) {
  89. for (auto it = cnt[i].rbegin(); it != cnt[i].rend(); it++) {
  90. int k = it->s;
  91. int p = it->f;
  92. k -= min(k, cnt[i-1][p]);
  93. int x = min(k, cnt[i-1][p-2]);
  94. cnt[i-1][p-2] -= x;
  95. k -= x;
  96. ans += k; if (p == 1) ans -= k/2;
  97. }
  98. }
  99. int on[2*N+10], off[2*N+10]; F0R(i, 2*N+10) on[i] = 0, off[i] = 0;
  100. int flow[2*N+10]; F0R(i, 2*N+10) flow[i] = 0;
  101. F0R(i, N) {
  102. int x = min(dist[i], dist[i+N]); int y = max(dist[i], dist[i+N]);
  103. on[x]++; if (y < 2*N+10) off[y]++;
  104. if (y == x+1) {
  105. off[y]--; flow[y]++;
  106. cout << i << " flows" << endl;
  107. }
  108. cout << y << " "<< y-x << endl;
  109.  
  110. }
  111.  
  112. bool found = false;
  113. FOR(i, 1, 2*N+10) {
  114. if (!found) {
  115. ans += on[i] + max(off[i] - off[i-1] - flow[i-1], 0) + (flow[i]+1)/2;
  116. found = true;
  117. } else {
  118. int num = off[i-1] + flow[i-1];
  119. int x = min(num, off[i]);
  120. int k = off[i] - x;
  121. num -= x;
  122. int l = flow[i] - min(num, flow[i]);
  123. ans += on[i] + k + (l+1)/2;
  124. }
  125. //cout << i << ": " << on[i] << " " << off[i] << " " << flow[i] << " " << ans << endl;
  126. }*/
  127.  
  128. cout << ans << endl;
  129. }
  130.  
  131. int main() {
  132. ios_base::sync_with_stdio(0); cin.tie(0);
  133.  
  134. int T; cin >> T;
  135. while(T--) {
  136. solve();
  137. }
  138.  
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement