結果

問題 No.883 ぬりえ
ユーザー tempura_pptempura_pp
提出日時 2019-09-13 22:15:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 2,677 bytes
コンパイル時間 1,355 ms
コンパイル使用メモリ 111,596 KB
実行使用メモリ 55,412 KB
最終ジャッジ日時 2023-09-17 15:30:09
合計ジャッジ時間 2,716 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 100 ms
55,412 KB
testcase_16 AC 25 ms
16,396 KB
testcase_17 AC 15 ms
11,572 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;


struct edge{int to;ll cap;int rev;};
struct Dinic{
    int n;
    vector<vector<edge>> v;
    vector<int> dist,iter;
    Dinic(int sz):n(sz),v(sz),dist(sz),iter(sz){}
    
    void addedge(int from,int to,ll cap){
        int x=v[to].size(),y=v[from].size();
        v[from].push_back({to,cap,x});
        v[to].push_back({from,0,y});
    }
    
    void bfs(int s){
        fill(dist.begin(),dist.end(),-1);
        queue<int> q;
        dist[s]=0;
        q.push(s);
        while(q.size()){
            int x=q.front();q.pop();
            rep(i,v[x].size()){
                edge &e=v[x][i];
                if(e.cap>0&&dist[e.to]<0){
                    dist[e.to]=dist[x]+1;
                    q.push(e.to);
                }
            }
        }
    }
    
    ll dfs(int x,int t,ll f){
        if(x==t)return f;
        for(int& i=iter[x];i<(int)v[x].size();++i){
            edge& e=v[x][i];
            if(e.cap>0&&dist[x]<dist[e.to]){
                ll d=dfs(e.to,t,min(f,e.cap));
                if(d>0){
                    e.cap-=d;
                    v[e.to][e.rev].cap+=d;
                    return d;
                }
            }
        }
        return 0;
    }

    ll max_flow(int s,int t){
        ll flow=0;
        while(1){
            bfs(s);
            if(dist[t]<0)return flow;
            fill(iter.begin(),iter.end(),0);
            ll f;
            while((f=dfs(s,t,1LL<<62))>0)flow+=f;
        }
    }
};
int main(){
    int n,k;
    cin>>n>>k;
    int ans = (n+k-1)/k;
    while(1){
    Dinic dn(2*ans+3);
    rep(i,ans){
        dn.addedge(2*ans,i,k);
        dn.addedge(ans+i,2*ans+1,k);
    }
    rep(i,ans)rep(j,ans){
        dn.addedge(i,j+ans,1);
    }
    dn.addedge(2*ans+2,2*ans,n);
    int ret = dn.max_flow(2*ans+2, 2*ans+1);
    if(ret<n){
        ++ans;
        continue;
    }
    cout<<ans<<endl;
    vector<vector<int>> res(ans,vector<int>(ans,0));
    rep(i,ans){
        for(auto to : dn.v[i]){
            if(ans<=to.to&&to.to<2*ans&&to.cap==0){
                res[i][to.to-ans]=1;
            }
        }
    }
    rep(i,ans){
        rep(j,ans)cout<<(res[i][j]?'#':'.');
        cout<<endl;
    }
    break;
    }
    return 0;
}
0