Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* https://judge.hlhs.hlc.edu.tw/ShowProblem?problemid=c040 */
- // #pragma GCC target("avx2")
- #pragma GCC optimize("O3", "unroll-loops")
- // #include <bits/extc++.h>
- // using namespace __gnu_pbds;
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- #define double long double
- // template <typename T>
- // using pbds_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
- using pii = pair<int, int>;
- template<typename T>
- using prior = priority_queue<T, vector<T>, greater<T>>;
- template<typename T>
- using Prior = priority_queue<T>;
- #define X first
- #define Y second
- #define ALL(x) (x).begin(), (x).end()
- #define eb emplace_back
- #define pb push_back
- #define fastIO() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
- #define RANDOM() random_device __rd; \
- mt19937 __gen = mt19937(__rd()); \
- uniform_int_distribution<int> __dis(1, 1E8); \
- auto rnd = bind(__dis, __gen)
- const int INF = 1E18;
- const int mod = 1E9 + 7;
- const int maxn = 2E6 + 5;
- int scc_id = 0;
- vector<int32_t> adj[maxn];
- int32_t scc[maxn], sz[maxn];
- int inv[maxn], minVal[maxn];
- map<int, int> disc;
- stack<int32_t> stk;
- set<int> st;
- vector<pii> v;
- void dfs(int now) {
- minVal[scc_id] = inv[now];
- stk.push(now);
- while (!stk.empty()) {
- now = stk.top(), stk.pop();
- if (scc[now]) continue;
- scc[now] = scc_id;
- ++sz[scc_id], minVal[scc_id] = min(minVal[scc_id], inv[now]);
- for (auto x : adj[now]) {
- if (!scc[x]) stk.push(x);
- }
- }
- }
- int32_t main() {
- // freopen("s06_01.in", "r", stdin);
- fastIO();
- int n, tok = 0, tot = 0;
- cin >> n, v.resize(n);
- for (auto &x : v) {
- cin >> x.X >> x.Y;
- if (!st.count(x.X)) tot += x.X, st.insert(x.X), disc[x.X] = ++tok, inv[tok] = x.X;
- if (!st.count(x.Y)) tot += x.Y, st.insert(x.Y), disc[x.Y] = ++tok, inv[tok] = x.Y;
- x.X = disc[x.X], x.Y = disc[x.Y];
- adj[x.X].eb(x.Y), adj[x.Y].eb(x.X);
- }
- for (int i = 1; i <= tok; ++i) {
- if (!scc[i]) ++scc_id, dfs(i);
- }
- for (auto x : v) --sz[scc[x.X]];
- for (int i = 1; i <= scc_id; ++i) {
- if (sz[i] == 1) tot -= minVal[i]; /// N nodes, N-1 edges ///
- }
- cout << tot << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement