Pagini recente » Cod sursa (job #3217508) | Cod sursa (job #2509974) | Cod sursa (job #1317380) | Cod sursa (job #2756047) | Cod sursa (job #2502261)
#include <iostream>
#include <fstream>
#include <vector>
#define NMAX 10000
using namespace std;
ifstream fin("biperm.in");
ofstream fout("biperm.out");
struct biperm {
int nod, tip;
};
struct index {
int i1,i2;
}vf[NMAX+1];
vector <biperm> g[NMAX + 1];
vector <int> indici;
int n, v1[NMAX + 1], v2[NMAX + 1], cc = 0, s = 0, viz[NMAX + 1], n1;
void read() {
int i, nr;
biperm aux;
fin >> n;
for(int i = 1; i <= n; i++)
fin >> v1[i];
for(int i = 1; i <= n; i++) {
fin >> v2[i];
if(v2[i] != v1[i]) {
aux.nod = v2[i];
aux.tip = 1;
g[v1[i]].push_back(aux);
aux.nod = v1[i];
aux.tip = 2;
g[v2[i]].push_back(aux);
}
}
}
void dfs(int nod, int &nr1, int &nr2) {
viz[nod] = 1;
n1 = nod;
for(int i = 0; i < g[nod].size(); i++) {
biperm aux = g[nod][i];
if(viz[aux.nod] == 0) {
if(aux.tip == 1) {
nr1++;
vf[nod].i1++;
vf[aux.nod].i2++;
}
else if(aux.tip == 2)
nr2++;
g[nod][i].tip = 0;
///eliminam muchia cu probleme
for(int j = 0; j < g[aux.nod].size(); j++) {
biperm aux2 = g[aux.nod][j];
if(aux2.nod == nod)
if(aux2.tip != aux.tip && aux.tip) {
g[aux.nod][j].tip = 0;
aux.tip = 0;
}
}
///-------------------------
dfs(aux.nod, nr1, nr2);
}
}
}
int main() {
read();
for(int i = 1; i <= n; i++)
if(viz[i] == 0) {
int nr1 = 0;
int nr2 = 0;
dfs(i, nr1, nr2);
///completam ciclul
for(int j = 0; j < g[n1].size(); j++) {
biperm aux = g[n1][j];
if(aux.nod == i)
if(aux.tip == 1) {
nr1++;
vf[n1].i1++;
vf[aux.nod].i2++;
}
else if(aux.tip == 2)
nr2++;
}
///-----------------
s += min(nr1, nr2);
if(n1 != i)
cc++;
}
fout << (1 << cc) << " " << s<<endl;
for(int i=1;i<=n;i++) {
if(vf[v1[i]].i1>0 && vf[v2[i]].i2>0) {
vf[v1[i]].i1--;
vf[v2[i]].i2--;
swap(v1[i],v2[i]);
}
}
for(int i=1;i<=n;i++)
fout<<v1[i]<<" ";
fout<<endl;
for(int i=1;i<=n;i++)
fout<<v2[i]<<" ";
return 0;
}