Submission #524149


Source Code Expand

Copy
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <fstream>
#include <bitset>

using namespace std;

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
template <typename T> void print_container(ostream& os, const T& c) { const char* _s = " "; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) os << _s; } } }
template <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; }
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; }
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; }

template <typename T> void print(T a, int n, const string& split = " ") { for (int i = 0; i < n; i++) { cout << a[i]; if (i + 1 != n) cout << split; } cout << endl; }
template <typename T> void print2d(T a, int w, int h, int width = -1, int br = 0) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }
template <typename T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
#define dump(v) (cout << #v << ": " << v << endl)

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define clr(a, x) memset(a, x, sizeof(a))
#define sz(a) ((int)(a).size())
#define mp(a, b) make_pair(a, b)
#define ten(n) ((long long)(1e##n))

template <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }
template <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }
template <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }
template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }
template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }
void fast_io() { cin.tie(0); ios::sync_with_stdio(false); }
bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }

typedef long long ll;
typedef pair<int, int> pint;

const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };

#include <unordered_map>

vector<pint> g[114514];
int x;
ll res = 0;
void ippan_merge_teku(unordered_map<int, int>& a, unordered_map<int, int>& b)
{
    if (a.size() < b.size())
        swap(a, b);
    for (auto& it : b)
        a[it.first] += it.second;
}
unordered_map<int, int> dfs(int cur, int par, int sum)
{
    unordered_map<int, int> a;
    ++a[sum];
    for (auto& it : g[cur])
    {
        int to, c;
        tie(to, c) = it;
        if (to != par)
        {
            auto b = dfs(to, cur, sum ^ c);

            if (a.size() < b.size())
                swap(a, b);

            for (auto& bit : b)
                res += (ll)a[bit.first ^ x] * bit.second;

            ippan_merge_teku(a, b);
        }
    }
    return move(a);
}
int main()
{
    fast_io();

    int n;
    cin >> n >> x;
    rep(i, n - 1)
    {
        int x, y, c;
        cin >> x >> y >> c;
        --x, --y;
        g[x].push_back(pint(y, c));
        g[y].push_back(pint(x, c));
    }

    dfs(0, -1, 0);
    cout << res << endl;
}

Submission Info

Submission Time
Task C - エックスオア多橋君
User takapt
Language C++11 (GCC 4.9.2)
Score 100
Code Size 4307 Byte
Status
Exec Time 413 ms
Memory 42528 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt
All 100 / 100 subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask1_17.txt, subtask1_18.txt, subtask1_19.txt, subtask1_20.txt, subtask1_21.txt, subtask1_22.txt, subtask1_23.txt, subtask1_24.txt
Case Name Status Exec Time Memory
subtask0_sample_01.txt 31 ms 3492 KB
subtask0_sample_02.txt 30 ms 3480 KB
subtask0_sample_03.txt 29 ms 3484 KB
subtask1_01.txt 31 ms 3484 KB
subtask1_02.txt 31 ms 3484 KB
subtask1_03.txt 397 ms 18272 KB
subtask1_04.txt 413 ms 18288 KB
subtask1_05.txt 413 ms 18536 KB
subtask1_06.txt 179 ms 42528 KB
subtask1_07.txt 153 ms 7192 KB
subtask1_08.txt 149 ms 7196 KB
subtask1_09.txt 281 ms 11020 KB
subtask1_10.txt 277 ms 9860 KB
subtask1_11.txt 32 ms 3496 KB
subtask1_12.txt 31 ms 3616 KB
subtask1_13.txt 221 ms 7980 KB
subtask1_14.txt 220 ms 8232 KB
subtask1_15.txt 55 ms 4640 KB
subtask1_16.txt 54 ms 4724 KB
subtask1_17.txt 53 ms 4592 KB
subtask1_18.txt 55 ms 4644 KB
subtask1_19.txt 55 ms 4640 KB
subtask1_20.txt 55 ms 4644 KB
subtask1_21.txt 56 ms 4504 KB
subtask1_22.txt 57 ms 4640 KB
subtask1_23.txt 57 ms 4644 KB
subtask1_24.txt 54 ms 4524 KB