/*
Copyright 2011 Marek "p2004a" Rusinowski
Turbo matching algorithm
*/
#include <cstdio>
#include <cstring>
#include <vector>
#define MAXN 10000
std:: vector < int > edges[ MAXN] ;
int part[ MAXN] , match[ MAXN] , n, m;
bool visited[ MAXN] ;
bool create_bipartite_graph( int v, int type) {
part[ v] = type;
for ( unsigned i = 0 ; i < edges[ v] .size ( ) ; ++ i) {
if ( part[ edges[ v] [ i] ] ! = ( type ^ 3 ) ) {
if ( part[ edges[ v] [ i] ] == type || ! create_bipartite_graph( edges[ v] [ i] , type ^ 3 ) ) {
return false ;
}
}
}
return true ;
}
bool dfs( int v) {
visited[ v] = true ;
for ( unsigned i = 0 ; i < edges[ v] .size ( ) ; ++ i) {
if ( ! visited[ edges[ v] [ i] ] && ( ! ~match[ edges[ v] [ i] ] || ( visited[ edges[ v] [ i] ] = true , dfs( match[ edges[ v] [ i] ] ) ) ) ) {
match[ v] = edges[ v] [ i] ;
match[ edges[ v] [ i] ] = v;
return true ;
}
}
return false ;
}
void turbo_matching( ) {
bool con = true ;
while ( con) {
con = false ;
memset ( visited, 0x00 , sizeof ( bool ) * n) ;
for ( int i = 0 ; i < n; ++ i) {
if ( part[ i] & 1 && ! visited[ i] && ! ~match[ i] ) {
con | = dfs( i) ;
}
}
}
}
int main( ) {
int a, b;
scanf ( "%d %d" , & n, & m) ;
for ( int i = 0 ; i < m; ++ i) {
scanf ( "%d %d" , & a, & b) ;
edges[ -- a] .push_back ( -- b) ;
edges[ b] .push_back ( a) ;
}
if ( ! create_bipartite_graph( 0 , 1 ) ) {
printf ( "false\n " ) ;
return 0 ;
}
memset ( match, 0xFF , sizeof ( int ) * n) ;
turbo_matching( ) ;
for ( int i = 0 ; i < n; ++ i) {
if ( ~match[ i] ) {
printf ( "%d %d\n " , i + 1 , match[ i] + 1 ) ;
match[ match[ i] ] = - 1 ;
}
}
return 0 ;
}
LyoKICBDb3B5cmlnaHQgMjAxMSBNYXJlayAicDIwMDRhIiBSdXNpbm93c2tpCiAgV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ogIH0KICByZXR1cm4gZmFsc2U7Cn0KCnZvaWQgdHVyYm9fbWF0Y2hpbmcoKSB7CiAgYm9vbCBjb24gPSB0cnVlOwogIHdoaWxlIChjb24pIHsKICAgIGNvbiA9IGZhbHNlOwogICAgbWVtc2V0KHZpc2l0ZWQsIDB4MDAsIHNpemVvZihib29sKSAqIG4pOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpIHsKICAgICAgaWYgKHBhcnRbaV0gJiAxICYmICF2aXNpdGVkW2ldICYmICF+bWF0Y2hbaV0pIHsKICAgICAgICBjb24gfD0gZGZzKGkpOwogICAgICB9CiAgICB9CiAgfQp9CgppbnQgbWFpbigpIHsKICBpbnQgYSwgYjsKICBzY2FuZigiJWQgJWQiLCAmbiwgJm0pOwogIGZvciAoaW50IGkgPSAwOyBpIDwgbTsgKytpKSB7CiAgICBzY2FuZigiJWQgJWQiLCAmYSwgJmIpOwogICAgZWRnZXNbLS1hXS5wdXNoX2JhY2soLS1iKTsKICAgIGVkZ2VzW2JdLnB1c2hfYmFjayhhKTsKICB9CiAgaWYgKCFjcmVhdGVfYmlwYXJ0aXRlX2dyYXBoKDAsIDEpKSB7CiAgICBwcmludGYoImZhbHNlXG4iKTsKICAgIHJldHVybiAwOwogIH0KICBtZW1zZXQobWF0Y2gsIDB4RkYsIHNpemVvZihpbnQpICogbik7CiAgdHVyYm9fbWF0Y2hpbmcoKTsKICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgaWYgKH5tYXRjaFtpXSkgewogICAgICBwcmludGYoIiVkICVkXG4iLCBpICsgMSwgbWF0Y2hbaV0gKyAxKTsKICAgICAgbWF0Y2hbbWF0Y2hbaV1dID0gLTE7CiAgICB9CiAgfQogIHJldHVybiAwOwp9Cg==
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:52: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
prog.cpp:54: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
stdout