結果
| 問題 |
No.439 チワワのなる木
|
| コンテスト | |
| ユーザー |
nanophoto12
|
| 提出日時 | 2016-11-05 22:31:05 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 106 ms / 5,000 ms |
| コード長 | 2,091 bytes |
| コンパイル時間 | 1,146 ms |
| コンパイル使用メモリ | 93,480 KB |
| 実行使用メモリ | 45,696 KB |
| 最終ジャッジ日時 | 2024-11-25 03:13:11 |
| 合計ジャッジ時間 | 3,224 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
#include <cmath>
#include <vector>
#include <map>
#include <functional>
#include <queue>
#include <iostream>
#include <string.h>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <cstdint>
#include <climits>
#include <unordered_set>
#include <sstream>
using namespace std;
#define ll long long int
#define ti4 tuple<int,int,int,int>
#define pii pair<ll,ll>
#define REP(x,n) for(int x = 0;x < n;x++)
struct UnionFind {
vector<ll> data;
UnionFind(ll size) : data(size, -1) { }
bool unionSet(ll x, ll y) {
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
bool findSet(ll x, ll y) {
return root(x) == root(y);
}
ll root(ll x) {
return data[x] < 0 ? x : data[x] = root(data[x]);
}
ll size(ll x) {
return -data[root(x)];
}
};
#define MAX_COUNT 1000001
string ss;
vector<int> g[MAX_COUNT];
int cc[MAX_COUNT];
int cw[MAX_COUNT];
ll ret;
ll tc;
ll tw;
void dfs(int current, int prev)
{
if(ss[current] == 'w') cw[current]=1;
else if(ss[current] == 'c') cc[current]=1;
for(auto e : g[current])
{
if(e == prev)
{
continue;
}
dfs(e, current);
cw[current] += cw[e];
cc[current] += cc[e];
if(ss[current] == 'w')
{
ret += cc[e] * (tw-1-cw[e]);
}
}
if(prev >= 0 && ss[current] == 'w')
{
ret += (tc-cc[current]) * (cw[current]-1);
}
}
int main()
{
ret = 0;
tc = tw = 0;
fill(cc, cc+MAX_COUNT,0);
fill(cw, cw+MAX_COUNT,0);
int n;
cin >> n;
cin >> ss;
for(auto r : ss)
{
if(r == 'w')
{
tw++;
}
else
{
tc++;
}
}
for(int i = 0;i < n-1;i++)
{
int a,b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(0,-1);
cout << ret << endl;
return 0;
}
nanophoto12