답안 #1100143

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1100143 2024-10-12T23:34:37 Z CpDark Roadside Advertisements (NOI17_roadsideadverts) C++17
100 / 100
60 ms 15828 KB
#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vp;
typedef vector<vp> vvp;

vvp graph;

// LCA - Lowest common ancestor
struct LCA {
    vvp table;
    vi timeIn;
    vi timeOut;
    vi depth;
    int log;
	int timer;
	int n;

    void dfs(int v, int p, int d, int w) {
        depth[v] = d;
        timeIn[v] = timer;
        table[v][0] = { p, w };
		timer++;

		for (int i = 0; i < graph[v].size(); i++) {
            int u = graph[v][i].first;
            int w = graph[v][i].second;
            if (u != p) dfs(u, v, d + 1, w);
		}

		timeOut[v] = timer;
		timer++;
	}

	void preCalc(int N, int root) {
		n = N;
		log = ceil(log2(n));
		timeIn.resize(n + 1);
        timeOut.resize(n + 1);
        depth.resize(n + 1);
        table.resize(n + 1, vp(log + 1));
		timer = 0;

		dfs(root, root, 0, 0);
		for (int i = 1; i <= log; i++) {
			for (int v = 1; v <= n; v++) {
                int p = table[v][i - 1].first;
                int w = table[v][i - 1].second;
                table[v][i] = { table[p][i - 1].first, table[p][i - 1].second + w };
			}
		}
	}

	bool isAncestor(int a, int b) { // Is a ancestor of b
		if (timeIn[a] <= timeIn[b] && timeOut[a] >= timeOut[b]) return true;
		return false;
	}

	int findLCA(int a, int b) {
		if (isAncestor(a, b)) return a;
		if (isAncestor(b, a)) return b;
		for (int i = log; i >= 0; i--) if (!isAncestor(table[a][i].first, b)) a = table[a][i].first;
		return table[a][0].first;
    }

    int LCA5(vector<int>& nodes) {  
        int ans = nodes[0];
        for (int i = 1; i < nodes.size(); i++) ans = findLCA(ans, nodes[i]);
        return ans;
    }
 
    int subsequentPathSum(int v, int u) {
        if (depth[v] < depth[u]) swap(v, u);
    
        int diff = depth[v] - depth[u];
        int sum = 0;
        for (int i = 0; i <= log; i++) {
			if ((diff >> i) & 1) sum += table[v][i].second, v = table[v][i].first;
			if (v == -1) return -1;
		}
        return sum;
    }
};

LCA lca;
bool cmp(int v, int u) {
    return lca.timeIn[v] < lca.timeIn[u];
}


void init(int N, vector<int> V, vector<int> U, vector<int> W) {
    graph.resize(N);
    for (int i = 0;i < N - 1;i++) {
        graph[V[i]].push_back({ U[i],W[i] });
        graph[U[i]].push_back({ V[i],W[i] });
    }
    lca.preCalc(N, 0);
}


int query(int a, int b, int c, int d, int e) {
    vi nodes = { a,b,c,d,e };
    sort(nodes.begin(), nodes.end(), cmp);
    
    int root = lca.LCA5(nodes);
	int ans = lca.subsequentPathSum(root, nodes[0]);
	for (int i = 1; i < 5; i++) ans += lca.subsequentPathSum(nodes[i], lca.findLCA(nodes[i - 1], nodes[i]));
 
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, Q;
    cin >> N;

    vi V(N - 1), U(N - 1), W(N - 1);
    for (int i = 0;i < N - 1;i++)cin >> V[i] >> U[i] >> W[i];
    
    init(N, V, U, W);

    cin >> Q;
    for (int i = 0;i < Q;i++) {
        int a, b, c, d, e;
        cin >> a >> b >> c >> d >> e;
        cout << query(a, b, c, d, e) << endl;
    }
    return 0;
}

Compilation message

roadsideadverts.cpp: In member function 'void LCA::dfs(int, int, int, int)':
roadsideadverts.cpp:27:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |   for (int i = 0; i < graph[v].size(); i++) {
      |                   ~~^~~~~~~~~~~~~~~~~
roadsideadverts.cpp: In member function 'int LCA::LCA5(std::vector<int>&)':
roadsideadverts.cpp:70:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |         for (int i = 1; i < nodes.size(); i++) ans = findLCA(ans, nodes[i]);
      |                         ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 14920 KB Output is correct
2 Correct 51 ms 15696 KB Output is correct
3 Correct 55 ms 15700 KB Output is correct
4 Correct 60 ms 15700 KB Output is correct
5 Correct 60 ms 15708 KB Output is correct
6 Correct 54 ms 15700 KB Output is correct
7 Correct 52 ms 15708 KB Output is correct
8 Correct 51 ms 15708 KB Output is correct
9 Correct 53 ms 15700 KB Output is correct
10 Correct 53 ms 15828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 14240 KB Output is correct
2 Correct 29 ms 15452 KB Output is correct
3 Correct 32 ms 15444 KB Output is correct
4 Correct 29 ms 15372 KB Output is correct
5 Correct 29 ms 15452 KB Output is correct
6 Correct 28 ms 15436 KB Output is correct
7 Correct 34 ms 15348 KB Output is correct
8 Correct 31 ms 15452 KB Output is correct
9 Correct 30 ms 15440 KB Output is correct
10 Correct 29 ms 15368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 55 ms 14920 KB Output is correct
3 Correct 51 ms 15696 KB Output is correct
4 Correct 55 ms 15700 KB Output is correct
5 Correct 60 ms 15700 KB Output is correct
6 Correct 60 ms 15708 KB Output is correct
7 Correct 54 ms 15700 KB Output is correct
8 Correct 52 ms 15708 KB Output is correct
9 Correct 51 ms 15708 KB Output is correct
10 Correct 53 ms 15700 KB Output is correct
11 Correct 53 ms 15828 KB Output is correct
12 Correct 28 ms 14240 KB Output is correct
13 Correct 29 ms 15452 KB Output is correct
14 Correct 32 ms 15444 KB Output is correct
15 Correct 29 ms 15372 KB Output is correct
16 Correct 29 ms 15452 KB Output is correct
17 Correct 28 ms 15436 KB Output is correct
18 Correct 34 ms 15348 KB Output is correct
19 Correct 31 ms 15452 KB Output is correct
20 Correct 30 ms 15440 KB Output is correct
21 Correct 29 ms 15368 KB Output is correct
22 Correct 58 ms 14940 KB Output is correct
23 Correct 47 ms 14172 KB Output is correct
24 Correct 60 ms 15700 KB Output is correct
25 Correct 54 ms 15624 KB Output is correct
26 Correct 58 ms 15700 KB Output is correct
27 Correct 54 ms 15708 KB Output is correct
28 Correct 56 ms 15704 KB Output is correct
29 Correct 55 ms 15700 KB Output is correct
30 Correct 55 ms 15708 KB Output is correct
31 Correct 57 ms 15708 KB Output is correct