Study/PS(Algorithm)
[C++] HackerRank 해커랭크 Roads and Libraries
노랑펭귄
2018. 4. 16. 13:58
문제링크 : https://www.hackerrank.com/challenges/torque-and-development
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 | #include <bits/stdc++.h> using namespace std; vector<int> p; int f(int a) { return p[a] == a ? a : p[a] = f(p[a]); } void u(int a, int b) { p[f(a)] = f(b); } int main() { int T; cin >> T; while (T--) { int N, M, a, b; long long c, d; cin >> N >> M >> c >> d; p.clear(); p.resize(N); iota(p.begin(), p.end(), 0); while (M--) { cin >> a >> b; --a, --b; u(a, b); } int comp = 0; for (int i = 0; i<N; ++i) { if (p[i] == i)++comp; } cout << (comp*c + (N - comp)*min(c, d)) << "\n"; } return 0; } | cs |