Pagini recente » Cod sursa (job #3246966) | Cod sursa (job #1606517) | Cod sursa (job #1786487) | Cod sursa (job #2256200) | Cod sursa (job #2714784)
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
//#define int long long
//#define hash_manual({x,y}) x*hash_key + y
const int Max = 2e4 + 5;
void nos()
{
//ios_base::sync_with_stdio(false);
//f.tie(NULL);
// g.tie(NULL);
}
vector < int > v[Max];
int stanga,dreapta;
int muchii;
const int hash_key = 1e5;
unordered_set < int > cost;
int source = 0;
int destination = Max - 1;
int hash_manual(pair < int , int > p)
{
return p.first * hash_key + p.second;
}
void add_edge( int n1, int n2)
{
v[n1].push_back(n2);
v[n2].push_back(n1);
cost.insert(hash_manual({n1,n2}));
}
int max_flow;
unordered_set < int > path_close;
void read()
{
f>>stanga>>dreapta>>muchii;
int i;
for(i=1; i<=muchii; i++)
{
int n1,n2;
f>>n1>>n2;
n2 += 1e4 + 1;
add_edge(source,n1);
add_edge(n1,n2);
add_edge(n2,destination);
path_close.insert(n2);
}
}
int parent[Max];
unordered_set < int > sol;
void relax(int leaf)
{
int path_flow = 1;
for(int nod = leaf; nod!=source; nod = parent[nod])
if(cost.find(hash_manual({parent[nod],nod})) == cost.end())
return;
cost.erase(hash_manual({leaf,destination}));
// cost.find({destination,leaf}) -> second += path_flow;
for(int nod = leaf; nod!=source; nod = parent[nod])
{
cost.erase(hash_manual({parent[nod],nod}));
cost.insert(hash_manual({nod,parent[nod]}));
int nod1 = parent[nod];
int nod2 = nod;
if(nod1 < nod2 and nod1 != source)
sol.insert(hash_manual({nod1,nod2}));
if(nod1 > nod2 and nod1 != destination)
sol.erase(hash_manual({nod2,nod1}));
}
max_flow += path_flow;
}
bool bfs()
{
bool relaxed[Max] = {};
bool viz[Max] = {};
queue < int > q;
q.push(source);
viz[source] = 1;
parent[source] = -1;
while(!q.empty())
{
int nod = q.front();
q.pop();
bool is_right = path_close.find(nod)!=path_close.end();
if(is_right and !relaxed[nod])
{
if(cost.find(hash_manual({nod,destination}))!=cost.end())
{
relaxed[nod] = 1;
relax(nod);
}
}
for(auto vec : v[nod])
if(!viz[vec])
{
if(cost.find(hash_manual({nod,vec}))!=cost.end())
{
q.push(vec);
parent[vec] = nod;
viz[vec] = 1;
}
}
}
return viz[destination];
}
void solve()
{
while(bfs());
g<<max_flow<<'\n';
for(auto it : sol)
g<<it / hash_key<<' '<<it % hash_key - 1e4 - 1<<'\n';
}
void restart()
{
}
int32_t main()
{
nos();
read();
solve();
restart();
return 0;
}