結果

問題 No.1221 木 *= 3
ユーザー firiexp
提出日時 2020-09-04 22:18:07
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 1,450 bytes
コンパイル時間 1,163 ms
コンパイル使用メモリ 111,420 KB
最終ジャッジ日時 2025-01-14 05:54:50
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:23:29: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   23 |     for (auto &&i : A) scanf("%d", &i);
      |                        ~~~~~^~~~~~~~~~
main.cpp:24:29: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   24 |     for (auto &&i : B) scanf("%d", &i);
      |                        ~~~~~^~~~~~~~~~
main.cpp:29:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   29 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;
template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;
int main() {
int n;
cin >> n;
vector<int> A(n), B(n), num(n);
for (auto &&i : A) scanf("%d", &i);
for (auto &&i : B) scanf("%d", &i);
vector<ll> dp1(n, 0), dp2(n, 0);
vector<vector<int>> G(n);
for (int i = 0; i < n-1; ++i) {
int a, b;
scanf("%d %d", &a, &b);
a--; b--;
G[a].emplace_back(b);
G[b].emplace_back(a);
}
deque<int> Q;
{
stack<int> s;
int cnt = 0;
vector<int> visited(n, 0);
s.emplace(0);
while(!s.empty()){
int a = s.top(); s.pop();
visited[a]++;
num[a] = cnt++;
Q.emplace_front(a);
for (auto &&i : G[a]) {
if(!visited[i]) s.emplace(i);
}
}
}
for (auto &&i : Q) {
dp1[i] = A[i];
for (auto &&j : G[i]) {
if(num[i] < num[j]) {
dp1[i] += max(dp1[j], dp2[j]);
dp2[i] += max(dp1[j], dp2[j]+B[i]+B[j]);
}
}
}
cout << max(dp1[0], dp2[0]) << "\n";
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0