matistjati

Untitled

Jun 24th, 2026
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define rep(i, a, b) for(int i = a; i < (b); ++i)
  5. #define all(x) begin(x), end(x)
  6. #define sz(x) (int)(x).size()
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. typedef vector<int> vi;
  10.  
  11. #include <bits/extc++.h> /// include-line, keep-include
  12.  
  13. const ll INF = numeric_limits<ll>::max() / 4;
  14.  
  15. struct MCMF {
  16. struct edge {
  17. int from, to, rev;
  18. ll cap, cost, flow;
  19. };
  20. int N;
  21. vector<vector<edge>> ed;
  22. vi seen;
  23. vector<ll> dist, pi;
  24. vector<edge*> par;
  25.  
  26. MCMF(int N) : N(N), ed(N), seen(N), dist(N), pi(N), par(N) {}
  27.  
  28. void addEdge(int from, int to, ll cap, ll cost) {
  29. if (from == to) return;
  30. ed[from].push_back(edge{ from,to,sz(ed[to]),cap,cost,0 });
  31. ed[to].push_back(edge{ to,from,sz(ed[from])-1,0,-cost,0 });
  32. }
  33.  
  34. void path(int s) {
  35. fill(all(seen), 0);
  36. fill(all(dist), INF);
  37. dist[s] = 0; ll di;
  38.  
  39. __gnu_pbds::priority_queue<pair<ll, int>> q;
  40. vector<decltype(q)::point_iterator> its(N);
  41. q.push({ 0, s });
  42.  
  43. while (!q.empty()) {
  44. s = q.top().second; q.pop();
  45. seen[s] = 1; di = dist[s] + pi[s];
  46. for (edge& e : ed[s]) if (!seen[e.to]) {
  47. ll val = di - pi[e.to] + e.cost;
  48. if (e.cap - e.flow > 0 && val < dist[e.to]) {
  49. dist[e.to] = val;
  50. par[e.to] = &e;
  51. if (its[e.to] == q.end())
  52. its[e.to] = q.push({ -dist[e.to], e.to });
  53. else
  54. q.modify(its[e.to], { -dist[e.to], e.to });
  55. }
  56. }
  57. }
  58. rep(i,0,N) pi[i] = min(pi[i] + dist[i], INF);
  59. }
  60.  
  61. pair<ll, ll> maxflow(int s, int t) {
  62. ll totflow = 0, totcost = 0;
  63. ll fc = -INF;
  64. while (path(s), seen[t]) {
  65. ll fl = INF;
  66. for (edge* x = par[t]; x; x = par[x->from])
  67. fl = min(fl, x->cap - x->flow);
  68.  
  69. ll c = 0;
  70. for (edge* x = par[t]; x; x = par[x->from]) {
  71. c += fl * x->cost;
  72. }
  73. if (fc==-INF) fc = c;
  74. else if (c!=fc) {
  75. break;
  76. }
  77.  
  78. totflow += fl;
  79. for (edge* x = par[t]; x; x = par[x->from]) {
  80. x->flow += fl;
  81. ed[x->to][x->rev].flow -= fl;
  82. }
  83. }
  84. rep(i,0,N) for(edge& e : ed[i]) totcost += e.cost * e.flow;
  85. return {totflow, totcost/2};
  86. }
  87.  
  88. // If some costs can be negative, call this before maxflow:
  89. void setpi(int s) { // (otherwise, leave this out)
  90. fill(all(pi), INF); pi[s] = 0;
  91. int it = N, ch = 1; ll v;
  92. while (ch-- && it--)
  93. rep(i,0,N) if (pi[i] != INF)
  94. for (edge& e : ed[i]) if (e.cap)
  95. if ((v = pi[i] + e.cost) < pi[e.to])
  96. pi[e.to] = v, ch = 1;
  97. assert(it >= 0); // negative cost cycle
  98. }
  99. };
  100.  
  101.  
  102. void dfs(int u, int t, vi& path, MCMF& flow) {
  103. if (u == t) return;
  104. path.push_back(u);
  105. for (auto& e : flow.ed[u]) if (e.flow>0) {
  106. e.flow--;
  107. dfs(e.to, t, path, flow);
  108. return;
  109. }
  110. }
  111.  
  112.  
  113. int main() {
  114. cin.tie(0)->sync_with_stdio(0);
  115.  
  116. int n;
  117. cin >> n;
  118. vector<ll> nums(n);
  119. rep(i, 0, n) cin >> nums[i];
  120.  
  121. int nodecnt = 2 * n + 2;
  122. MCMF flow(nodecnt);
  123.  
  124. rep(i, 0, n) {
  125. flow.addEdge(nodecnt - 2, i * 2, 1, 0);
  126. flow.addEdge(i*2+1, nodecnt-1, 1, 0);
  127. flow.addEdge(i*2, i*2+1, 1, -1);
  128. rep(j, i+1, n) {
  129. if (nums[i]%nums[j]==0||nums[j]%nums[i]==0) {
  130. flow.addEdge(i * 2 + 1, j * 2, 1, 0);
  131. }
  132. }
  133. }
  134. flow.setpi(nodecnt - 2);
  135. auto [flow_val, cost] = flow.maxflow(nodecnt - 2, nodecnt - 1);
  136. cout << -cost / flow_val << " " << flow_val << "\n";
  137.  
  138. rep(i, 0, flow_val) {
  139. vi path;
  140. dfs(nodecnt - 2, nodecnt - 1, path, flow);
  141. path.erase(path.begin());
  142. cout << path[0] / 2 + 1 << " ";
  143. rep(j, 2, sz(path)) {
  144. if (path[j]/2 != path[j - 1]/2) cout << (path[j] / 2) + 1 << " ";
  145. }
  146. cout << "\n";
  147. }
  148.  
  149.  
  150. return 0;
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment