結果

問題 No.1669 パズル作成
ユーザー chocoruskchocorusk
提出日時 2021-09-04 16:42:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,470 bytes
コンパイル時間 3,520 ms
コンパイル使用メモリ 192,120 KB
実行使用メモリ 108,216 KB
最終ジャッジ日時 2023-08-23 05:35:11
合計ジャッジ時間 7,398 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,600 KB
testcase_01 AC 2 ms
5,552 KB
testcase_02 AC 2 ms
7,616 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 3 ms
9,840 KB
testcase_06 AC 38 ms
9,116 KB
testcase_07 AC 85 ms
105,404 KB
testcase_08 AC 58 ms
11,248 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 58 ms
11,312 KB
testcase_13 AC 58 ms
11,236 KB
testcase_14 AC 58 ms
11,544 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 22 ms
30,156 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 22 ms
28,216 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 25 ms
34,208 KB
testcase_30 AC 31 ms
10,576 KB
testcase_31 AC 31 ms
10,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
struct unionfind{
    vector<int> par, sz;
    unionfind() {}
    unionfind(int n):par(n), sz(n, 1){
        for(int i=0; i<n; i++) par[i]=i;
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }
    void unite(int x, int y){
        x=find(x); y=find(y);
        if(x==y) return;
        if(sz[x]>sz[y]) swap(x, y);
        par[x]=y;
        sz[y]+=sz[x];
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};
int dp[5050][5050];
bitset<5000> bs[5050], bs1[5050];

int main()
{
    int n, m;
    cin>>n>>m;
    int r[100010], c[100010];
    vector<int> vr[5050];
    for(int i=0; i<m; i++){
        cin>>r[i]>>c[i];
        r[i]--; c[i]--;
        vr[c[i]].push_back(r[i]);
        bs[r[i]][c[i]]=1;
    }
    unionfind uf(n);
    for(int i=0; i<n; i++){
        for(int j=0; j<(int)vr[i].size()-1; j++){
            uf.unite(vr[i][j], vr[i][j+1]);
        }
    }
    for(int i=0; i<n; i++){
        int x=uf.find(i);
        bs1[x]|=bs[i];
    }
    vector<P> v;
    v.push_back({uf.size(0), bs1[uf.find(0)].count()});
    for(int i=1; i<n; i++){
        if(uf.find(i)!=i || uf.same(0, i)) continue;
        v.push_back({uf.size(i), bs1[i].count()});
    }
    int n1=v.size();
    for(int i=0; i<=n1; i++) for(int j=0; j<=n; j++) dp[i][j]=-1;
    dp[0][v[0].first]=v[0].second;
    for(int i=1; i<n1; i++){
        for(int j=0; j<=n; j++){
            if(dp[i-1][j]==-1) continue;
            dp[i][j]=max(dp[i][j], dp[i-1][j]);
            if(j+v[i].first<=n) dp[i][j+v[i].first]=max(dp[i][j+v[i].first], dp[i-1][j]+v[i].second);
        }
    }
    int ans=n*n;
    for(int j=0; j<=n; j++){
        int x=dp[n1-1][j];
        if(x==-1) continue;
        ans=min(ans, x*j+(n-x)*(n-j));
    }
    ans-=m;
    cout<<ans<<endl;
    return 0;
}
0