QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#141459 | #6659. 외곽 순환 도로 2 | penguinman# | Compile Error | / | / | C++17 | 4.5kb | 2023-08-17 14:03:35 | 2024-08-26 15:51:09 |
Judging History
你现在查看的是最新测评结果
- [2024-08-26 15:51:09]
- 管理员手动重测本题所有提交记录
- 测评结果:Compile Error
- 用时:0ms
- 内存:0kb
- [2024-07-04 01:46:38]
- 评测
- 测评结果:Compile Error
- 用时:0ms
- 内存:0kb
- [2023-08-17 14:03:35]
- 提交
answer
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll,ll>;
#define rep(i,j,k) for(ll i=ll(j); i<ll(k); i++)
#define REP(i,j,k) for(ll i=ll(j); i<=ll(k); i++)
#define per(i,j,k) for(ll i=ll(j); i>=ll(k); i--)
#define ln "\n"
#define all(a) a.begin(),a.end()
#define pb emplace_back
#define mp std::make_pair
#define mtp std::make_tuple
constexpr ll inf = (1ll<<60);
struct union_find{
ll N;
vi par, sz;
union_find(ll n): N(n){
par.resize(N);
sz.resize(N,1);
rep(i,0,N) par[i] = i;
}
ll root(ll now){
if(par[now] == now) return now;
return par[now] = root(par[now]);
}
void merge(ll u, ll v){
u = root(u);
v = root(v);
if(u == v) return;
if(sz[u] > sz[v]) std::swap(u,v);
sz[v] += sz[u];
sz[u] = 0;
par[u] = v;
}
bool same(ll u, ll v){
return root(u) == root(v);
}
};
long long place_police(vector<int> P, vector<long long> C, vector<long long> W){
ll N = P.size()+1;
vii edge(N), weight(N);
vi par(N,-1), wpar(N,inf);
rep(i,1,N){
edge[P[i-1]].pb(i);
weight[P[i-1]].pb(C[i-1]);
par[i] = P[i-1];
wpar[i] = C[i-1];
}
vi ord;
vi left(N), right(N);
std::function<void(ll)> dfs = [&](ll now){
left[now] = ord.size();
ord.pb(now);
for(auto next: edge[now]){
dfs(next);
}
right[now] = ord.size();
ord.pb(now);
};
dfs(0);
ll K = W.size();
vi V;
rep(i,0,N){
if(left[i] == right[i]-1) V.pb(i);
}
assert(V.size() == K);
vi ml(K), mr(K), mm(K);
rep(i,0,K){
ml[i] = mr[i] = inf;
mm[i] = W[i];
ll dist = 0;
{
ll now = V[i];
ll idx = right[now];
while(true){
ml[i] = std::min(ml[i], wpar[now]);
dist++;
if(ord[idx+1] != par[now]) break;
idx++;
now = par[now];
}
}
{
ll now = V[(i+1)%K];
ll idx = left[now];
while(true){
dist++;
mr[i] = std::min(mr[i], wpar[now]);
if(ord[idx-1] != par[now]) break;
idx--;
now = par[now];
}
}
if(dist%2) mm[i] = 0;
//cout << ml[i] << " " << mr[i] << " " << mm[i] << ln;
}
ll ans = inf;
{
vii dp(K+1, vi(2,inf));
dp[0][0] = 0;
rep(i,0,K){
dp[i+1][0] = dp[i][0]+std::min({ml[i], mr[i]});
dp[i+1][1] = dp[i][1]+std::min({ml[i], mr[i], mm[i]});
dp[i+1][1] = std::min(dp[i+1][1], dp[i][0]+mm[i]);
if(i > 0){
dp[i+1][1] = std::min(dp[i+1][1], dp[i-1][1]+std::max(mr[i-1], ml[i]));
dp[i+1][0] = std::min(dp[i+1][0], dp[i-1][0]+std::max(mr[i-1], ml[i]));
}
}
ans = std::min(ans, dp[K][1]);
}
{
{
vi ML(K), MR(K), MM(K);
rep(i,0,K){
ML[i] = ml[(i+K-1)%K];
MR[i] = mr[(i+K-1)%K];
MM[i] = mm[(i+K-1)%K];
}
ml = ML, mr = MR, mm = MM;
}
vii dp(K+1, vi(2,inf));
dp[0][0] = 0;
rep(i,0,K){
dp[i+1][0] = dp[i][0]+std::min({ml[i], mr[i]});
dp[i+1][1] = dp[i][1]+std::min({ml[i], mr[i], mm[i]});
dp[i+1][1] = std::min(dp[i+1][1], dp[i][0]+mm[i]);
if(i > 0){
dp[i+1][1] = std::min(dp[i+1][1], dp[i-1][1]+std::max(mr[i-1], ml[i]));
dp[i+1][0] = std::min(dp[i+1][0], dp[i-1][0]+std::max(mr[i-1], ml[i]));
}
}
ans = std::min(ans, dp[K][1]);
}
return ans;
}
void my_assert(bool x) {
if (!x) {
puts("Wrong input");
exit(0);
}
}
int main() {
int N;
my_assert(scanf("%d", &N) == 1);
vector<int> P(N - 1);
vector<long long> C(N - 1), W;
vector<int> cnt(N);
for (int i = 0; i < N - 1; i++) {
my_assert(scanf("%d %lld", &P[i], &C[i]) == 2);
cnt[i+1]++;
cnt[P[i]]++;
}
for (int i = 0; i < N; i++) {
if (cnt[i] == 1) {
long long w;
my_assert(scanf("%lld", &w) == 1);
W.push_back(w);
}
}
printf("%lld\n", place_police(P, C, W));
return 0;
}
Details
/usr/bin/ld: /tmp/ccIjLdk6.o: in function `my_assert(bool)': answer.code:(.text+0x270): multiple definition of `my_assert(bool)'; /tmp/ccgopPD5.o:implementer.cpp:(.text+0x30): first defined here /usr/bin/ld: /tmp/ccIjLdk6.o: in function `main': answer.code:(.text.startup+0x0): multiple definition of `main'; /tmp/ccgopPD5.o:implementer.cpp:(.text.startup+0x0): first defined here collect2: error: ld returned 1 exit status