이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;
const int nmax = 200001;
vector <vector <pii> > g(nmax);
int in[nmax], out[nmax], timer = 0;
int p[nmax][20];
int d[nmax];
void dfs(int v, int e){
in[v] = ++timer;
p[v][0] = e;
for(int j= 1; j < 20; j++)
p[v][j] = p[p[v][j - 1]][j - 1];
for(auto x : g[v]){
int to = x.f, len = x.s;
if(to == e) continue;
d[to] = d[v] + len;
dfs(to, v);
}
out[v] = ++timer;
}
bool ispar(int x, int y){
return in[x] <= in[y] && out[x] >= out[y];
}
int lca(int u, int v){
if(ispar(u, v))
return u;
if(ispar(v, u))
return v;
for(int j = 19; j >= 0; j--){
if(ispar(p[v][j], u)) continue;
v = p[v][j];
}
return p[v][0];
}
int dist(int a, int b){
return d[a] + d[b] - 2 * d[lca(a, b)];
}
int solve(vector <int> vec){
vector <pii> t;
for(int to : vec)
t.pb({in[to], to});
sort(t.begin(), t.end());
for(int j = 1; j< vec.size(); j++){
int x = lca(t[j - 1].s, t[j].s);
t.pb({in[x], x});
}
sort(t.begin(), t.end());
int ans = 0;
stack <int> st;
st.push(t[0].s);
for(int i = 1; i < t.size(); i++){
while(!st.empty()){
int x = st.top();
if(out[x] < in[t[i].s]) st.pop();
else break;
}
int x = st.top();
ans += dist(x, t[i].s);
st.push(t[i].s);
}
return ans;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
int w; cin >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
dfs(0, 0);
int q; cin >> q;
while(q--){
vector <int> x(5);
for(int i = 0; i < 5; i++)
cin >> x[i];
cout << solve(x) << "\n";
}
}
컴파일 시 표준 에러 (stderr) 메시지
roadsideadverts.cpp: In function 'int solve(std::vector<int>)':
roadsideadverts.cpp:57:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int j = 1; j< vec.size(); j++){
| ~^~~~~~~~~~~~
roadsideadverts.cpp:65:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i = 1; i < t.size(); i++){
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |