Submission #1080188

#TimeUsernameProblemLanguageResultExecution timeMemory
1080188TB_Ancient Books (IOI17_books)C++17
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define fo(i, n) for(ll i = 0; i<(n); i++)
#define F first
#define S second
#define pb push_back
#define deb(x) cout << #x << " = " << (x) << endl
#define deb2(x, y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl

typedef vector<ll> vl;
typedef vector<vl> vvl;

struct UnionFind{
    vl p, siz;
    ll n;
    UnionFind(int n) : n(n){
        p.assign(n, 1);
        siz.assign(n, 1);
        fo(i, n) p[i] = i;
    }

    int find(int x){
        if(x == p[x]) return x;
        return p[x] = find(p[x]);
    }

    bool same(int a, int b){
        return find(a) == find(b);
    }

    void unite(int a, int b){
        a = find(a);
        b = find(b);
        if(a == b) return;
        if(siz[b] > siz[a]) swap(a, b);
        siz[a]+=siz[b];
        p[b] = a;
    }
};

vl color, nextB;
ll ans = 0, c = 0;

void dfs(int u){
    if(color[u]!=-1) return;
    color[u] = c;
    dfs(nextB[u]);
    ans+=abs(nextB[u]-u);
}

long long minimum_walk(std::vector<int> p, int s){
    ll n = p.size();
    fo(i, n)nextB.pb(p[i]);
    color.assign(n, -1);
    fo(i, n){
        if((nextB[i] == i && i)|| color[i]!=-1) continue;
        dfs(i);
        c++;
    }
    priority_queue<tuple<ll, ll, ll>> pq;
    fo(i, n){
        if(color[i]==-1) continue;
        fo(j, n){
            if(color[j]==-1) continue;
            pq.push({-abs(j-i), color[i], color[j]});
        }
    }
    UnionFind uf(c);
    ll cost, from, to;
    while(!pq.empty()){
        tie(cost, from, to) = pq.top();
        pq.pop();
        if(uf.same(from, to)) continue;
        uf.unite(from, to);
        ans-=cost*2;
    }

	return ans;
}



int main(){

    vector<int> a = {2, 1, 0, 3};
    ll res = minimum_walk(a, 0);
    deb(res);

    return 0;
}

Compilation message (stderr)

/usr/bin/ld: /tmp/ccRwL5u2.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccZWoj64.o:books.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status