class, structの勉強メモ
概要
流石に自作の構造体が無いと辛すぎるのでまとめた。お前UF木ライブラリで持ってるの何?
結局よくわからなかったので要復習!!
classとstructの違い
デフォルトのメンバのアクセス修飾子が違うだけっぽい?
- class: private
- struct: public
競プロでは便利なのでstructだけ使っていけば良さそう
書き方例
俺はBFSでこの問題を解くんだという強い意志を持つと以下の解法が生えます。
dequeの操作が辛いんだけど書き方違う?
atcoder.jp Submission #5806534 - M-SOLUTIONS Programming Contest
struct Node { int num; set<Node> neighbor; Node(int num) : num(num), neighbor(){} bool operator<(const Node &right) const {return this->num < right.num;} }; signed main() { ios::sync_with_stdio(false); ll n; cin >> n; vector<Node> nodes; REP(i, n+1) { nodes.push_back(Node(i)); // nodes.emplace_back(i);こっちでもよい } REP(i, n-1) { int a, b; cin >> a >> b; nodes[a].neighbor.insert(b); nodes[b].neighbor.insert(a); } vector<ll> c(n+1); REP(i, n) cin >> c[i]; sort(all(c), greater<ll>()); deque<Node> q; q.push_back(nodes[1]); vector<int> ans(n+1); ans[1] = c[0]; int cnt = 1; while(!q.empty()) { Node a = q.front(); q.pop_front(); for(Node next: a.neighbor) { if(ans[next.num] != 0) { continue; } ans[next.num] = c[cnt]; cnt++; // q.push_back(next);って書きたいけどダメらしい。変な参照の仕方しているのか? q.push_back(nodes[next.num]); } } cout << accumulate(all(c), 0L) - c[0] << endl; REP_FROM(i, 1, n+1) cout << ans[i] << " "; cout << endl; return 0; }