Cod sursa(job #2480449)

Utilizator stefzahZaharia Stefan Tudor stefzah Data 25 octombrie 2019 17:08:46
Problema Dusman Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>
#include <iostream>

using namespace std;
ifstream fin("dusman.in");
ofstream fout("dusman.out");
int N, K, M, ct;
int sol[1005];
bool a[1005][1005];
bool viz[1005], ok;

void Afisare() {
    int i;
    for (i = 1; i <= N; i++)
        fout << sol[i] << " ";
}

void Back(int top) {
    int i;
    if (top == N + 1) {
        ct++;
        if (ct == K) {
            Afisare();
            ok = 1;
        }
    } else
        for (i = 1; i <= N; i++)
            if (viz[i] == 0 && a[sol[top - 1]][i] == 0) {
                sol[top] = i;
                viz[i] = true;
                if (ok == 0)
                    Back(top + 1);
                viz[i] = false;
            }
}

int main() {
    int i, x, y;
    fin >> N >> K >> M;
    for (i = 1; i <= M; i++) {
        fin >> x >> y;
        a[x][y] = true;
        a[y][x] = true;
    }
    Back(1);
}