Pagini recente » Cod sursa (job #1606659) | Cod sursa (job #1686646) | Cod sursa (job #2608950) | Cod sursa (job #1622862) | Cod sursa (job #2714787)
#pragma GCC optimize("Ofast")
#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;
#define BUF_SIZE 1<<17
char buffer[BUF_SIZE];
int pbuf = BUF_SIZE;
FILE *fi, *fo;
inline char nextch(){
if(pbuf == BUF_SIZE){
fread(buffer, BUF_SIZE, 1, fi);
pbuf=0;
}
return buffer[pbuf++];
}
inline int nextnum(){
int a = 0;
char c = nextch();
while(!isdigit(c))
c = nextch();
while(isdigit(c)){
a = a*10+c-'0';
c = nextch();
}
return a;
}
void read()
{
fi = fopen("cuplaj.in","r");
//f>>stanga>>dreapta>>muchii;
stanga = nextnum();
dreapta = nextnum();
muchii = nextnum();
int i;
for(i=1; i<=muchii; i++)
{
int n1,n2;
n1 = nextnum();
n2 = nextnum();
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;
}