Advertisement
yuhung94

CSES Coin Grid

Oct 28th, 2022
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 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. vector<int> v2[2*N];
  50. int tag[2*N];
  51. vector<pii> e;
  52. void paint(int x){
  53.     tag[x]=true;
  54.     for(int i:v2[x]){
  55.         if(!tag[i]){
  56.             paint(i);
  57.         }
  58.     }
  59. }
  60. void getce(int n){
  61.     int ans=bipartiteMatching(n);
  62.     cout<<ans<<"\n";
  63.     for(pii p2:e){
  64.         if(match[p2.S]!=p2.F){
  65.             v2[p2.S+n].eb(p2.F);
  66.         }
  67.         else v2[p2.F].eb(p2.S+n);
  68.     }
  69.     rep(i,1,n){
  70.         if(match[i]==-1){
  71.             paint(i+n);
  72.         }
  73.     }
  74.     rep(i,1,n){
  75.         if(tag[i]) cout<<1<<" "<<i<<"\n";
  76.     }
  77.     rep(i,n+1,2*n){
  78.         if(!tag[i]) cout<<2<<" "<<i-n<<"\n";
  79.     }
  80. }
  81. char c[N][N];
  82. signed main(){
  83.     quick
  84.     int n;
  85.     cin>>n;
  86.     rep(i,1,n){
  87.         rep(j,1,n){
  88.             cin>>c[i][j];
  89.             if(c[i][j]=='o') v[i].eb(j),e.eb(mp(i,j));
  90.         }
  91.     }
  92.     getce(n);
  93.     return 0;
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement