Advertisement
yuhung94

tioj 1253

Oct 28th, 2022
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1.  
  2. #pragma GCC optimzize("Ofast,no-stack-protector")
  3. #include<bits/stdc++.h>
  4. #define int long long
  5. #define quick ios::sync_with_stdio(0);cin.tie(0);
  6. #define rep(x,a,b) for(int x=a;x<=b;x++)
  7. #define repd(x,a,b) for(int x=a;x>=b;x--)
  8. #define lowbit(x) (x&-x)
  9. #define sz(x) (int)(x.size())
  10. #define F first
  11. #define S second
  12. #define all(x) x.begin(),x.end()
  13. #define mp make_pair
  14. #define eb emplace_back
  15. using namespace std;
  16. typedef pair<int,int> pii;
  17. void debug(){
  18.     cout<<"\n";
  19. }
  20. template <class T,class ... U >
  21. void debug(T a, U ... b){
  22.     cout<<a<<" ",debug(b...);
  23. }
  24. const int N=1e3+7;
  25. const int INF=1e18;
  26. vector<int> v[N];//X的鄰點
  27. int match[N]; // Y所匹配的節點
  28. bool vis[N]; //在這次尋找中是否被走過
  29. int dfs(int x){
  30.     for(int y:v[x]){
  31.         if(vis[y]) continue;
  32.         vis[y]=true;
  33.         if(match[y]==-1||dfs(match[y])){
  34.             match[y]=x;
  35.             return true;
  36.         }
  37.     }
  38.     return false;
  39. }
  40. int bipartiteMatching(int n){
  41.     fill(match,match+n+1,-1);
  42.     int ans=0;
  43.     rep(i,1,n){
  44.         fill(vis,vis+n+1,0);
  45.         if(dfs(i)) ans++;
  46.     }
  47.     return ans;
  48. }
  49. signed main(){
  50.     quick
  51.     int n,k;
  52.     int Case=1;
  53.     while(cin>>n>>k&&n&&k){
  54.         fill(v,v+n+1,vector<int>());
  55.         while(k--){
  56.             int a,b;
  57.             cin>>a>>b;
  58.             v[a].eb(b);
  59.         }
  60.         cout<<"Case #"<<Case++<<":"<<bipartiteMatching(n)<<"\n";
  61.     }
  62.     return 0;
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement