/** work as hard as you can and keep the first place **/
#pragma GCC optimize("Ofast, fma")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define SZ(x) (int)x.size()
#define Mp make_pair
const int N = 3e5 + 50;
const ll mod = 1e9 + 7;
const ll inf = 8e18;
const int LOG = 20;
ll tot, ret, ted[N], sub[N], Size[N];
int n, m, k, ptr, ROOT, sz[N], V[N], U[N], C[N], par[N], ord[N], in1[N], in2[N], T[N], query[N], St[N], Fi[N];
vector < int > vec, roots, G[N];
inline void init() { for(int i = 1; i <= n; i ++) sz[i] = 1, par[i] = i, ted[i] = T[i]; }
int get(int x) { return (x == par[x]? x : par[x] = get(par[x])); }
bool unify(int v, int u)
{
v = get(v), u = get(u);
if(v == u) return 0;
par[u] = v;
ted[v] += ted[u];
return 1;
}
bool cmp(int i, int j)
{
return C[i] < C[j];
}
set < pii > st[N];
void pre(int v, int last = 0)
{
St[v] = ++ptr;
for(auto id : G[v])
{
if(id == last) continue;
int u = V[id] ^ U[id] ^ v;
pre(u, id);
}
Fi[v] = ptr;
///printf("v = %d St = %d Fi = %d\n", v, St[v], Fi[v]);
}
void dfs(int v, int last = 0)
{
sub[v] = Size[v];
for(auto id : G[v])
{
if(id == last) continue;
int u = V[id] ^ U[id] ^ v;
dfs(u, id);
if(SZ(st[v]) < SZ(st[u])) st[v].swap(st[u]);
for(auto now : st[u])
{
int node = now.S;
if(!(St[v] <= St[node] && Fi[node] <= Fi[v]))
{
st[v].insert(now);
}
}
sub[v] += sub[u];
}
pii cu = *st[v].begin();
while(St[v] <= St[cu.S] && Fi[cu.S] <= Fi[v])
{
st[v].erase(cu);
cu = *st[v].begin();
}
if(last > m)
{
ret += 1ll * cu.F * sub[v];
}
}
void solve(int mask)
{
///printf("mask = %d\n", mask);
ptr = 0;
ret = 0;
for(auto v : roots) sz[v] = 1, G[v].clear(), par[v] = v, ted[v] = 0, st[v].clear();
for(int i = m + 1; i <= m + k; i ++)
{
int id = i - m - 1;
if(mask >> id & 1)
{
G[V[i]].push_back(i);
G[U[i]].push_back(i);
///printf("E = %d\n", i);
if(!unify(V[i], U[i]))
{
///printf("0\n");
return;
}
}
}
for(auto i : vec)
{
///printf("%d %d\n", V[i], U[i]);
if(V[i] == U[i]) continue;
if(unify(V[i], U[i]))
{
G[V[i]].push_back(i);
G[U[i]].push_back(i);
///printf("E = %d\n", i);
}
else
{
st[V[i]].insert({C[i], U[i]});
st[U[i]].insert({C[i], V[i]});
}
}
pre(ROOT);
dfs(ROOT);
tot = max(tot, ret);
///printf("%lld\n", ret);
}
int main()
{
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= m; i ++)
{
scanf("%d%d%d", &V[i], &U[i], &C[i]);
ord[i] = i;
}
sort(ord + 1, ord + m + 1, cmp);
init();
for(int j = 1; j <= m; j ++)
{
int i = ord[j];
in1[i] = unify(V[i], U[i]);
}
init();
for(int i = m + 1; i <= m + k; i ++)
{
scanf("%d%d", &V[i], &U[i]);
unify(V[i], U[i]);
}
for(int i = 1; i <= n; i ++) { scanf("%d", &T[i]); }
for(int j = 1; j <= m; j ++)
{
int i = ord[j];
in2[i] = unify(V[i], U[i]);
}
init();
for(int i = 1; i <= m; i ++)
{
if(in1[i] > in2[i])
{
vec.push_back(i);
}
if(in2[i])
{
unify(V[i], U[i]);
}
}
for(int i = 1; i <= n; i ++)
{
roots.push_back(get(i));
}
sort(all(roots));
roots.resize(unique(all(roots)) - roots.begin());
sort(all(vec), cmp);
for(auto v : roots)
{
Size[v] = ted[v];
///printf("v = %d Size = %lld\n", v, Size[v]);
}
for(int i = 1; i <= m + k; i ++)
{
V[i] = get(V[i]), U[i] = get(U[i]);
}
ROOT = get(1);
for(int mask = 0; mask < 1 << k; mask ++)
{
solve(mask);
}
printf("%lld\n", tot);
return 0;
}
/** test corner cases(n = 1?) watch for overflow or minus indices **/
Compilation message
toll.cpp:3:34: warning: bad option '-f fma' to pragma 'optimize' [-Wpragmas]
3 | #pragma GCC optimize("Ofast, fma")
| ^
toll.cpp:30:18: warning: bad option '-f fma' to attribute 'optimize' [-Wattributes]
30 | inline void init() { for(int i = 1; i <= n; i ++) sz[i] = 1, par[i] = i, ted[i] = T[i]; }
| ^
toll.cpp:32:14: warning: bad option '-f fma' to attribute 'optimize' [-Wattributes]
32 | int get(int x) { return (x == par[x]? x : par[x] = get(par[x])); }
| ^
toll.cpp:34:24: warning: bad option '-f fma' to attribute 'optimize' [-Wattributes]
34 | bool unify(int v, int u)
| ^
toll.cpp:43:22: warning: bad option '-f fma' to attribute 'optimize' [-Wattributes]
43 | bool cmp(int i, int j)
| ^
toll.cpp:50:29: warning: bad option '-f fma' to attribute 'optimize' [-Wattributes]
50 | void pre(int v, int last = 0)
| ^
toll.cpp:63:29: warning: bad option '-f fma' to attribute 'optimize' [-Wattributes]
63 | void dfs(int v, int last = 0)
| ^
toll.cpp:94:20: warning: bad option '-f fma' to attribute 'optimize' [-Wattributes]
94 | void solve(int mask)
| ^
toll.cpp:137:10: warning: bad option '-f fma' to attribute 'optimize' [-Wattributes]
137 | int main()
| ^
toll.cpp: In function 'int main()':
toll.cpp:139:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | scanf("%d%d%d", &n, &m, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:142:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | scanf("%d%d%d", &V[i], &U[i], &C[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:155:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
155 | scanf("%d%d", &V[i], &U[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:158:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
158 | for(int i = 1; i <= n; i ++) { scanf("%d", &T[i]); }
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21452 KB |
Output is correct |
2 |
Correct |
13 ms |
21452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21452 KB |
Output is correct |
2 |
Correct |
13 ms |
21452 KB |
Output is correct |
3 |
Correct |
15 ms |
21500 KB |
Output is correct |
4 |
Correct |
15 ms |
21452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21452 KB |
Output is correct |
2 |
Correct |
13 ms |
21452 KB |
Output is correct |
3 |
Correct |
15 ms |
21500 KB |
Output is correct |
4 |
Correct |
15 ms |
21452 KB |
Output is correct |
5 |
Correct |
16 ms |
21620 KB |
Output is correct |
6 |
Correct |
16 ms |
21688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21452 KB |
Output is correct |
2 |
Correct |
13 ms |
21452 KB |
Output is correct |
3 |
Correct |
15 ms |
21500 KB |
Output is correct |
4 |
Correct |
15 ms |
21452 KB |
Output is correct |
5 |
Correct |
16 ms |
21620 KB |
Output is correct |
6 |
Correct |
16 ms |
21688 KB |
Output is correct |
7 |
Correct |
316 ms |
31088 KB |
Output is correct |
8 |
Correct |
350 ms |
31164 KB |
Output is correct |
9 |
Correct |
339 ms |
31216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21452 KB |
Output is correct |
2 |
Correct |
13 ms |
21452 KB |
Output is correct |
3 |
Correct |
15 ms |
21500 KB |
Output is correct |
4 |
Correct |
15 ms |
21452 KB |
Output is correct |
5 |
Correct |
16 ms |
21620 KB |
Output is correct |
6 |
Correct |
16 ms |
21688 KB |
Output is correct |
7 |
Correct |
316 ms |
31088 KB |
Output is correct |
8 |
Correct |
350 ms |
31164 KB |
Output is correct |
9 |
Correct |
339 ms |
31216 KB |
Output is correct |
10 |
Execution timed out |
2596 ms |
31284 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |