Pagini recente » Cod sursa (job #2131749) | Cod sursa (job #1315943) | Cod sursa (job #1657053) | Cod sursa (job #2019615) | Cod sursa (job #1393025)
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
using namespace std;
#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "felinare";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif
typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;
const int INF = (1LL << 31) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int NMAX = 8191;
int N, M, sol, cupl;
vector<int> A[NMAX + 5];
vector<int> B[NMAX + 5];
int P[NMAX + 5];
int Q[NMAX + 5];
bitset < 2 * NMAX + 5 > viz;
int light[NMAX + 5];
int pair_up(int x) {
if(viz[x])
return 0;
viz[x] = 1;
for(auto y : A[x])
if((!Q[y]) || pair_up(Q[y])) {
P[x] = y;
Q[y] = x;
return 1;
}
return 0;
}
void dfs(int x) {
viz[x] = 1;
for(auto y : A[x])
if(Q[y] != x && !viz[y + N]) {
viz[y + N] = 1;
dfs(Q[y]);
}
}
int main() {
int i, x, y, ok;
#ifndef ONLINE_JUDGE
freopen(inputFile.c_str(), "r", stdin);
freopen(outputFile.c_str(), "w", stdout);
#endif
scanf("%d%d", &N, &M);
while(M--) {
scanf("%d%d", &x, &y);
A[x].push_back(y);
B[y].push_back(x);
}
do {
viz = 0;
ok = 0;
for(i = 1; i <= N; i++)
if(!P[i])
ok |= pair_up(i);
} while(ok);
viz = 0;
for(i = 1; i <= N; i++)
if(!P[i])
dfs(i);
for(i = 1; i <= N; i++) {
if(P[i])
cupl++;
if(!viz[i])
light[i] |= 1;
if(viz[i + N])
light[i] |= 2;
}
printf("%d\n", 2 * N - cupl);
for(i = 1; i <= N; i++)
printf("%d\n", 3 ^ light[i]);
return 0;
}