Cod sursa(job #2748618)

Utilizator Tazik_swapSarbu Mihail Tazik_swap Data 1 mai 2021 21:18:09
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.88 kb
//#pragma optimization_level 3
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordset;
*/
#define fr first
#define sc second
#define vec vector
#define pb push_back
#define pii pair<int, int>
#define forn(x,y) for(int x = 1 ; x <= (int)y ; ++x)
#define all(x) (x).begin(),(x).end()
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
 
using namespace std;
 
typedef long long ll;
typedef unsigned int uint;
typedef pair<ll,ll> pll;
typedef complex<int> point;
const int nmax = 10005;
const ll linf = 1e18;
const ll mod = 998244353;
const int inf = 1e9;
const int sq = 5000;

vec < int > g[nmax];

int lf[nmax], rg[nmax];
bitset <nmax> viz;

ifstream fin ("cuplaj.in");
ofstream fout ("cuplaj.out");

bool solve(int nod){
	if(viz[nod]){
		return false;
	}
	viz[nod] = 1;
	for(int i : g[nod]){
		if(!lf[i]){
			lf[i] = nod;
			rg[nod] = i;
			return true;
		}
	}
	for(int i : g[nod]){
		if(solve(lf[i])){
			lf[i] = nod;
			rg[nod] = i;
			return true;
		}
	}
	return false;
}

int main(){
	 int n, m, e;
	 fin >> n >> m >> e;
	 while(e--){
	 	int a, b;
	 	fin >> a >> b;
	 	g[a].pb(b);
	 }
	 for(bool ch = true; ch;){
	 	ch = false;
	 	viz.reset();	
	 	for(int i = 1; i <= n; i++){
	 		if(rg[i] == 0){
	 			ch |= solve(i);
	 		}
	 	}
	 }
	 int cnt = 0;
	 for(int i = 1; i <= n; i++){
	 	cnt += rg[i] > 0;
	 }
	 fout << cnt << '\n';
	 for(int i = 1; i <= n; i++){
	 	if(rg[i]){
	 		fout << i << ' ' << rg[i] << '\n';
	 	}
	 }
}