Submission #978423

#TimeUsernameProblemLanguageResultExecution timeMemory
978423steveonalexToll (APIO13_toll)C++17
78 / 100
2540 ms35096 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define ALL(v) (v).begin(), (v).end() #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng(1); ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;} ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll LASTBIT(ll mask){return mask & (-mask);} ll pop_cnt(ll mask){return __builtin_popcountll(mask);} ll ctz(ll mask){return __builtin_ctzll(mask);} ll clz(ll mask){return __builtin_clzll(mask);} ll logOf(ll mask){return 63 - clz(mask);} template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b){a = b; return true;} return false; } template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b){a = b; return true;} return false; } template <class T> void printArr(T a, string separator = " ", string finish = "\n", ostream& out = cout){ for(auto i: a) out << i << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } #define int long long struct DSU{ int n; vector<int> parent, sz; DSU(int _n){ n = _n; parent.resize(n+1); sz.resize(n+1, 1); for(int i = 1; i<=n; ++i) parent[i] = i; } int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));} bool same_set(int u, int v){return find_set(u) == find_set(v);} bool join_set(int u, int v){ u = find_set(u), v = find_set(v); if (u != v){ if (sz[u] < sz[v]) swap(u, v); parent[v] = u; sz[u] += sz[v]; return true; } return false; } }; const int N = 1e5 + 69, LOG_N = 10; int n, m, k; vector<array<int, 3>> path, useful; vector<pair<int, int>> money; vector<pair<int, int>> graph[N]; ll goon[N]; namespace Nig{ vector<pair<int, int>> graph[N]; ll chode[N]; vector<array<int, 3>> ans; void reset(){ for(int i = 1; i<=n; ++i){ graph[i].clear(); } } void dfs(int u, int p){ chode[u] = goon[u]; for(pair<int, int> v: graph[u]) if (v.first != p){ dfs(v.first, u); chode[u] += chode[v.first]; if (v.second) ans.push_back({u, v.first, chode[v.first]}); } } vector<array<int, 3>> go(int root, DSU &mst){ ans.clear(); for(array<int, 3> i: useful){ int u = i[0], v = i[1]; if (mst.join_set(u, v)){ graph[u].push_back({v, 0}); graph[v].push_back({u, 0}); } } dfs(root, root); return ans; } } signed main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define int long long cin >> n >> m >> k; path.resize(m); for(int i = 0; i<m; ++i){ for(int j = 0;j < 3; ++j) cin >> path[i][j]; } sort(ALL(path), [] (array<int, 3> x, array<int, 3> y){return x[2] < y[2];}); DSU mst(n); for(int i = 0; i<m; ++i){ int u = path[i][0], v = path[i][1], w = path[i][2]; if (mst.join_set(u, v)){ useful.push_back(path[i]); graph[u].push_back({v, w}); graph[v].push_back({u, w}); } } money.resize(k); for(int i = 0; i<k; ++i) cin >> money[i].first >> money[i].second; for(int i = 1; i<=n; ++i) cin >> goon[i]; mst = DSU(n); vector<array<int, 3>> annoying, chill; for(pair<int, int> i: money) mst.join_set(i.first, i.second); for(array<int, 3> i: useful){ if (mst.join_set(i[0], i[1])) { chill.push_back(i); } else annoying.push_back(i); } mst = DSU(n); for(array<int, 3> i: chill) { mst.join_set(i[0], i[1]); } vector<int> S; S.push_back(0); for(int i= 1; i<=n; ++i) if (i == mst.find_set(i)) S.push_back(i); for(pair<int, int> &i: money){ i.first = lower_bound(ALL(S), mst.find_set(i.first)) - S.begin(); i.second = lower_bound(ALL(S), mst.find_set(i.second)) - S.begin(); } for(array<int, 3> &i: annoying){ i[0] = lower_bound(ALL(S), mst.find_set(i[0])) - S.begin(); i[1] = lower_bound(ALL(S), mst.find_set(i[1])) - S.begin(); } useful = annoying; vector<ll> sum(S.size()); for(int i= 1; i<=n; ++i){ int idx = lower_bound(ALL(S), mst.find_set(i)) - S.begin(); sum[idx] += goon[i]; } memset(goon, 0, sizeof goon); for(int i= 1; i<S.size(); ++i) goon[i] = sum[i]; n = S.size() - 1; // cout << n << "\n"; // for(int i = 1; i<=n; ++i) cout << goon[i] << " "; cout << "\n\n"; // for(pair<int, int> i: money) cout << i.first << " " << i.second << "\n"; // for(array<int, 3> i: useful) printArr(i); int root = lower_bound(ALL(S), mst.find_set(1)) - S.begin(); ll ans = 0; for(int mask = 1; mask < MASK(k); ++mask){ DSU mst(n); bool check = true; for(int i = 0; i<k; ++i) if (GETBIT(mask, i)){ check = check && mst.join_set(money[i].first, money[i].second); } if (check == false) continue; Nig::reset(); // cerr << string(50, '-') << endl; for(int i= 0; i<k; ++i) if (GETBIT(mask, i)) { int u = money[i].first, v = money[i].second; Nig::graph[u].push_back({v, 1}); Nig::graph[v].push_back({u, 1}); } vector<array<int, 3>> edging = Nig::go(root, mst); sort(ALL(edging), [](array<int, 3> x, array<int, 3> y){return x[2] > y[2];}); vector<int> toll(edging.size(), -1); for(int i = 0; i < edging.size(); ++i){ vector<array<int, 3>> sex = useful; for(int j = 0; j<i; ++j){ int u = edging[j][0], v = edging[j][1], w = toll[j]; sex.push_back({{-u, -v, w}}); } sort(ALL(sex), [](array<int, 3> x, array<int, 3> y){ if (x[2] != y[2]) return x[2] < y[2]; return x[1] < y[1]; }); DSU mst(n); for(int j = i+1; j<edging.size(); ++j){ int u = edging[j][0], v = edging[j][1]; mst.join_set(u, v); } int u = edging[i][0], v = edging[i][1]; for(array<int, 3> item: sex){ if (mst.join_set(abs(item[0]), abs(item[1]))){ if (mst.same_set(u, v) && toll[i] == -1){ toll[i] = item[2]; } } } } ll cur = 0; for(int i = 0; i<edging.size(); ++i) cur += 1LL * toll[i] * edging[i][2]; // for(int i = 0; i<edging.size(); ++i) cerr << toll[i] << " " << edging[i][2] << "\n"; // cerr << cur << endl; maximize(ans, cur); } cout << ans << "\n"; return 0; }

Compilation message (stderr)

toll.cpp: In function 'int main()':
toll.cpp:172:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  172 |     for(int i= 1; i<S.size(); ++i) goon[i] = sum[i];
      |                   ~^~~~~~~~~
toll.cpp:206:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  206 |         for(int i = 0; i < edging.size(); ++i){
      |                        ~~^~~~~~~~~~~~~~~
toll.cpp:218:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  218 |             for(int j = i+1; j<edging.size(); ++j){
      |                              ~^~~~~~~~~~~~~~
toll.cpp:233:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  233 |         for(int i = 0; i<edging.size(); ++i) cur += 1LL * toll[i] * edging[i][2];
      |                        ~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...