Cod sursa(job #2539150)

Utilizator RedXtreme45Catalin RedXtreme45 Data 5 februarie 2020 18:20:43
Problema Suma si numarul divizorilor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.88 kb
#include <fstream>
#include <vector>
#define Nmax 50001
using namespace std;
ifstream fin("domino.in");
ofstream fout("domino.out");
int n,rot[Nmax],ap[12],nr,v[12][12],fol[Nmax];
vector <pair<int,int> > G[12];
vector <int> sol;
void plecare(int start)
{
    while (!G[start].empty())
    {
        int x=G[start].back().first;
        int y=G[start].back().second;
        G[start].pop_back();

        if (fol[y]==0){
                if (v[start][x]==0)
            rot[y]=1;
            fol[y]=1;
            sol.push_back(y);
            plecare(x);
        }
    }

}
int main()
{
    int a,b,i;
    fin>>n;
    for (i=1; i<=n; i++)
    {
        fin>>a>>b;
        ap[a]++;
        ap[b]++;
        G[a].push_back({b,i});
        G[b].push_back({a,i});
        v[a][b]=1;
    }
    for (i=0; i<=9; i++)
    {
        if (ap[i]%2==1)
            nr++;
    }
    int x,x1;
    if (nr==2)
    {
        for (i=0; i<=9; i++)
        {
            if (ap[i]%2==1)
            {
                x1=x;
                x=i;
            }
        }
    }
    if (nr==0)
    {
        for (i=0; i<=9; i++)
        {
            if (ap[i]!=0)
            {
                x1=x;
                x=i;
            }
        }
    }
    if (nr==1)
    {
        for (i=0; i<=9; i++)
        {
            if (ap[i]!=0 && ap[i]%2==0)
                x=i;
            if (ap[i]%2==1)
                x1=i;
        }
    }
    if (nr>2)
    {
        fout<<0;
    }
    else
    {
        fout<<1<<"\n";
        int o=max(v[x][x1],v[x1][x]);
        int nr=0;
        for (auto i:G[x])
        {
            if (i.first==x1)
                G[x].erase(G[x].begin()+nr);
            nr++;
        }

        plecare(x);

        sol.push_back(o);
        for (auto i:sol)
            fout<<i<<" "<<rot[i]<<"\n";

    }
    return 0;
}