結果
| 問題 |
No.439 チワワのなる木
|
| ユーザー |
vjudge1
|
| 提出日時 | 2024-12-22 15:29:08 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 60 ms / 5,000 ms |
| コード長 | 974 bytes |
| コンパイル時間 | 1,727 ms |
| コンパイル使用メモリ | 170,092 KB |
| 実行使用メモリ | 18,944 KB |
| 最終ジャッジ日時 | 2024-12-22 15:29:12 |
| 合計ジャッジ時間 | 3,789 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
#include <bits/stdc++.h>
#define int long long
using namespace std;
void file_IO(){
freopen("cww.in", "r", stdin);
freopen("cww.out", "w", stdout);
}
const int N = 1e5 + 10;
int n, cnt = 0, ans = 0, ch[N], sz[N][2];
vector<int> g[N];
void dfs(int u, int fno){
sz[u][ch[u] ^ 1] = 0, sz[u][ch[u]] = 1;
for (auto v : g[u])
if (v ^ fno){
dfs(v, u);
for (int x : {0, 1}) sz[u][x] += sz[v][x];
if (ch[u]) ans += sz[v][0] * (cnt - 1 - sz[v][1]);
}
if (ch[u]) ans += (n - cnt - sz[u][0]) * (sz[u][1] - 1);
}
signed main(){
//file_IO();
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++){
char x; cin >> x;
ch[i] = (x == 'w' ? 1 : 0), cnt += ch[i];
}
for (int i = 1, u, v; i < n; i++){
cin >> u >> v;
g[u].push_back(v), g[v].push_back(u);
}
dfs(1, 0);
cout << ans << endl;
return 0;
}
vjudge1