답안 #322153

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
322153 2020-11-14T06:59:55 Z Sparky_09 Roadside Advertisements (NOI17_roadsideadverts) C++17
100 / 100
137 ms 20076 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long

int n, l;
const int N = (int)1e5+5;
vector<int> graph[N], gdist[N], d(N), tin, tout;
int timer;
vector<vector<int>> up;

void dfs(int v, int p){
  tin[v] = ++timer;
  up[v][0] = p;
  for(int i = 1; i <= l; i++){
    up[v][i] = up[up[v][i-1]][i-1];
  }
  for(int i = 0; i < gdist[v].size(); i++){
    int loc = graph[v][i];
    if(loc==p) continue;
    d[loc]=d[v]+gdist[v][i];
    dfs(loc,v);
  }
  tout[v] = ++timer;
}

bool is_ancestor(int u, int v){
  return tin[u]<=tin[v] and tout[u]>=tout[v];
}

int lca(int u, int v){
  if(is_ancestor(u,v)) return u;
  if(is_ancestor(v,u)) return v;
  for(int i = l; i >= 0; --i){
    if(!is_ancestor(up[u][i], v)) u = up[u][i];
  }
  return up[u][0];
}

bool fun(int x, int y){
  return tin[x] < tin[y];
}

int dist(int x, int y){
  return d[x] + d[y] - 2 * d[lca(x, y)];
}
             
int main(){
  ios_base::sync_with_stdio(false);
  //freopen("input.txt", "r", stdin);
  cin >> n;
  for(int i = 0; i < n-1; i++){
    int x,y,d; cin>>x>>y>>d;
    graph[x].emplace_back(y); 
    graph[y].push_back(x);
    gdist[x].emplace_back(d); 
    gdist[y].push_back(d);
  }

  //preprocess
  tin.resize(n);
  tout.resize(n);
  timer = 0; 
  l = 18;
  up.assign(n, vector<int>(l+1));
  dfs(0,0);

  int quer;
  cin>>quer;
  while(quer--){
    vector<int> q(5);
    for(int i = 0; i < 5; i++) cin >> q[i];
    sort(q.begin(), q.end(), fun);
    int ans = 0;
    ans += dist(q[0], lca(q[4], q[0]));
    for(int i = 1; i < 5; i++){
      ans += dist(lca(q[i-1],q[i]), q[i]);
    }
    cout << ans << '\n';
  }
}

Compilation message

roadsideadverts.cpp: In function 'void dfs(int, int)':
roadsideadverts.cpp:17:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |   for(int i = 0; i < gdist[v].size(); i++){
      |                  ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 17004 KB Output is correct
2 Correct 119 ms 20076 KB Output is correct
3 Correct 120 ms 19820 KB Output is correct
4 Correct 118 ms 19912 KB Output is correct
5 Correct 125 ms 19820 KB Output is correct
6 Correct 120 ms 19948 KB Output is correct
7 Correct 122 ms 19900 KB Output is correct
8 Correct 132 ms 19820 KB Output is correct
9 Correct 120 ms 19948 KB Output is correct
10 Correct 133 ms 19820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 14956 KB Output is correct
2 Correct 100 ms 18668 KB Output is correct
3 Correct 99 ms 18796 KB Output is correct
4 Correct 102 ms 18668 KB Output is correct
5 Correct 93 ms 18668 KB Output is correct
6 Correct 88 ms 18668 KB Output is correct
7 Correct 89 ms 18668 KB Output is correct
8 Correct 92 ms 18668 KB Output is correct
9 Correct 91 ms 18668 KB Output is correct
10 Correct 111 ms 18668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5356 KB Output is correct
2 Correct 116 ms 17004 KB Output is correct
3 Correct 119 ms 20076 KB Output is correct
4 Correct 120 ms 19820 KB Output is correct
5 Correct 118 ms 19912 KB Output is correct
6 Correct 125 ms 19820 KB Output is correct
7 Correct 120 ms 19948 KB Output is correct
8 Correct 122 ms 19900 KB Output is correct
9 Correct 132 ms 19820 KB Output is correct
10 Correct 120 ms 19948 KB Output is correct
11 Correct 133 ms 19820 KB Output is correct
12 Correct 64 ms 14956 KB Output is correct
13 Correct 100 ms 18668 KB Output is correct
14 Correct 99 ms 18796 KB Output is correct
15 Correct 102 ms 18668 KB Output is correct
16 Correct 93 ms 18668 KB Output is correct
17 Correct 88 ms 18668 KB Output is correct
18 Correct 89 ms 18668 KB Output is correct
19 Correct 92 ms 18668 KB Output is correct
20 Correct 91 ms 18668 KB Output is correct
21 Correct 111 ms 18668 KB Output is correct
22 Correct 115 ms 18028 KB Output is correct
23 Correct 108 ms 16108 KB Output is correct
24 Correct 129 ms 19180 KB Output is correct
25 Correct 128 ms 19052 KB Output is correct
26 Correct 137 ms 19180 KB Output is correct
27 Correct 135 ms 19052 KB Output is correct
28 Correct 127 ms 19052 KB Output is correct
29 Correct 130 ms 19052 KB Output is correct
30 Correct 126 ms 19052 KB Output is correct
31 Correct 135 ms 19052 KB Output is correct