Cod sursa(job #2884297)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 2 aprilie 2022 20:29:33
Problema Dusman Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <iostream>
#include <fstream>

using namespace std;

const string filename = "dusman";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");

/// I <3 probleme cu backtracking

int n, m, nr_sol, p, sol[1005];
bool rau[1005][1005], used[1005];

void afis()
{
    nr_sol++;
    if(nr_sol == p)
    {
        for(int i = 1; i <= n; i++)
            fout << sol[i] << ' ';
        exit(0);
    }
}

void bkt(int k)
{
    for(int i = 1; i <= n; i++)
    {
        if(used[i] || rau[sol[k - 1]][i])
            continue;
        sol[k] = i;
        used[i] = 1;
        if(k == n)
            afis();
        else
            bkt(k + 1);
        used[i] = 0;
    }
}

int main()
{
    fin >> n >> p >> m;
    for(int x, y, i = 1; i <= m; i++)
    {
        fin >> x >> y;
        rau[x][y] = 1;
        rau[y][x] = 1;
    }
    bkt(1);
    return 0;
}