Pagini recente » Cod sursa (job #2763003) | Cod sursa (job #700575) | Cod sursa (job #1150653) | Cod sursa (job #1515493) | Cod sursa (job #1155283)
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("domino.in");
ofstream fout("domino.out");
int N;
int nodnow, pred, nr_nod;
int muchie[10][10];
vector<int> V[10], A[10][10];
stack<int> ST;
bool instack[10], nr[10], used[50002];
int is_euler(int nod)
{
ST.push(nod);
instack[nod] = true;
while (!ST.empty())
{
int now = ST.top();
ST.pop();
for (vector<int>::iterator it = V[now].begin(); it != V[now].end(); ++it)
{
if (!instack[*it])
{
ST.push(*it);
instack[*it] = true;
}
}
}
int ok = 1;
for (int i = 0; i <= 9; ++i)
{
if (!nr[i])
continue;
if (V[i].size() % 2 == 1 || !instack[i])
{
if (V[i].size() % 2 == 1)
{
++nr_nod;
nodnow = i;
if (nr_nod == 3)
{
ok = 0;
break;
}
}
if (!instack[i])
{
ok = 0;
break;
}
}
}
nr_nod = 0;
return ok;
}
void solve_euler(int nod)
{
ST.push(nod);
while (!ST.empty())
{
int now = ST.top();
bool ok = true;
for (vector<int>::iterator it = V[now].begin(); it != V[now].end(); ++it)
{
if (!muchie[now][*it])
continue;
ok = false;
ST.push(*it);
--muchie[now][neighbour];
--muchie[neighbour][now];
break;
}
if (ok)
{
//fout << now << ' ';
if (!nr_nod)
{
pred = now;
ST.pop();
nr_nod = 1;
continue;
}
bool ok = false;
for (vector<int>::iterator it = A[pred][now].begin(); it != A[pred][now].end(); ++it)
{
if (!used[*it])
{
ok = true;
used[*it] = true;
fout << *it << ' ' << 0 << '\n';
continue;
}
}
if (!ok)
{
for (vector<int>::iterator it = A[now][pred].begin(); it != A[now][pred].end(); ++it)
{
if (!used[*it])
{
used[*it] = true;
fout << *it << ' ' << 1 << '\n';
continue;
}
}
}
pred = now;
ST.pop();
}
}
}
int main()
{
fin >> N;
for (int i = 1, nod1, nod2; i <= N; ++i)
{
fin >> nod1 >> nod2;
nr[nod1] = true;
nr[nod2] = true;
nodnow = nod2;
++muchie[nod1][nod2];
++muchie[nod2][nod1];
A[nod1][nod2].push_back(i);
V[nod1].push_back(nod2);
V[nod2].push_back(nod1);
}
if (!is_euler(nodnow))
{
fout << 0 << '\n';
fin.close();
fout.close();
return 0;
}
fout << 1 << '\n';
solve_euler(nodnow);
fin.close();
fout.close();
return 0;
}