Cod sursa(job #3329184)

Utilizator MagicantPlusIuoras Andrei MagicantPlus Data 12 decembrie 2025 09:30:50
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 6.3 kb
#define programstate2
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned ll
#define vec vector
#define flist forward_list
#define um unordered_map
#define us unordered_set
#define prioq priority_queue
#define all(x) x.begin(), x.end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define ld long double
#define cd complex<ld>
#define pll pair<ll, ll>
#define tll tuple<ll, ll, ll>
#define ass assign
#define fun function

#define LL_MAX LLONG_MAX
#define LL_MIN LLONG_MIN
#define ULL_MAX ULLONG_MAX
#define LD_MAX LDBL_MAX
#define LD_MIN LDBL_MIN

#define nl " \n"

#define vv(type, name, n, ...) vector<vector<type>> name(n, vector<type>(__VA_ARGS__))
#define vvv(type, name, n, m, ...) \
	vector<vector<vector<type>>> name(n, vector<vector<type>>(m, vector<type>(__VA_ARGS__)))

// https://trap.jp/post/1224/
#define rep1(n) for(ll i=0; i<(ll)(n); ++i)
#define rep2(i,n) for(ll i=0; i<(ll)(n); ++i)
#define rep3(i,a,b) for(ll i=(ll)(a); i<(ll)(b); ++i)
#define rep4(i,a,b,c) for(ll i=(ll)(a); i<(ll)(b); i+=(ll)(c))
#define cut4(a,b,c,d,e,...) e
#define rep(...) cut4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define rep1e(n) for(ll i=1; i<=(ll)(n); ++i)
#define rep2e(i,n) for(ll i=1; i<=(ll)(n); ++i)
#define rep3e(i,a,b) for(ll i=(ll)(a); i<=(ll)(b); ++i)
#define rep4e(i,a,b,c) for(ll i=(ll)(a); i<=(ll)(b); i+=(ll)(c))
#define repe(...) cut4(__VA_ARGS__,rep4e,rep3e,rep2e,rep1e)(__VA_ARGS__)
#define per1(n) for(ll i=(ll)(n)-1; i>=0; --i)
#define per2(i,n) for(ll i=(ll)(n)-1; i>=0; --i)
#define per3(i,a,b) for(ll i=(ll)(a)-1; i>=(ll)(b); --i)
#define per4(i,a,b,c) for(ll i=(ll)(a)-1; i>=(ll)(b); i-=(ll)(c))
#define per(...) cut4(__VA_ARGS__,per4,per3,per2,per1)(__VA_ARGS__)
#define per1e(n) for(ll i=(ll)(n); i>=1; --i)
#define per2e(i,n) for(ll i=(ll)(n); i>=1; --i)
#define per3e(i,a,b) for(ll i=(ll)(a); i>=(ll)(b); --i)
#define per4e(i,a,b,c) for(ll i=(ll)(a); i>=(ll)(b); i-=(ll)(c))
#define pere(...) cut4(__VA_ARGS__,per4e,per3e,per2e,per1e)(__VA_ARGS__)
#define rep_subset(i,s) for(ll i=0;i!=-1;i=i==(s)?-1:(i|~(s))+1&(s))
#define per_subset(i,s) for(ll i=(s);i!=-1;i=i==0?-1:i-1&(s))

template<class T, class S> bool mkmin(T& a, const initializer_list<S>& b)
{S m = *min_element(all(b)); if(m<a){a=m;return 1;}return 0;}
template<class T, class S> bool mkmax(T& a, const initializer_list<S>& b)
{S m = *max_element(all(b)); if(m>a){a=m;return 1;}return 0;}
template<class T, class S> bool mkmin(T& a, const S& b){if(b<a){a=b;return 1;}return 0;}
template<class T, class S> bool mkmax(T& a, const S& b){if(b>a){a=b;return 1;}return 0;}

#ifdef programstate0
#endif

#ifdef programstate1
ifstream fin("./special/input.txt");
ofstream ferr("./special/messages.txt");
#define cerr ferr
#define cin fin
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(vector<vector<T>> x){cerr << "\n"; for(auto i: x) {for(auto j : i) {cerr << j << ' ';} cerr << "\n";}}
template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(unordered_set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 777771449
#endif

const ll MOD = 1e9 + 7;
const ll INF = (1ll << 59);

#ifdef programstate2
const string FILENAME = "cuplaj";
ifstream fin(FILENAME + ".in");
ofstream fout(FILENAME + ".out");
#define cin fin
#define cout fout
#endif

ll nnt() {ll x; cin >> x; return x;}
string nst() {string x; cin >> x; return x;}
char nct() {char x; cin >> x; return x;}

class Dinic
{
public:

    struct Edge
    {
        ll to, flow, res;
        Edge *back;
    };
    
    ll n;
    vec<flist<Edge>> adj;

    void build(ll x)
    {
        n = x;
        adj.resize(n);
    }
    void addEdge(ll a, ll b, ll c, bool undirected = 0)
    {
        adj[a].pf({b, 0, c, NULL});
        adj[b].pf({a, 0, c*undirected, &adj[a].front()});
        adj[a].front().back = &adj[b].front();
    }
    void pushFlow(Edge& e, ll flow)
    {
        e.flow += flow, e.res -= flow;
        e.back->flow -= flow, e.back->res += flow;
    }
    ll maxFlow(ll s, ll t)
    {
        vec<ll> level(n);
        vec<flist<Edge>::iterator> ptr(n);
        ll ans = 0;

        fun<bool()> bfs([&]()->bool
        {
            queue<ll> q({s});
            fill(all(level), -1);
            level[s] = 0;

            while(q.size() && level[t] == -1)
            {
                ll o = q.front();
                q.pop();

                for(auto& e : adj[o])
                {
                    if(level[e.to] == -1 && e.res)
                    {
                        level[e.to] = level[o] + 1;
                        q.push(e.to);
                    }
                }
            }
            return level[t] != -1;
        });
        
        fun<ll(ll,ll)> dfs([&](ll o, ll flow)->ll
        {
            if(o == t || !flow) return flow;
            for(; ptr[o] != adj[o].end(); ptr[o]++)
            {
                Edge& e = *ptr[o];

                if(level[e.to] != level[o]+1) continue;
                if(ll pushed = dfs(e.to, min(flow, e.res)))
                {
                    pushFlow(e, pushed);
                    return pushed;
                }
            }
            return 0;
        });
        
        while(bfs())
        {
            rep(n) ptr[i] = adj[i].begin();
            while(ll pushed = dfs(s, INF))
                ans += pushed;
        }
        
        return ans;
    }
};

int main()
{
	ll L, R, m;
	cin >> L >> R >> m;

	Dinic dinic;
	dinic.build(L+R+2);

	rep(m)
	{
		ll a = nnt()-1, b = nnt()-1;
		dinic.addEdge(a, b+L, 1);
	}
	
	rep(o, L) dinic.addEdge(L+R, o, 1);
	rep(o, R) dinic.addEdge(o+L, L+R+1, 1);

	cout << dinic.maxFlow(L+R, L+R+1) << nl;

	rep(o, L)
	{
		for(auto& e : dinic.adj[o])
		{
			if(e.flow && e.to != L+R)
			{
				cout << o+1 << ' ' << e.to - L + 1 << nl;
				break;
			}
		}
	}

	return 0;
}