matistjati

Untitled

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