結果
問題 | No.317 辺の追加 |
ユーザー | kei |
提出日時 | 2018-10-08 23:55:21 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,314 ms / 2,000 ms |
コード長 | 4,226 bytes |
コンパイル時間 | 2,199 ms |
コンパイル使用メモリ | 190,076 KB |
実行使用メモリ | 19,712 KB |
最終ジャッジ日時 | 2024-10-12 16:08:19 |
合計ジャッジ時間 | 14,348 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 183 ms
14,136 KB |
testcase_03 | AC | 170 ms
14,464 KB |
testcase_04 | AC | 351 ms
13,652 KB |
testcase_05 | AC | 135 ms
12,800 KB |
testcase_06 | AC | 209 ms
15,904 KB |
testcase_07 | AC | 58 ms
7,296 KB |
testcase_08 | AC | 781 ms
14,300 KB |
testcase_09 | AC | 252 ms
16,940 KB |
testcase_10 | AC | 284 ms
19,512 KB |
testcase_11 | AC | 909 ms
11,340 KB |
testcase_12 | AC | 299 ms
19,520 KB |
testcase_13 | AC | 1,314 ms
12,504 KB |
testcase_14 | AC | 262 ms
17,900 KB |
testcase_15 | AC | 276 ms
19,008 KB |
testcase_16 | AC | 361 ms
15,464 KB |
testcase_17 | AC | 261 ms
18,340 KB |
testcase_18 | AC | 288 ms
19,592 KB |
testcase_19 | AC | 309 ms
19,712 KB |
testcase_20 | AC | 482 ms
14,816 KB |
testcase_21 | AC | 86 ms
10,496 KB |
testcase_22 | AC | 47 ms
7,168 KB |
testcase_23 | AC | 47 ms
7,296 KB |
testcase_24 | AC | 153 ms
16,012 KB |
testcase_25 | AC | 104 ms
12,928 KB |
testcase_26 | AC | 116 ms
13,244 KB |
testcase_27 | AC | 91 ms
11,136 KB |
testcase_28 | AC | 47 ms
7,296 KB |
testcase_29 | AC | 13 ms
5,248 KB |
testcase_30 | AC | 214 ms
16,312 KB |
testcase_31 | AC | 223 ms
16,312 KB |
testcase_32 | AC | 219 ms
16,312 KB |
testcase_33 | AC | 220 ms
16,316 KB |
testcase_34 | AC | 220 ms
16,316 KB |
testcase_35 | AC | 223 ms
16,312 KB |
testcase_36 | AC | 221 ms
16,316 KB |
testcase_37 | AC | 220 ms
16,312 KB |
testcase_38 | AC | 216 ms
16,316 KB |
testcase_39 | AC | 226 ms
16,188 KB |
ソースコード
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* <url:https://yukicoder.me/problems/no/317> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ class SCC { public: typedef int TYPE; TYPE V; vector<vector<TYPE>> G; vector<vector<TYPE>> rG; vector<vector<TYPE>> group_G; vector<vector<TYPE>> group_rG; vector<TYPE> group; vector<TYPE> List; vector<int> visited; int group_num; SCC(TYPE V) :V(V),group_num(0) { G.resize(V); rG.resize(V); } void init(TYPE V){ G.clear(); G.resize(V); rG.clear(); rG.resize(V); } void add_edge(TYPE u, TYPE v) { G[u].emplace_back(v); rG[v].emplace_back(u); } void dfs1(TYPE u) { visited[u] = 1; for (TYPE& next : G[u]){ if (visited[next] != 1) dfs1(next); } List.emplace_back(u); } void dfs2(TYPE u,TYPE group_num) { visited[u] = 1; group[u] = group_num; for (TYPE& next : rG[u]){ if (visited[next] != 1) dfs2(next, group_num); } } void make_group() { group.clear(); group.assign(V, -1); visited.clear(); visited.assign(V, 0); for (int i = 0; i < V;i++){ if (visited[i] != 1) dfs1(i); } reverse(List.begin(), List.end()); visited.clear(); visited.assign(V, 0); group_num = 0; for (TYPE& v : List) { if (visited[v] != 1) dfs2(v, group_num++); } } void make_group_graph(){ group_G.clear(); group_G.resize(group_num); group_rG.clear(); group_rG.resize(group_num); for(int u = 0; u < V;u++){ for(TYPE& v: G[u]){ if(!same(u,v)){ group_G[group[u]].emplace_back(group[v]); group_rG[group[v]].emplace_back(group[u]); } } } } TYPE get_group(TYPE n){ return group[n]; } bool same(TYPE u,TYPE v) { return group[u] == group[v]; } vector<vector<TYPE>>& get_G() { return G; } vector<vector<TYPE>>& get_rG() { return rG; } vector<vector<TYPE>>& get_group_G() { return group_G; } vector<vector<TYPE>>& get_group_rG() { return group_rG; } }; void solve(){ int N,M; cin >> N >> M; SCC scc(N); for(int i = 0; i < M;i++){ int u,v; cin >> u >> v; u--; v--; scc.add_edge(u, v); scc.add_edge(v, u); } scc.make_group(); vector<int> cc(scc.group_num); for(auto g:scc.group) cc[g]++; map<ll,ll> mp; for(auto c:cc){ mp[c]++; } vector<pll> m; for(auto p:mp) m.push_back(p); vector<ll> dp(N+1,LINF); dp[0] = 0; for(const pll& p:m){ for(int i = N; i >= 0;i--){ if(dp[i]==LINF) continue; for(int j = 1; j <= p.second;j++){ dp[i+p.first*j] = min(dp[i+p.first*j],dp[i]+j); } } } for(int i = 1; i <= N;i++){ if(dp[i]==LINF) cout << -1 << endl; else cout << dp[i]-1 << endl; } } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); solve(); return 0; }