Advertisement
anoosykh95

bpm

Jul 21st, 2016
3,465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define unmatched -1
  4. #define MAX 505
  5. #define p push_back
  6.  
  7. int n,m,num;
  8. vector < int > v[MAX];
  9. int match[MAX];
  10. bool vis[MAX];
  11. bool dfs(int x)
  12. {
  13.     vis[x]=1;
  14.     for(int i=0;i<v[x].size();++i){
  15.        int node = v[x][i];
  16.        if(vis[node])continue;
  17.        vis[node]=1;
  18.        if(match[node]==unmatched || dfs(match[node])){
  19.            match[x]=node;
  20.            match[node]=x;
  21.            return 1;
  22.        }
  23.    }
  24.    return 0;
  25. }
  26. int main()
  27. {
  28.    while(cin>>n>>m>>num){
  29.         fill ( match , match+MAX , unmatched);
  30.  
  31.         for(int i=0;i<num;++i){
  32.            int fr,to;
  33.            scanf("%d %d",&fr,&to);
  34.            v[fr].p(to+n);
  35.            v[to+n].p(fr);
  36.        }
  37.  
  38.        int sizematching=0;
  39.  
  40.        for(int i=0;i<n;++i){
  41.            if(match[i]==unmatched){
  42.                fill(vis , vis+MAX , 0);
  43.                if(dfs(i)){
  44.                    sizematching++;
  45.                }
  46.            }
  47.        }
  48.        cout<<sizematching<<endl;
  49.        for(int i=0;i<n;++i){
  50.            if(match[i]!=unmatched){
  51.                cout<<i<<" "<<match[i]-n<<endl;
  52.            }
  53.        }
  54.    }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement