#include <cstdio>
#include <vector>
#include <algorithm>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef pair < int, int > pii;
const int N = 3e5 + 500;
const int M = 1e6 + 500;
int n, m, k;
int u[N], v[N], tz[N], mra[N];
int pu[N], pv[N], par[N], rd[N];
int lg[M];
ll siz[N];
vector < int > vrati, stb;
vector < pii > g[N];
bool cmp(int x, int y){
return tz[x] < tz[y];
}
int pr(int x){
if(par[x] == x) return x;
return par[x] = pr(par[x]);
}
void mrg(int x, int y, bool posebno = 0){
if(pr(x) == pr(y)) return;
if(posebno){
//printf("moram %d %d\n", x, y);
//printf("prije %lld %lld\n", siz[pr(x)], siz[pr(y)]);
siz[pr(x)] += siz[pr(y)];
}
par[pr(y)] = pr(x);
}
ll sol = 0;
ll dfs(int x, int lst){
ll ret = siz[x];
for(pii y : g[x]){
if(y.Y == lst) continue;
ll vel = dfs(y.Y, x);
if(y.X >= m)
sol += vel * tz[y.X];
ret += vel;
}
return ret;
}
int main(){
for(int i = 0;i < 20;i++)
lg[(1 << i)] = i;
scanf("%d%d%d", &n, &m, &k);
for(int i = 1;i <= n;i++)
par[i] = i;
for(int i = 0;i < m;i++){
scanf("%d%d%d", u + i, v + i, tz + i);
rd[i] = i;
}
sort(rd, rd + n, cmp);
for(int i = 0;i < k;i++){
scanf("%d%d", pu + i, pv + i);
mrg(pu[i], pv[i]);
}
for(int i = 0;i < m;i++){
if(pr(u[rd[i]]) != pr(v[rd[i]])){
mra[rd[i]] = 1; mrg(u[rd[i]], v[rd[i]]);
}
}
for(int i = 1;i <= n;i++)
par[i] = i;
for(int i = 0;i < m;i++){
if(mra[i]) mrg(u[i], v[i]);
}
for(int i = 1;i <= n;i++)
if(par[i] == i)
vrati.PB(i);
for(int i = 0;i < m;i++){
if(pr(u[rd[i]]) != pr(v[rd[i]])){
stb.PB(rd[i]); mrg(u[rd[i]], v[rd[i]]);
}
}
for(int i = 1;i <= n;i++){
par[i] = i; scanf("%lld", siz + i);
}
for(int i = 0;i < m;i++){
if(mra[i]) mrg(u[i], v[i], 1);
}
for(int x : stb)
u[x] = pr(u[x]), v[x] = pr(v[x]);
for(int i = 0;i < k;i++)
pu[i] = pr(pu[i]), pv[i] = pr(pv[i]);
ll ans = 0;
int root = pr(1);
//printf("tu sam\n");
//for(int x : vrati)
// printf("cvor %d vel %lld\n", x, siz[x]);
//printf("root = %d\n", root);
for(int msk = 0;msk < (1 << k);msk++){
int rmsk = msk;
//printf("rijesavam msk = %d\n", msk);
for(int x : vrati)
par[x] = x, g[x].clear();
for(int x : stb){
mrg(u[x], v[x]);
//printf("x = %d\n", x);
int cur = msk;
for(; cur ; cur -= cur & -cur){
int sd = lg[cur & -cur];
if(pr(pu[sd]) == pr(pv[sd])){
msk -= (1 << sd);
g[pu[sd]].PB({m + sd, pv[sd]});
g[pv[sd]].PB({m + sd, pu[sd]});
//printf("dodajem pos %d -- %d -- %d\n", pu[sd], tz[x], pv[sd]);
tz[m + sd] = tz[x];
break;
}
}
if(!cur){
//printf("dodajem %d -- %d -- %d\n", u[x], tz[x], v[x]);
g[u[x]].PB({x, v[x]}), g[v[x]].PB({x, u[x]});
}
}
dfs(root, root);
msk = rmsk;
ans = max(ans, sol);
}
printf("%lld\n", ans);
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:61:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &n, &m, &k);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", u + i, v + i, tz + i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", pu + i, pv + i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:92:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
par[i] = i; scanf("%lld", siz + i);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |