Pagini recente » Cod sursa (job #1971820) | Cod sursa (job #3275124) | Cod sursa (job #2541073) | Cod sursa (job #1307285) | Cod sursa (job #3195162)
#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>
#define NMAX 8193
using namespace std;
typedef vector<int>::iterator iter;
ifstream fin ("felinare.in");
ofstream fout ("felinare.out");
struct noduri {
int nod, index;
};
struct muchie {
int x, y;
};
int n, m, index_l[NMAX], index_r[NMAX];
vector<int> nod_l[NMAX], nod_r[NMAX], G[NMAX];
vector<muchie> muchii;
bitset<NMAX> viz;
void citire()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
G[x].push_back(y);
muchii.push_back({x, y});
}
}
int match(int nod)
{
if (viz[nod])
return false;
viz[nod] = true;
for (iter it = G[nod].begin(); it != G[nod].end(); it++)
{
if (index_l[*it] == 0)
{
index_l[nod] = 1;
index_r[*it] = 1;
return true;
}
if (match(*it))
{
index_l[nod] = 1;
index_r[*it] = 1;
return true;
}
}
return false;
}
int main()
{
citire();
bool ok = true;
while (ok)
{
ok = false;
viz.reset();
for (int i = 1; i <= n; i++)
{
if (index_l[i] == 0)
ok |= match(i);
}
}
int sol = 0;
for (int i = 0; i < muchii.size(); i++)
{
sol += (index_l[muchii[i].x] + index_r[muchii[i].y]);
}
fout << sol << "\n";
for (int i = 0; i < muchii.size(); i++)
{
if (index_l[muchii[i].x] == 0 && index_r[muchii[i].y] == 1)
fout << 0 << "\n";
if (index_l[muchii[i].x] == 1 && index_r[muchii[i].y] == 0)
fout << 1 << "\n";
if (index_l[muchii[i].x] == 0 && index_r[muchii[i].y] == 1)
fout << 2 << "\n";
if (index_l[muchii[i].x] == 1 && index_r[muchii[i].y] == 1)
fout << 3 << "\n";
}
return 0;
}