C++の練習を兼ねて, AtCoder Regular Contest 037 の 問題A (A – 全優) ~ 問題B (B – バウムテスト) を解いてみた.
■感想.
1. 問題B は, 木の個数を数える内容で, 個人的には, 面白いと感じた.
2. 幅優先探索の復習が出来たので, 非常に良かったと思う.
3. 時間を見つけて, 引き続き, 過去問を振り返っていきたいと思う.
本家のサイトARC 037 解説をご覧下さい.
■C++版プログラム(問題A/AC版).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <bits/stdc++.h> using namespace std; #define repex(i, a, b, c) for(int i = a; i < b; i += c) #define repx(i, a, b) repex(i, a, b, 1) #define rep(i, n) repx(i, 0, n) #define repr(i, a, b) for(int i = a; i >= b; i--) int main(){ // 1. 入力情報. int N; scanf("%d", &N); // 2. 最短の合計勉強時間. int ans = 0, m; rep(i, N){ scanf("%d", &m); if(m < 80) ans += (80 - m); } // 3. 出力. printf("%d\n", ans); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[入力例] 5 70 90 60 80 50 [出力例] 60 ※AtCoderのテストケースより [入力例] 6 100 100 100 100 100 100 [出力例] 0 ※AtCoderのテストケースより |
■C++版プログラム(問題B/AC版).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
// C++14(GCC 5.4.1) #include <bits/stdc++.h> using namespace std; using vvi = vector<vector<int>>; #define repex(i, a, b, c) for(int i = a; i < b; i += c) #define repx(i, a, b) repex(i, a, b, 1) #define rep(i, n) repx(i, 0, n) #define repr(i, a, b) for(int i = a; i >= b; i--) #define pb push_back const int MAX = 111; int connection[MAX]; // 連結成分番号. int v[MAX]; // 頂点数. int e[MAX]; // 辺数. vvi G(MAX); // グラフ. // グラフを幅優先探索する. // https://ja.wikipedia.org/wiki/幅優先探索 // ※bfsの動作確認用. // @param G: グラフ. // @param s: グラフの探索開始頂点. // @param l: 連結成分番号(※1以上). // @return: 特に無し. void bfs(vvi &G, int s, int l){ // 1. 空のキュー. queue<int> q; // 2. 訪問済みフラグ設定. connection[s] = l; // 3. 探索地点 s をキュー q に追加. q.push(s); while(!q.empty()){ // 4. キューから取り出す. int u = q.front(); q.pop(); // 5. 取り出した要素を処理. for(auto &e : G[u]){ // 6. 訪問済であれば, 処理をスキップ. if(connection[e] > 0) continue; if(connection[e] == 0 && e != s) connection[e] = l, q.push(e); } } return; } int main(){ // 1. 入力情報. int N, M; scanf("%d %d", &N, &M); // 2. バウムテストする. vvi G(N); rep(i, M){ int u, v; scanf("%d %d", &u, &v); u--, v--; G[u].pb(v); G[v].pb(u); } // 3. 各頂点に対する連結成分番号を設定. int id = 0; rep(i, N) if(connection[i] == 0) bfs(G, i, ++id); // rep(i, N) printf("i=%d con=%d\n", i, connection[i]); // 4. 各連結成分の頂点数を保存. rep(i, N) v[connection[i]]++; // 5. 各連結成分の辺数を保存. rep(i, N) for(auto &p : G[i]) e[connection[p]]++; // 6. 木を抽出. int ans = 0; repx(i, 1, N + 1) if(v[i] == e[i] / 2 + 1) ans++; // 7. 出力. printf("%d\n", ans); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
[入力例] 8 7 1 2 2 3 2 4 5 6 6 7 6 8 7 8 [出力例] 1 ※AtCoderのテストケースより [入力例] 5 1 1 2 [出力例] 4 ※AtCoderのテストケースより [入力例] 11 11 1 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 11 [出力例] 0 ※AtCoderのテストケースより [入力例] 16 11 1 2 1 3 4 5 6 7 6 8 6 9 10 11 10 12 12 13 14 15 15 16 [出力例] 5 |
■参照サイト
AtCoder Regular Contest 037