Cod sursa(job #2287314)

Utilizator lupulescu2001Lupulescu Vlad lupulescu2001 Data 21 noiembrie 2018 19:26:38
Problema Xor Max Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.21 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("biperm.in");
ofstream fout("biperm.out");

const int nMax = 10000;
int n, pas, l1[nMax + 5], l2[nMax + 5], use[nMax + 5], k, mutari[nMax + 5], p, use2[nMax + 5];
vector<pair<int, int>> g[nMax + 5];
vector<int> poz[nMax + 5];

int Euclid(int a, int b) {
    if (b == 0)
        return a;
    return Euclid(b, a % b);
}

void Perm(pair<int,int> nr) {
    use[nr.first] = k;
    pas++;
    if (use[g[nr.first][0].first] == 0)
        Perm(g[nr.first][0]);
    else if (use[g[nr.first][1].first] == 0)
        Perm(g[nr.first][1]);
    else
        return;
}

int main() {
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> l1[i];
    for (int i = 1; i <=n; i++) {
        fin >> l2[i];
        g[l1[i]].push_back({l2[i],i});
        g[l2[i]].push_back({l1[i],i});
    }
    for (int i = 1; i <= n; i++) {
        if (use[l1[i]] == 0) {
            k++;
            pas = 0;
            Perm({l1[i],i});
            if (pas > 1)
                p++;
        }
    }
    int sol = 1;
    for (int i = 1; i <= p; i++)
        sol *= 2;
    for (int i = 1; i <= n; i++)
        poz[use[l1[i]]].push_back(i);
    for (int i = 1; i <= n; i++)
        use[i] = 0;
    fout << sol << " ";
    sol = 0;
    for (int i = 1; i <= k; i++) {
        int ultim = poz[i].size() - 1;
        for (auto j : poz[i]) {
            if (use[l1[j]] == 0 && use2[l2[j]] == 0) {
                use[l1[j]] = 1;
                use2[l2[j]] = 1;
            } else if (use[l1[j]] || use2[l2[j]]){
                mutari[i]++;
                use[l2[j]] = 1;
                use2[l2[j]] = 1;
            }
        }
        sol += min(mutari[i], int(poz[i].size()) - mutari[i]);
    }
    fout << sol <<'\n';
    for (int i = 1; i<= n; i++)
        use[i] = 0;
    for (int i = 1; i <= n; i++) {
        if (use[l1[i]] == 0)
            use[l1[i]] = 1;
        else {
            swap(l1[i], l2[i]);
            use[l1[i]] = 1;
        }
    }
    for (int i = 1; i <= n; i++)
        fout << l1[i] << " ";
    fout << '\n';
    for (int i = 1; i <= n; i++)
        fout << l2[i] << " ";
    fout << '\n';
    return 0;
}