# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
362689 |
2021-02-04T06:11:39 Z |
sebinkim |
Toll (APIO13_toll) |
C++14 |
|
1 ms |
624 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
struct edge{
int u, v, c;
edge() {}
edge(int u, int v, int c) : u(u), v(v), c(c) {}
bool operator < (const edge &e) const { return c < e.c; }
};
vector <edge> E, K;
vector <int> V;
int U1[101010], U2[101010];
ll C[101010];
vector <pii> T[44];
int P[44], U[44], D[44], Y[44], Z[44];
ll X[44], W[44];
int n, m, k, rt;
ll ans;
int find(int p) { return p == U[p]? p : U[p] = find(U[p]); }
int find1(int p) { return p == U1[p]? p : U1[p] = find1(U1[p]); }
int find2(int p) { return p == U2[p]? p : U2[p] = find2(U2[p]); }
ll dfs(int p, int r)
{
P[p] = r; X[p] = W[p];
for(pii &t: T[p]){
if(t.first != r){
Z[t.first] = t.second;
D[t.first] = D[p] + 1;
X[p] += dfs(t.first, p);
}
}
return X[p];
}
int color(int p, int q, int c)
{
if(p == q) return p;
if(D[p] < D[q]) swap(p, q);
if(U[p] == p){
Y[p] = c;
U[p] = color(P[p], q, c);
}
else U[p] = color(U[p], q, c);
}
int main()
{
int i, j, u, v, c;
ll s;
scanf("%d%d%d", &n, &m, &k);
for(i=0; i<m; i++){
scanf("%d%d%d", &u, &v, &c);
E.emplace_back(u, v, c);
}
sort(E.begin(), E.end());
for(i=1; i<=n; i++){
U1[i] = i;
}
for(edge &e: E){
if(find1(e.u) != find1(e.v)){
U1[find1(e.u)] = find1(e.v);
}
else e.c = 1e9;
}
for(i=0; i<k; i++){
scanf("%d%d", &u, &v);
E.emplace_back(u, v, 0);
}
for(i=1; i<=n; i++){
scanf("%lld", C + i);
}
sort(E.begin(), E.end());
for(i=1; i<=n; i++){
U1[i] = i; U2[i] = i;
}
for(edge &e: E){
if(find1(e.u) != find1(e.v)){
U1[find1(e.v)] = find1(e.u);
if(e.c){
C[find2(e.u)] += C[find2(e.v)];
U2[find2(e.v)] = find2(e.u);
e.c = 1e9;
}
}
}
sort(E.begin(), E.end());
for(; E.back().c > 1e6; E.pop_back());
for(i=1; i<=n; i++){
if(find2(i) == i) V.push_back(i);
}
sort(V.begin(), V.end());
for(i=0; i<V.size(); i++){
W[i] = C[V[i]];
}
rt = lower_bound(V.begin(), V.end(), find2(1)) - V.begin();
for(edge &e: E){
e.u = lower_bound(V.begin(), V.end(), find2(e.u)) - V.begin();
e.v = lower_bound(V.begin(), V.end(), find2(e.v)) - V.begin();
if(e.u == e.v) for(; ; );
}
for(i=0; i<(1<<k); i++){
K.clear();
for(j=0; j<V.size(); j++){
T[j].clear(); U[j] = j;
X[j] = Y[j] = Z[j] = 0;
}
for(j=0; j<k; j++) if(i & (1 << j)){
edge &e = E[j];
if(find(e.u) != find(e.v)){
U[find(e.u)] = find(e.v);
T[e.u].emplace_back(e.v, 1);
T[e.v].emplace_back(e.u, 1);
}
else break;
}
if(j < k) continue;
for(; j<E.size(); j++){
edge &e = E[j];
if(find(e.u) != find(e.v)){
U[find(e.u)] = find(e.v);
T[e.u].emplace_back(e.v, 0);
T[e.v].emplace_back(e.u, 0);
}
else K.push_back(e);
}
dfs(rt, rt);
for(j=0; j<V.size(); j++){
U[j] = j;
}
for(edge &e: K){
color(e.u, e.v, e.c);
}
for(j=0, s=0; j<V.size(); j++){
if(Z[j]) s += X[j] * Y[j];
}
ans = max(ans, s);
}
printf("%lld\n", ans);
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:118:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for(i=0; i<V.size(); i++){
| ~^~~~~~~~~
toll.cpp:133:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | for(j=0; j<V.size(); j++){
| ~^~~~~~~~~
toll.cpp:150:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | for(; j<E.size(); j++){
| ~^~~~~~~~~
toll.cpp:162:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
162 | for(j=0; j<V.size(); j++){
| ~^~~~~~~~~
toll.cpp:170:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
170 | for(j=0, s=0; j<V.size(); j++){
| ~^~~~~~~~~
toll.cpp: In function 'int color(int, int, int)':
toll.cpp:55:1: warning: control reaches end of non-void function [-Wreturn-type]
55 | }
| ^
toll.cpp: In function 'int main()':
toll.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
62 | 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]
65 | scanf("%d%d%d", &u, &v, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
83 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
toll.cpp:88:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
88 | scanf("%lld", C + i);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
624 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
624 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
624 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
624 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
624 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |