ec1117

Untitled

May 31st, 2021 (edited)
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. #define f first
  5. #define s second
  6. #define pi pair<int,int>
  7. #define FOR(i,a,b) for(int i=a;i<b;i++)
  8. #define For(i,a) FOR(i,0,a)
  9. #define trav(a,b) for(auto& a:b)
  10. #define all(x) begin(x),end(x)
  11. #define sz(x) (int)(x.size())
  12. #define vi vector<int>
  13. #define vpi vector<pi>
  14. #define pb push_back
  15. #define ins insert
  16. #define f first
  17. #define f first
  18.  
  19. const int MX=305;
  20. const int MX2=MX*MX;
  21.  
  22. vpi adj[MX][2];
  23. bool on[MX2];
  24. bool done[MX][2],vis[MX][2];
  25. vpi edg;
  26.  
  27. bool dfs(int n, int typ){
  28.     // cout<<"HI: "<<n<<" "<<typ<<endl;
  29.     if(vis[n][typ])return false;
  30.     vis[n][typ]=true;
  31.  
  32.     if(typ && !done[n][typ]){
  33.         // cout<<"GOOD "<<n<<" "<<typ<<endl;
  34.         return true;
  35.     }
  36.     trav(x,adj[n][typ]){
  37.         if(typ==0){
  38.             if(!on[x.s]){
  39.                 bool z=dfs(x.f,1);
  40.                 if(z){
  41.                     done[n][0]=true;
  42.                     done[x.f][1]=true;
  43.                     on[x.s]=true;
  44.                     return true;
  45.                 }
  46.             }
  47.         } else {
  48.             if(on[x.s]){
  49.                 bool z=dfs(x.f,0);
  50.                 if(z){
  51.                     on[x.s]=false;
  52.                     return true;
  53.                 }
  54.             }
  55.         }
  56.     }
  57.     return false;
  58. }
  59.  
  60. int main(){
  61.     int L,R,M;cin>>L>>R>>M;
  62.     For(i,M){
  63.         int a,b;cin>>a>>b;
  64.         adj[a][0].pb({b,i});
  65.         adj[b][1].pb({a,i});
  66.         edg.pb({a,b});
  67.     }
  68.     For(i,L){
  69.         if(!done[i][0]){
  70.             memset(vis,false,sizeof vis);
  71.             dfs(i,0);
  72.         }
  73.         // cout<<"DONE: "<<i<<endl;
  74.     }
  75.     int cnt=0;
  76.     For(i,L)if(done[i][0])cnt++;
  77.     cout<<cnt<<endl;
  78.     For(i,M)if(on[i]){
  79.         pi tmp=edg[i];
  80.         cout<<tmp.f<<" "<<tmp.s<<endl;
  81.     }
  82. }
Add Comment
Please, Sign In to add comment