Submission #1020652

# Submission time Handle Problem Language Result Execution time Memory
1020652 2024-07-12T08:05:17 Z CodeLakVN Roadside Advertisements (NOI17_roadsideadverts) C++17
7 / 100
40 ms 13128 KB
#include <bits/stdc++.h>

#define no "NO"
#define yes "YES"
#define F first
#define S second
#define vec vector
#define task "DATA"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maximize(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool minimize(U &a, V b) {
    if (a > b) { a = b; return 1; } return 0;
}

const int N = (int)2e5 + 9;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

int numNode;
vector<ii> adj[N];
int par[N][20], high[N], dist[N];

void dfs(int u) {
    for (auto e : adj[u]) {
        int v = e.F, w = e.S;
        if (v == par[u][0]) continue;
        dist[v] = dist[u] + w;
        par[v][0] = u;
        high[v] = high[u] + 1;
        dfs(v);
    }
}

void setupLCA() {
    dfs(1);
    FOR(i, 1, 18) FOR(u, 1, numNode)
        par[u][i] = par[par[u][i - 1]][i - 1];
    high[0] = -1;
}

int lca(int u, int v) {
    if (high[u] < high[v]) swap(u, v);
    FOD(i, 18, 0) if (high[par[u][i]] >= high[v])
        u = par[u][i];
    if (u == v) return u;
    FOD(i, 18, 0) if (par[u][i] != par[v][i]) {
        u = par[u][i];
        v = par[v][i];
    }
    return par[u][0];
}

bool cmp(int &u, int &v) {
    return high[u] <= high[v];
}

void main_code() {
    cin >> numNode;
    FOR(i, 1, numNode - 1) {
        int u, v, c;
        cin >> u >> v >> c;
        u++;
        v++;
        adj[u].push_back({v, c});
        adj[v].push_back({u, c});
    }

    setupLCA();

    int numQuery;
    cin >> numQuery;
    while (numQuery--) {
        vector<int> pos;
        FOR(i, 1, 5) {
            int x;
            cin >> x;
            x++;
            pos.push_back(x);
        }
        sort(pos.begin(), pos.end(), cmp);
        pos.push_back(pos[0]);
        int ans = 0;
        FOR(i, 0, 4) {
            ans += dist[pos[i]];
            ans -= dist[lca(pos[i], pos[i + 1])];
        }
        cout << ans << "\n";
    }
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    const bool MULTITEST = 0;
    int num_test = 1; if (MULTITEST) cin >> num_test;
    while (num_test--) { main_code(); cout << "\n"; }
}

/* Lak lu theo dieu nhac */

Compilation message

roadsideadverts.cpp: In function 'int main()':
roadsideadverts.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
roadsideadverts.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 40 ms 13128 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 11352 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4956 KB Output is correct
2 Incorrect 40 ms 13128 KB Output isn't correct
3 Halted 0 ms 0 KB -