結果

問題 No.317 辺の追加
ユーザー fumofumofunifumofumofuni
提出日時 2021-09-16 01:18:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 970 ms / 2,000 ms
コード長 3,162 bytes
コンパイル時間 2,676 ms
コンパイル使用メモリ 221,784 KB
実行使用メモリ 10,036 KB
最終ジャッジ日時 2023-09-11 12:36:27
合計ジャッジ時間 20,619 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 175 ms
8,120 KB
testcase_03 AC 171 ms
7,448 KB
testcase_04 AC 208 ms
7,176 KB
testcase_05 AC 145 ms
6,084 KB
testcase_06 AC 193 ms
7,708 KB
testcase_07 AC 61 ms
4,628 KB
testcase_08 AC 330 ms
7,000 KB
testcase_09 AC 223 ms
9,436 KB
testcase_10 AC 258 ms
9,968 KB
testcase_11 AC 157 ms
7,124 KB
testcase_12 AC 260 ms
9,924 KB
testcase_13 AC 172 ms
7,144 KB
testcase_14 AC 235 ms
9,664 KB
testcase_15 AC 250 ms
9,936 KB
testcase_16 AC 227 ms
8,344 KB
testcase_17 AC 240 ms
9,656 KB
testcase_18 AC 252 ms
9,892 KB
testcase_19 AC 252 ms
10,036 KB
testcase_20 AC 239 ms
7,456 KB
testcase_21 AC 117 ms
5,200 KB
testcase_22 AC 62 ms
4,456 KB
testcase_23 AC 64 ms
4,384 KB
testcase_24 AC 190 ms
6,780 KB
testcase_25 AC 141 ms
6,872 KB
testcase_26 AC 143 ms
6,708 KB
testcase_27 AC 116 ms
5,948 KB
testcase_28 AC 67 ms
4,420 KB
testcase_29 AC 16 ms
4,380 KB
testcase_30 AC 816 ms
6,292 KB
testcase_31 AC 946 ms
6,352 KB
testcase_32 AC 945 ms
6,212 KB
testcase_33 AC 961 ms
6,412 KB
testcase_34 AC 937 ms
6,412 KB
testcase_35 AC 963 ms
6,480 KB
testcase_36 AC 959 ms
6,360 KB
testcase_37 AC 970 ms
6,272 KB
testcase_38 AC 827 ms
6,196 KB
testcase_39 AC 943 ms
6,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
} 

struct UnionFind {
    vector<int> par;
    vector<int> edge;
    
    UnionFind(int n) : par(n, -1),edge(n, 0) {}

    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) {
            edge[x]++;
            return false;
        }
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        edge[x] += edge[y]+1;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
};
template< typename T >
vector< T > slide_min(const vector< T > &v, int k)
{
  deque< int > deq;
  vector< T > ret;
  for(int i = 0; i < v.size(); i++) {
    while(!deq.empty() && v[deq.back()] >= v[i]) {
      deq.pop_back();
    }
    deq.push_back(i);
    if(i - k + 1 >= 0) {
      ret.emplace_back(v[deq.front()]);
      if(deq.front() == i - k + 1) deq.pop_front();
    }
  }
  return ret;
}

int main(){
    ll n,m;cin >> n >> m;
    UnionFind uf(n);
    rep(i,m){
        ll a,b;cin >> a >> b;
        a--;b--;uf.merge(a,b);
    }
    vl v;
    rep(i,n){
        if(uf.root(i)!=i)continue;
        v.push_back(uf.size(i));
    }
    sort(all(v));
    map<ll,ll> mp;
    for(auto p:v){
        mp[p]++;
    }
    vl dp(n+1,INF);dp[0]=0;
    for(auto [val,k]:mp){
        //cout << val <<" " << k << endl;
        vvl ndp(val);
        rep(i,n+1){
            ndp[i%val].emplace_back(dp[i]-i/val);
        }
        rep(i,val){
            deque<ll> deq;
            rep(j,ndp[i].size()){
                while(!deq.empty()&&ndp[i][deq.back()]>=ndp[i][j]){
                    deq.pop_back();
                }
                deq.push_back(j);
                dp[j*val+i]=ndp[i][deq.front()]+j;
                if(deq.front()<=j-k)deq.pop_front();
            }
        }
        //rep(i,n+1)cout << dp[i] <<" ";cout << endl;
    }
    rep(i,n){
        if(dp[i+1]==INF)cout << -1 << endl;
        else cout << dp[i+1]-1 << endl;
    }
}   
0