Submission #524095


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);

            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 0
Code Size 4238 Byte
Status
Exec Time 2036 ms
Memory 42540 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 0 / 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 28 ms 3484 KB
subtask0_sample_02.txt 27 ms 3488 KB
subtask0_sample_03.txt 27 ms 3480 KB
subtask1_01.txt 27 ms 3492 KB
subtask1_02.txt 28 ms 3616 KB
subtask1_03.txt 2034 ms 13248 KB
subtask1_04.txt 2035 ms 15860 KB
subtask1_05.txt 2036 ms 16004 KB
subtask1_06.txt 176 ms 42540 KB
subtask1_07.txt 192 ms 7200 KB
subtask1_08.txt 193 ms 7204 KB
subtask1_09.txt 2034 ms 11148 KB
subtask1_10.txt 2033 ms 9800 KB
subtask1_11.txt 29 ms 3616 KB
subtask1_12.txt 31 ms 3612 KB
subtask1_13.txt 855 ms 8008 KB
subtask1_14.txt 838 ms 8252 KB
subtask1_15.txt 193 ms 5008 KB
subtask1_16.txt 229 ms 5080 KB
subtask1_17.txt 289 ms 5200 KB
subtask1_18.txt 308 ms 5044 KB
subtask1_19.txt 257 ms 4996 KB
subtask1_20.txt 288 ms 5032 KB
subtask1_21.txt 328 ms 4952 KB
subtask1_22.txt 243 ms 4996 KB
subtask1_23.txt 206 ms 4988 KB
subtask1_24.txt 316 ms 4968 KB