이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
void local() {
#define taskname ""
if(fopen(taskname".inp", "r")) {
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
}
#define ll long long
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
template <class X, class Y> bool mini(X& x, Y y) {return x > y ? x = y, true : false;}
template <class X, class Y> bool maxi(X& x, Y y) {return x < y ? x = y, true : false;}
const int N = 1e5 + 5;
const int LG = 17;
int n, s, q, h, c[N];
vector<tuple<int, int, int>>edge;
pair<int, int>query[N];
namespace sub2 {
bool check() {
return n * q <= 1e6;
}
vector<pair<int, int>>adj[N];
bitset<N>flag;
ll res = 1e18;
void dfs(int u, int p, ll d) {
if(u == h) res = -1;
if(flag[u]) mini(res, d);
for(pair<int, int>& pi : adj[u]) {
int v, w; tie(v, w) = pi;
if(v == p) continue;
dfs(v, u, d + w);
}
}
void solve() {
for(int i = 1; i <= s; i++) flag[c[i]] = true;
for(int _ = 1; _ <= q; _++) {
int ban, r; tie(ban, r) = query[_];
for(int i = 1; i <= n; i++) adj[i].clear();
for(int i = 1; i < n; i++) {
if(i == ban) continue;
int u, v, w; tie(u, v, w) = edge[i];
adj[u].emplace_back(v, w); adj[v].emplace_back(u, w);
}
res = 1e18; dfs(r, 0, 0);
if(res == -1) cout << "escaped\n";
else if(res < 1e18) cout << res << '\n';
else cout << "oo\n";
}
}
}
namespace sub3 {
bool check() {
return s == n;
}
bool res[N];
vector<int>adj[N];
int st[N], en[N], id = 0;
void pre_dfs(int u, int p) {
st[u] = ++id;
for(int& v: adj[u]) {
if(v == p) continue;
pre_dfs(v, u);
}
en[u] = id;
}
bool isin(int x, int l, int r) {
return l <= x && x <= r;
}
void solve() {
for(int i = 1; i < n; i++) {
int u, v, w; tie(u, v, w) = edge[i];
adj[u].emplace_back(v); adj[v].emplace_back(u);
}
pre_dfs(h, 0);
for(int i = 1; i <= q; i++) {
int ban, r; tie(ban, r) = query[i];
int u, v, w; tie(u, v, w) = edge[ban];
if(st[u] > st[v]) swap(u, v);
if(isin(st[r], st[v], en[v])) cout << "0\n";
else cout << "escaped\n";
}
}
}
namespace sub4 {
bitset<N>flag;
ll res[N];
vector<pair<int, int>>adj[N];
vector<pair<int, int>>event[N];
int st[N], en[N], id = 0, de[N]; ll mi[N], dist[N], up[N][LG], e[N][LG], upw[N];
void pre_dfs(int u, int p) {
st[u] = ++id;
mi[u] = 1e18;
for(pair<int, int>& pi : adj[u]) {
int v, w; tie(v, w) = pi;
if(v == p) continue;
upw[v] = w;
dist[v] = dist[u] + w;
de[v] = de[up[v][0] = u] + 1;
for(int i = 1; (1 << i) <= de[v]; i++) up[v][i] = up[up[v][i - 1]][i - 1];
pre_dfs(v, u);
mini(mi[u], mi[v] + w);
}
en[u] = id;
if(flag[u]) mi[u] = 0;
}
void dfs1(int u, int p) {
pair<ll, int> p0, p1;
p0 = p1 = make_pair(1e18, 0), make_pair(1e18, 0);
for(pair<int, int>& pi : adj[u]) {
int v, w; tie(v, w) = pi;
if(v == p) continue;
dfs1(v, u);
if(flag[u]) e[v][0] = w;
if(!mini(p0, make_pair(mi[v] + w, v))) mini(p1, make_pair(mi[v] + w, v));
}
for(pair<int, int>& pi : adj[u]) {
int v, w; tie(v, w) = pi;
if(v == p) continue;
if(p0.se == v) mini(e[v][0], p1.fi + upw[v]);
else mini(e[v][0], p0.fi + upw[v]);
//cout << v << ' ' << e[v][0] << '\n';
}
}
void dfs2(int u, int p) {
for(int i = 1; (1 << i) <= de[u]; i++) {
e[u][i] = min(e[u][i - 1], e[up[u][i - 1]][i - 1] + dist[u] - dist[up[u][i - 1]]);
//cout << u << ' ' << i << ' ' << up[u][i - 1] << ' ' << e[up[u][i - 1]][i - 1] << ' ' << dist[u] - dist[up[u][i - 1]] << ' ' << e[u][i] << '\n';
}
for(pair<int, int>& pi : adj[u]) {
int v, w; tie(v, w) = pi;
if(v == p) continue;
dfs2(v, u);
}
}
bool isin(int x, int l, int r) {
return l <= x && x <= r;
}
void solve() {
for(int i = 1; i <= s; i++) flag[c[i]] = true;
for(int i = 1; i < n; i++) {
int u, v, w; tie(u, v, w) = edge[i];
adj[u].emplace_back(v, w); adj[v].emplace_back(u, w);
}
for(int i = 1; i <= n; i++) {
for(int j = 0; j < LG; j++) {
e[i][j] = 1e18;
}
}
pre_dfs(h, 0);
dfs1(h, 0); dfs2(h, 0);
e[h][0] = mi[h];
for(int i = 1; i <= q; i++) {
res[i] = 1e18;
int ban, r; tie(ban, r) = query[i];
int u, v, w; tie(u, v, w) = edge[ban];
if(st[u] > st[v]) swap(u, v);
if(!isin(st[r], st[v], en[v])) cout << "escaped\n";
else {
res[i] = mi[r];
int dst = de[r] - de[v], u = r;
for(int j = 0; (1 << j) <= dst; j++) {
if(dst & (1 << j)) {
mini(res[i], e[u][j] + dist[r] - dist[u]);
u = up[u][j];
}
}
if(res[i] >= 1e18) cout << "oo\n";
else cout << res[i] << '\n';
}
}
}
}
int main() {
fastio; local();
cin >> n >> s >> q >> h;
edge.emplace_back(0, 0, 0);
for(int i = 1; i < n; i++) {
int u, v, w ; cin >> u >> v >> w;
edge.emplace_back(u, v, w);
}
for(int i = 1; i <= s; i++) cin >> c[i];
for(int i = 1; i <= q; i++) cin >> query[i].fi >> query[i].se;
sub4::solve();
}
컴파일 시 표준 에러 (stderr) 메시지
valley.cpp: In function 'void local()':
valley.cpp:7:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
7 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:8:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |