Cod sursa(job #3004421)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 16 martie 2023 12:21:48
Problema Felinare Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.07 kb
#include <bits/stdc++.h>
//#pragma GCC optimize ("03")
#define FastIO ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define FILES freopen("felinare.in" , "r" , stdin) , freopen("felinare.out" , "w" , stdout)
#define ll long long
#define ull unsigned long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define qwerty1 first
#define qwerty2 second
#define qwerty3 -> first
#define qwerty4 -> second
#define umap unordered_map
#define uset unordered_set
#define pii pair < ll , ll >
#define pq priority_queue
#define dbg(x) cerr << #x << ": " << x << '\n'

namespace FastRead
{
    char __buff[5000];ll __lg = 0 , __p = 0;
    char nc()
    {
        if(__lg == __p){__lg = fread(__buff , 1 , 5000 , stdin);__p = 0;if(!__lg) return EOF;}
        return __buff[__p++];
    }
    template<class T>void read(T&__x)
    {
        T __sgn = 1; char __c;while(!isdigit(__c = nc()))if(__c == '-')__sgn = -1;
        __x = __c - '0';while(isdigit(__c = nc()))__x = __x * 10 + __c - '0';__x *= __sgn;
    }
}

using namespace FastRead;
using namespace std;

const ll N = 9e3 + 10;
const ll M = 1e9 + 7;
const ld PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n , m;
set < pair < int , pii > , greater < pair < int , pii > > > s;
vector < int > GI[N] , GO[N];
int in[N] , out[N];
bool state[N][2];

signed main()
{
	#ifndef ONLINE_JUDGE
		FastIO , FILES;
	#endif

	int i;

	cin >> n >> m;

	for(i = 1 ; i <= m ; i++)
    {
        int x , y;

        cin >> x >> y;

        GO[x].pb(y);
        GI[y].pb(x);

        ++in[y];
        ++out[x];
    }

    for(i = 1 ; i <= n ; i++)
    {
        state[i][0] = state[i][1] = 1;

        s.insert({in[i] , {i , 0}});
        s.insert({out[i] , {i , 1}});
    }

    int edg = m;

    while(edg)
    {
        pair < int , pii > p = *s.begin();
        s.erase(s.begin());

        int node = p.second.first;
        int ori = p.second.second;
        int turnOff = p.first;

        state[node][ori] = 0;
        edg -= turnOff;

        if(ori == 0)
        {
            for(auto adj : GI[node])
                if(state[adj][1])
                {
                    s.erase({out[adj] , {adj , 1}});
                    --out[adj];
                    s.insert({out[adj] , {adj , 1}});
                }
        }
        else
        {
            for(auto adj : GO[node])
                if(state[adj][0])
                {
                    s.erase({in[adj] , {adj , 0}});
                    --in[adj];
                    s.insert({in[adj] , {adj , 0}});
                }
        }
    }

    int on = 0;

    for(i = 1 ; i <= n ; i++)
        on += state[i][0] + state[i][1];

    cout << on << '\n';

    for(i = 1 ; i <= n ; i++)
    {
        if(state[i][0] == state[i][1])
        {
            if(state[i][0]) cout << 3;
            else cout << 0;
        }
        else if(state[i][0]) cout << 2;
        else cout << 1;

        cout << '\n';
    }

    return 0;
}