結果
問題 |
No.1479 Matrix Eraser
|
ユーザー |
|
提出日時 | 2025-01-26 01:09:10 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 135 ms / 3,000 ms |
コード長 | 1,669 bytes |
コンパイル時間 | 1,014 ms |
コンパイル使用メモリ | 86,636 KB |
実行使用メモリ | 9,532 KB |
最終ジャッジ日時 | 2025-01-26 01:09:21 |
合計ジャッジ時間 | 5,123 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 39 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:69:29: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions] 69 | auto[u,v]=E[i++].second; | ^
ソースコード
#include<iostream> #include<vector> #include<algorithm> #include<cassert> using namespace std; #include<algorithm> #include<vector> struct bimatch{ int n; vector<vector<int> >G; vector<int>match; vector<bool>used; bimatch(int _n=0):n(_n),G(n),match(n),used(n){} void add_edge(int u,int v) { G[u].push_back(v); G[v].push_back(u); } bool dfs(int v) { used[v]=true; for(int u:G[v]) { int w=match[u]; if(w<0||!used[w]&&dfs(w)) { match[v]=u; match[u]=v; return true; } } return false; } int count() { int ans=0; bool flag=true; fill(match.begin(),match.end(),-1); while(flag) { flag=false; fill(used.begin(),used.end(),false); for(int v=0;v<n;v++)if(!used[v]&&match[v]<0&&dfs(v))ans++,flag=true; } return ans; } }; int H,W; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin>>H>>W; vector<pair<int,pair<int,int> > >E; E.reserve(H*W); for(int i=0;i<H;i++)for(int j=0;j<W;j++) { int a;cin>>a; if(a)E.push_back(make_pair(a,make_pair(i,j))); } sort(E.begin(),E.end()); int ans=0; for(int i=0;i<E.size();) { int a=E[i].first; vector<int>U,V; while(i<E.size()&&E[i].first==a) { auto[u,v]=E[i++].second; U.push_back(u); V.push_back(v); } vector<int>u=U,v=V; sort(u.begin(),u.end()); sort(v.begin(),v.end()); u.erase(unique(u.begin(),u.end()),u.end()); v.erase(unique(v.begin(),v.end()),v.end()); bimatch P(u.size()+v.size()); for(int k=0;k<U.size();k++) { int x=lower_bound(u.begin(),u.end(),U[k])-u.begin(); int y=lower_bound(v.begin(),v.end(),V[k])-v.begin(); P.add_edge(x,y+u.size()); } ans+=P.count(); } cout<<ans<<endl; }