結果

問題 No.439 チワワのなる木
ユーザー nanophoto12nanophoto12
提出日時 2016-11-05 12:09:25
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,975 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 93,676 KB
実行使用メモリ 30,368 KB
最終ジャッジ日時 2023-08-16 13:50:51
合計ジャッジ時間 3,184 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
26,796 KB
testcase_01 WA -
testcase_02 AC 11 ms
26,784 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 64 ms
27,464 KB
testcase_25 AC 83 ms
30,368 KB
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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)];
    }
};

bool cw[1000001];
vector<int> v[1000001];

int main()
{
    int n;
    cin >> n;
    string ss;
    cin >> ss;
    for(int i = 0;i < n;i++)
    {
        if(ss[i] == 'c')
        {
            cw[i] = true;
        }
        else
        {
            cw[i] = false;
        }
    }
    vector<int> start;
    for(int i = 0;i < n-1;i++)
    {
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        bool f = cw[a];
        bool t = cw[b];
        
        if(f)
        {
            start.push_back(a);
            if(!t)
            {
                v[a].push_back(b);
            }
        }
        else if(t)
        {
            start.push_back(b);
            v[b].push_back(a);
        }
        else
        {
            v[a].push_back(b);
            v[b].push_back(a);
        }
    }
    ll sum = 0;
    for(auto e : start)
    {
        const vector<int>& second = v[e];
        for(auto t : second)
        {
            sum += v[t].size();
        }
    }
    cout << sum << endl;
    return 0;
}
0