結果
| 問題 |
No.2949 Product on Tree
|
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2024-11-02 14:53:12 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 201 ms / 2,000 ms |
| コード長 | 2,119 bytes |
| コンパイル時間 | 1,106 ms |
| コンパイル使用メモリ | 57,268 KB |
| 最終ジャッジ日時 | 2025-02-25 02:05:28 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 46 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:71:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
71 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
main.cpp:72:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
72 | for (int i = 0; i < n; i++) scanf("%d", as + i);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:75:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
75 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*-
*
* 2949.cc: No.2949 Product on Tree - yukicoder
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
/* constant */
const int MAX_N = 200000;
const int MOD = 998244353;
/* typedef */
using vi = vector<int>;
template<const int MOD>
struct MI {
int v;
MI(): v() {}
MI(int _v): v(_v % MOD) { if (v < 0) v += MOD; }
MI(long long _v): v(_v % MOD) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
MI operator+(const MI m) const { return MI(v + m.v); }
MI operator-(const MI m) const { return MI(v + MOD - m.v); }
MI operator*(const MI m) const { return MI((long long)v * m.v); }
MI &operator+=(const MI m) { return (*this = *this + m); }
MI &operator-=(const MI m) { return (*this = *this - m); }
MI &operator*=(const MI m) { return (*this = *this * m); }
bool operator==(const MI m) const { return v == m.v; }
bool operator!=(const MI m) const { return v != m.v; }
MI pow(int n) const { // a^n % MOD
MI pm = 1, a = *this;
while (n > 0) {
if (n & 1) pm *= a;
a *= a;
n >>= 1;
}
return pm;
}
MI inv() const { return pow(MOD - 2); }
MI operator/(const MI m) const { return *this * m.inv(); }
MI &operator/=(const MI m) { return (*this = *this / m); }
};
using mi = MI<MOD>;
/* global variables */
int as[MAX_N];
vi nbrs[MAX_N];
int ps[MAX_N], cis[MAX_N];
mi dp[MAX_N];
/* subroutines */
/* main */
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", as + i);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
nbrs[u].push_back(v);
nbrs[v].push_back(u);
}
mi sum = 0;
ps[0] = -1;
for (int u = 0; u >= 0;) {
auto &nbru = nbrs[u];
int up = ps[u];
if (cis[u] == 0) dp[u] = as[u];
if (cis[u] < nbru.size()) {
int v = nbru[cis[u]++];
if (v != up) {
ps[v] = u;
u = v;
}
}
else {
if (up >= 0) {
sum += dp[up] * dp[u];
dp[up] += dp[u] * as[up];
}
u = up;
}
}
printf("%d\n", (int)sum);
return 0;
}
tnakao0123