Cod sursa(job #3254186)

Utilizator ArthurrrfubinacaARthur Paun Arthurrrfubinaca Data 6 noiembrie 2024 14:01:46
Problema Arbore partial de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream fin("hamilton.in");
ofstream fout("hamilton.out");
void citire()
{
    fin>>n;
    while(fin>>x>>y)
    {
        g[x].push_back(y);
        g[y].push_back(x);
    }
}
void afisare()
{
    for(int i=1;i<=n;i++)
        fout<<st[i]<<" ";
    fout<<st[1];
}
bool valid(int k)
{
    for(int i=1;i<k;i++)
    {
        if(st[i]==st[k])
            return 0;
    }
    if(find(g[st[k]].begin(),g[st[k]].end(),st[k-1])==g[st[k]].end())
        return 0;
    if(k==n && find(g[st[k]].begin(),g[st[k]].end(),st[1])==g[st[k]].end())
        return 0;
    return 1;
}
int bt(int k)
{
    for(int i=1;i<=n,ok=0;i++)
    {
        st[k]=i;
        if(valid(k)==1)
        {
            if(k==n)
            {
                ok=1;
                return;
            }
            else
                return(bt(k+1));
        }
    }
}
int main()
{
    citire();
    stiv[1]=1;
    bt(2);
    if(nrsol==0)
        fout<<0;
    else
        {
            fout<<1<<'\n';
            afisare();
        }
    return 0;
}