#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
return l + rd() % (r - l + 1);
}
const int N = 1e5 + 5;
const int M = 3e5 + 5;
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);
vector <int> adj[N];
vector <int> ind;
long long tot[N];
long long val[N];
int fr[M];
int to[M];
int w[M];
int par[N];
int mx[N];
int a[N];
int b[N];
int n,k,m;
int getpar(int u) {
return (par[u] < 0) ? u : (par[u] = getpar(par[u]));
}
bool join(int u, int v) {
u = getpar(u);
v = getpar(v);
if (u == v)
return false;
if (par[u] > par[v])
swap(u, v);
par[u] += par[v];
par[v] = u;
tot[u] += tot[v];
return true;
}
long long dfs(int u, int d = 0, int p = -1) {
// cerr << val[u] << " " << u << " " << d << " " << p << "\n";
long long res = val[u] * d;
for (int i : adj[u]) {
int v = a[i] ^ b[i] ^ u;
if (v == p)
continue;
res += dfs(v, d + mx[i], u);
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef ONLINE_JUDGE
// freopen(TASK".inp","r",stdin);
// freopen(TASK".out","w",stdout);
#endif
cin >> n >> m >> k;
for (int i = 1; i <= m; i++) {
cin >> fr[i] >> to[i] >> w[i];
ind.push_back(i);
}
sort(ALL(ind), [](int i, int j) {
return w[i] < w[j];
});
for (int i = 1; i <= k; i++)
cin >> a[i] >> b[i];
{
memset(par, -1, sizeof(par));
vector <int> newind;
for (int i : ind) {
if (join(fr[i], to[i])) {
newind.push_back(i);
for (int j = 1; j <= k; j++)
if (!mx[j] && getpar(a[j]) == getpar(b[j]))
mx[j] = w[i];
}
}
swap(newind, ind);
}
{
vector <int> newind;
memset(par, -1, sizeof(par));
for (int i = 1; i <= k; i++)
join(a[i], b[i]);
for (int i : ind)
if (join(fr[i], to[i]))
newind.push_back(i);
swap(newind, ind);
}
memset(par, -1, sizeof(par));
for (int i = 1; i <= n; i++)
cin >> tot[i];
for (int i : ind)
join(fr[i], to[i]);
vector <int> node;
for (int i = 1; i <= n; i++) {
if (par[i] < 0)
node.push_back(i), val[i] = tot[i];
tot[i] = 0;
}
for (int i = 1; i <= k; i++) {
a[i] = getpar(a[i]);
b[i] = getpar(b[i]);
}
for (int i = 1; i <= n; i++)
adj[i].clear();
long long ans = 0;
int root = getpar(1);
for (int mask = 0; mask < bit(k); mask++) {
for (int v : node) {
par[v] = -1;
adj[v].clear();
}
bool ok = true;
for (int i = 1; i <= k; i++) if (getbit(mask, i - 1)) {
if (!join(a[i], b[i])) {
ok = false;
break;
}
adj[a[i]].push_back(i);
adj[b[i]].push_back(i);
}
if (!ok)
continue;
maxi(ans, dfs(root));
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3068 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3068 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3068 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3068 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |