結果
問題 | No.317 辺の追加 |
ユーザー | kei |
提出日時 | 2018-10-08 23:55:21 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,359 ms / 2,000 ms |
コード長 | 4,226 bytes |
コンパイル時間 | 2,120 ms |
コンパイル使用メモリ | 184,292 KB |
実行使用メモリ | 19,736 KB |
最終ジャッジ日時 | 2024-04-20 18:59:04 |
合計ジャッジ時間 | 14,792 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 195 ms
14,080 KB |
testcase_03 | AC | 172 ms
14,336 KB |
testcase_04 | AC | 381 ms
13,720 KB |
testcase_05 | AC | 146 ms
12,672 KB |
testcase_06 | AC | 204 ms
15,824 KB |
testcase_07 | AC | 56 ms
7,296 KB |
testcase_08 | AC | 803 ms
14,300 KB |
testcase_09 | AC | 262 ms
16,876 KB |
testcase_10 | AC | 321 ms
19,532 KB |
testcase_11 | AC | 941 ms
11,412 KB |
testcase_12 | AC | 301 ms
19,672 KB |
testcase_13 | AC | 1,359 ms
12,628 KB |
testcase_14 | AC | 251 ms
17,948 KB |
testcase_15 | AC | 282 ms
19,044 KB |
testcase_16 | AC | 373 ms
15,436 KB |
testcase_17 | AC | 262 ms
18,252 KB |
testcase_18 | AC | 285 ms
19,504 KB |
testcase_19 | AC | 290 ms
19,736 KB |
testcase_20 | AC | 504 ms
14,924 KB |
testcase_21 | AC | 89 ms
10,496 KB |
testcase_22 | AC | 48 ms
7,296 KB |
testcase_23 | AC | 50 ms
7,296 KB |
testcase_24 | AC | 157 ms
15,940 KB |
testcase_25 | AC | 113 ms
12,928 KB |
testcase_26 | AC | 117 ms
13,044 KB |
testcase_27 | AC | 90 ms
11,136 KB |
testcase_28 | AC | 47 ms
7,296 KB |
testcase_29 | AC | 13 ms
5,376 KB |
testcase_30 | AC | 209 ms
16,384 KB |
testcase_31 | AC | 206 ms
16,312 KB |
testcase_32 | AC | 204 ms
16,312 KB |
testcase_33 | AC | 210 ms
16,436 KB |
testcase_34 | AC | 209 ms
16,436 KB |
testcase_35 | AC | 208 ms
16,312 KB |
testcase_36 | AC | 207 ms
16,436 KB |
testcase_37 | AC | 207 ms
16,432 KB |
testcase_38 | AC | 207 ms
16,308 KB |
testcase_39 | AC | 210 ms
16,312 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; }