Pagini recente » Cod sursa (job #7835) | Cod sursa (job #2525871) | Cod sursa (job #2879628) | Cod sursa (job #708271) | Cod sursa (job #2145158)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("felinare.in");
ofstream g("felinare.out");
const int nmax = (1<<13);
vector <int> ls[nmax];
int lft[nmax], rgt[nmax], viz[nmax], K, n, m, i, j, x, y, sol1, stinsst[nmax], stinsdr[nmax];
bool pairup(int x) {
if (viz[x] == K) return 0;
int l = ls[x].size(), i, y;
viz[x] = K;
for (i = 0; i < l; i++) {
y = ls[x][i];
if (rgt[y] == 0) {
rgt[y] = x;
lft[x] = y;
return 1;
}
}
for (i = 0; i < l; i++) {
y = ls[x][i];
if (pairup(rgt[y])) {
rgt[y] = x;
lft[x] = y;
return 1;
}
}
return 0;
}
void aprinde(int x) {
int l = ls[x].size(), i, y;
for (i = 0; i < l; i++) {
y = ls[x][i];
if (stinsdr[y] == 0) {
stinsdr[y] = 1;
stinsst[x] = 0;
aprinde(rgt[y]);
}
}
}
int main() {
f >> n >> m;
for (i = 1; i <= m; i++) {
f >> x >> y;
ls[x].push_back(y);
}
for (i = 1; i <= n; i++) {
K = i;
sol1 += pairup(i);
}
g << 2*n-sol1 << '\n';
for (i = 1; i <= n; i++)
stinsst[i] = (lft[i]!=0);
for (i = 1; i <= n; i++)
if (stinsst[i] == 0)
aprinde(i);
for (i = 1; i <= n; i++) {
int cnt = 0;
if (stinsst[i] == 0) cnt++;
if (stinsdr[i] == 0) cnt+=2;
g << cnt << '\n';
}
return 0;
}