Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define rep(i, a, b) for(int i = a; i < (b); ++i)
- #define all(x) begin(x), end(x)
- #define sz(x) (int)(x).size()
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- #include <bits/extc++.h> /// include-line, keep-include
- const ll INF = numeric_limits<ll>::max() / 4;
- struct MCMF {
- struct edge {
- int from, to, rev;
- ll cap, cost, flow;
- };
- int N;
- vector<vector<edge>> ed;
- vi seen;
- vector<ll> dist, pi;
- vector<edge*> par;
- MCMF(int N) : N(N), ed(N), seen(N), dist(N), pi(N), par(N) {}
- void addEdge(int from, int to, ll cap, ll cost) {
- if (from == to) return;
- ed[from].push_back(edge{ from,to,sz(ed[to]),cap,cost,0 });
- ed[to].push_back(edge{ to,from,sz(ed[from])-1,0,-cost,0 });
- }
- void path(int s) {
- fill(all(seen), 0);
- fill(all(dist), INF);
- dist[s] = 0; ll di;
- __gnu_pbds::priority_queue<pair<ll, int>> q;
- vector<decltype(q)::point_iterator> its(N);
- q.push({ 0, s });
- while (!q.empty()) {
- s = q.top().second; q.pop();
- seen[s] = 1; di = dist[s] + pi[s];
- for (edge& e : ed[s]) if (!seen[e.to]) {
- ll val = di - pi[e.to] + e.cost;
- if (e.cap - e.flow > 0 && val < dist[e.to]) {
- dist[e.to] = val;
- par[e.to] = &e;
- if (its[e.to] == q.end())
- its[e.to] = q.push({ -dist[e.to], e.to });
- else
- q.modify(its[e.to], { -dist[e.to], e.to });
- }
- }
- }
- rep(i,0,N) pi[i] = min(pi[i] + dist[i], INF);
- }
- pair<ll, ll> maxflow(int s, int t) {
- ll totflow = 0, totcost = 0;
- ll fc = -INF;
- while (path(s), seen[t]) {
- ll fl = INF;
- for (edge* x = par[t]; x; x = par[x->from])
- fl = min(fl, x->cap - x->flow);
- ll c = 0;
- for (edge* x = par[t]; x; x = par[x->from]) {
- c += fl * x->cost;
- }
- if (fc==-INF) fc = c;
- else if (c!=fc) {
- break;
- }
- totflow += fl;
- for (edge* x = par[t]; x; x = par[x->from]) {
- x->flow += fl;
- ed[x->to][x->rev].flow -= fl;
- }
- }
- rep(i,0,N) for(edge& e : ed[i]) totcost += e.cost * e.flow;
- return {totflow, totcost/2};
- }
- // If some costs can be negative, call this before maxflow:
- void setpi(int s) { // (otherwise, leave this out)
- fill(all(pi), INF); pi[s] = 0;
- int it = N, ch = 1; ll v;
- while (ch-- && it--)
- rep(i,0,N) if (pi[i] != INF)
- for (edge& e : ed[i]) if (e.cap)
- if ((v = pi[i] + e.cost) < pi[e.to])
- pi[e.to] = v, ch = 1;
- assert(it >= 0); // negative cost cycle
- }
- };
- void dfs(int u, int t, vi& path, MCMF& flow) {
- if (u == t) return;
- path.push_back(u);
- for (auto& e : flow.ed[u]) if (e.flow>0) {
- e.flow--;
- dfs(e.to, t, path, flow);
- return;
- }
- }
- int main() {
- cin.tie(0)->sync_with_stdio(0);
- int n;
- cin >> n;
- vector<ll> nums(n);
- rep(i, 0, n) cin >> nums[i];
- int nodecnt = 2 * n + 2;
- MCMF flow(nodecnt);
- rep(i, 0, n) {
- flow.addEdge(nodecnt - 2, i * 2, 1, 0);
- flow.addEdge(i*2+1, nodecnt-1, 1, 0);
- flow.addEdge(i*2, i*2+1, 1, -1);
- rep(j, i+1, n) {
- if (nums[i]%nums[j]==0||nums[j]%nums[i]==0) {
- flow.addEdge(i * 2 + 1, j * 2, 1, 0);
- }
- }
- }
- flow.setpi(nodecnt - 2);
- auto [flow_val, cost] = flow.maxflow(nodecnt - 2, nodecnt - 1);
- cout << -cost / flow_val << " " << flow_val << "\n";
- rep(i, 0, flow_val) {
- vi path;
- dfs(nodecnt - 2, nodecnt - 1, path, flow);
- path.erase(path.begin());
- cout << path[0] / 2 + 1 << " ";
- rep(j, 2, sz(path)) {
- if (path[j]/2 != path[j - 1]/2) cout << (path[j] / 2) + 1 << " ";
- }
- cout << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment