Submission #384762

# Submission time Handle Problem Language Result Execution time Memory
384762 2021-04-02T08:01:02 Z ParsaAlizadeh Toll (APIO13_toll) C++17
16 / 100
5 ms 5500 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long       ll;
typedef pair<ll, ll>    pll;
typedef pair<int, int>  pii;

#define all(x)          x.begin(), x.end()
#define kill(x)         return cout << x << endl, 0
#define X               first
#define Y               second
#define endl            '\n'

constexpr ll pw(ll a, ll b, ll mod) {
    return (!b    ? 1 :
            b & 1 ? a * pw(a * a % mod, b / 2, mod) % mod :
                    pw(a * a % mod, b / 2, mod));
}

constexpr int N   = 1e5 + 10;
constexpr int M   = 3e5 + 10;
constexpr int K   = 20;

struct edge {
    int u, v, c;

    bool operator<(const edge & oth) {
        return c < oth.c;
    }
};

int n, m, k, par[N], sz[N];
vector<edge> E, Gr;
vector<int> comp[N];
vector<pii> adj[N];
ll ans;

int find(int u) {
    return (par[u] == -1 ? u : par[u] = find(par[u]));
}

int merge(int u, int v, int c) {
    u = find(u);
    v = find(v);
    if (u == v)
        return -2;
    if (v < u)
        swap(u, v);
    int flag = -1;
    for (int i = 0; i < comp[v].size(); i++) {
        int j = comp[v][i];
        if (find(Gr[j].u) == u) {
            Gr[j].c = c;
            flag = j;
            break;
        }
    }
    for (int x : comp[v])
        if (find(Gr[x].u) != u)
            comp[u].push_back(x);
    par[v] = u;
    return flag;
}

inline void add_edge(int u, int v, int c) {
    adj[u].emplace_back(v, c);
    adj[v].emplace_back(u, c);
}

void DFS(int u, int p, ll c) {
    for (auto & e : adj[u]) if (e.X != p) {
        DFS(e.X, u, e.Y);
        sz[u] += sz[e.X];
    }
    ans += c * sz[u];
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> m >> k;
    for (int i = 0; i < m; i++) {
        int u, v, c; 
        cin >> u >> v >> c;
        E.push_back({u, v, c});
    }
    sort(all(E));
    memset(par, -1, sizeof(par));
    for (int i = 0; i < k; i++) {
        int u, v;
        cin >> u >> v;
        if (v < u) swap(u, v);
        comp[v].push_back(i);
        Gr.push_back({u, v, 0});
    }
    for (int i = 1; i <= n; i++) {
        cin >> sz[i];
    }
    for (auto & e : E) {
        int flag = merge(e.u, e.v, e.c);
        if (flag == -1)
            add_edge(e.u, e.v, 0);
        else if (flag != -2)
            add_edge(Gr[flag].u, Gr[flag].v, Gr[flag].c);
    }
    DFS(1, 0, 0);
    cout << ans << endl;
    return 0;
}

Compilation message

toll.cpp: In function 'int merge(int, int, int)':
toll.cpp:50:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |     for (int i = 0; i < comp[v].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5484 KB Output is correct
2 Correct 5 ms 5500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5484 KB Output is correct
2 Correct 5 ms 5500 KB Output is correct
3 Incorrect 5 ms 5356 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5484 KB Output is correct
2 Correct 5 ms 5500 KB Output is correct
3 Incorrect 5 ms 5356 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5484 KB Output is correct
2 Correct 5 ms 5500 KB Output is correct
3 Incorrect 5 ms 5356 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5484 KB Output is correct
2 Correct 5 ms 5500 KB Output is correct
3 Incorrect 5 ms 5356 KB Output isn't correct
4 Halted 0 ms 0 KB -