Cod sursa(job #1686683)

Utilizator Vele_GeorgeVele George Vele_George Data 12 aprilie 2016 12:56:38
Problema Dusman Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <iostream>
#include <fstream>
#include <map>
#define nmax 1005
using namespace std;

int n, m, k, sol[nmax], nr = 0, x, y;
map <int, map<int, bool> > enemy;
bool used [nmax], gasit_solutie = 0;


ifstream f("dusman.in");
ofstream g("dusman.out");

void ins(int nod, int poz)
{

   if (gasit_solutie) return;

    sol[poz] = nod;
    used[nod] = 1;

    if (poz == n)
    {
        ++nr;
        if (nr == k)
        {
            for(int i=1; i<=n; ++i)
                g << sol[i] << " ";
            gasit_solutie = 1;
        }
        used[nod] = 0;
        return;
    }

    for(int i=1; i<=n; ++i)
        if (!used[i] && !enemy[nod][i])
        {
            ins(i, poz+1);
        }
    used[nod] = 0;
}


inline void back_track()
{
    for(int i=1; i<=n; ++i)
    {

        if (gasit_solutie) return;
        ins(i, 1);
    }
}
int main()
{

    f >> n >> k >> m;

    for(int i=1; i<=m; ++i)
    {
        f >> x >> y;
        enemy[x][y] = 1;
        enemy[y][x] = 1;
    }
    back_track();

    f.close();
    g.close();
    return 0;
}