# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
401680 |
2021-05-10T17:00:30 Z |
Jorge213 |
Toll (APIO13_toll) |
C++14 |
|
1 ms |
204 KB |
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int u, v;
long long w;
bool operator<(const Edge &e) const {
return w < e.w;
}
};
struct DSU {
vector<int> dsu;
DSU(int n): dsu(n) {
for (int i = 1; i < n; i++)
dsu[i] = i;
}
int root(int idx) {
while (idx != dsu[idx]) {
dsu[idx] = dsu[dsu[idx]];
idx = dsu[idx];
}
return idx;
}
bool join(int a, int b) {
if (root(a) == root(b))
return false;
dsu[root(b)] = root(a);
return true;
}
};
struct Tree {
vector<vector<pair<int, long long>>> adjList;
vector<long long> we, cnt;
vector<int> d, par;
Tree(int sz, vector<long long> &a):
adjList(sz), we(sz), d(sz, 0), par(sz), cnt(a) {};
void addEdge(int u, int v, long long w) {
adjList[u].push_back({v, w});
adjList[v].push_back({u, w});
}
void dfs(int v, int p) {
par[v] = p;
d[v] = d[p] + 1;
for (auto [u, w] : adjList[v]) {
if (u == p) continue;
dfs(u, v);
we[u] = w;
cnt[v] += cnt[u];
}
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<Edge> edges, nedges;
vector<long long> a(n + 1);
int ai, bi;
long long ci;
for (int i = 0; i < m; i++) {
cin >> ai >> bi >> ci;
edges.push_back({ai, bi, ci});
}
sort(edges.begin(), edges.end());
int xi, yi;
for (int i = 0; i < k; i++) {
cin >> xi >> yi;
nedges.push_back({xi, yi, 0});
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
DSU dsu(n + 1), ndsu(n + 1);
for (auto [u, v, w] : nedges) {
ndsu.join(u, v);
}
for (auto [u, v, w] : edges) {
u = dsu.root(u), v = dsu.root(v);
if (ndsu.root(u) != ndsu.root(v))
dsu.join(u, v);
}
int sz = 0;
vector<int> comp(n + 1, 0);
vector<long long> p = {0};
for (int i = 1; i <= n; i++) {
int v = dsu.root(i);
if (!comp[v]) {
comp[v] = ++sz;
p.push_back(0);
}
comp[i] = comp[v];
p[comp[i]] += a[i];
}
for (auto &[u, v, w] : nedges) {
u = comp[u], v = comp[v];
}
DSU dsu2(sz + 1);
vector<Edge> mn;
for (auto [u, v, w] : edges) {
if (dsu2.join(comp[u], comp[v])) {
mn.push_back({comp[u], comp[v], w});
}
}
long long ans = 0;
for (int bm = 1; bm < (1 << k); bm++) {
Tree g(sz + 1, p);
DSU st(sz + 1);
int ecnt = 0;
for (int i = 0; i < k; i++) {
if (!(bm & (1 << i))) continue;
auto [u, v, w] = nedges[i];
if (st.join(u, v)) {
g.addEdge(u, v, LLONG_MAX);
ecnt++;
}
}
int idx = 0;
for (; ecnt < sz - 1; idx++) {
auto [u, v, w] = mn[idx];
if (st.join(u, v)) {
g.addEdge(u, v, w);
ecnt++;
}
}
g.dfs(1, -1);
for (; idx < mn.size(); idx++) {
auto [u, v, w] = mn[idx];
if (g.d[u] < g.d[v])
swap(u, v);
while (g.d[u] > g.d[v]) {
g.we[u] = min(g.we[u], w);
u = g.par[u];
}
while (u != v) {
g.we[u] = min(g.we[u], w);
g.we[v] = min(g.we[v], w);
u = g.par[u], v = g.par[v];
}
}
long long can = 0;
for (int v = 1; v <= sz; v++) {
can += g.we[v] * g.cnt[v];
}
ans = max(ans, can);
}
cout << ans << '\n';
return 0;
}
Compilation message
toll.cpp: In constructor 'Tree::Tree(int, std::vector<long long int>&)':
toll.cpp:39:17: warning: 'Tree::par' will be initialized after [-Wreorder]
39 | vector<int> d, par;
| ^~~
toll.cpp:38:24: warning: 'std::vector<long long int> Tree::cnt' [-Wreorder]
38 | vector<long long> we, cnt;
| ^~~
toll.cpp:41:2: warning: when initialized here [-Wreorder]
41 | Tree(int sz, vector<long long> &a):
| ^~~~
toll.cpp: In member function 'void Tree::dfs(int, int)':
toll.cpp:52:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for (auto [u, w] : adjList[v]) {
| ^
toll.cpp: In function 'int main()':
toll.cpp:92:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
92 | for (auto [u, v, w] : nedges) {
| ^
toll.cpp:96:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
96 | for (auto [u, v, w] : edges) {
| ^
toll.cpp:116:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
116 | for (auto &[u, v, w] : nedges) {
| ^
toll.cpp:122:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
122 | for (auto [u, v, w] : edges) {
| ^
toll.cpp:136:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
136 | auto [u, v, w] = nedges[i];
| ^
toll.cpp:145:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
145 | auto [u, v, w] = mn[idx];
| ^
toll.cpp:153:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
153 | for (; idx < mn.size(); idx++) {
| ~~~~^~~~~~~~~~~
toll.cpp:154:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
154 | auto [u, v, w] = mn[idx];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |