Submission #1161101


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf=1e9;
const int64_t inf64=1e18;
const double eps=1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
    	os << v << ",";
    }
    os << "]";
    return os;
}

using i64=int64_t;

template<class graph_type> int compute_subtree_size(const graph_type &graph,const vector<bool> &centroid,vector<int> &subtree_size,int u,int p){
    int c=1;
    for(const auto &edge:graph[u]){
        int v=edge.to;
        if(v==p or centroid[v]) continue;
        c+=compute_subtree_size(graph,centroid,subtree_size,v,u);
    }
    subtree_size[u]=c;
    return c;
}

//tは連結成分全体のサイズ
//u以下で,削除により残る最大の部分木の頂点数が最小となる頂点の,(残る最大の部分木の頂点数,頂点番号)のペアを返す
template<class graph_type> pair<int,int> search_centroid(const graph_type &graph,const vector<bool> &centroid,const vector<int> &subtree_size,int u,int p,int t){
    pair<int,int> res=make_pair(inf,-1);
    int s=1,m=0;
    for(const auto &edge:graph[u]){
        int v=edge.to;
        if(v==p or centroid[v]) continue;
        res=min(res,search_centroid(graph,centroid,subtree_size,v,u,t));
        m=max(m,subtree_size[v]);
        s+=subtree_size[v];
    }
    m=max(m,t-s);
    res=min(res,make_pair(m,u));
    return res;
}

struct edge{
    int to,cost;
};

void solve(){
    int n,x;
    cin >> n >> x;
    vector<vector<edge>> graph(n);
    rep(i,0,n-1){
        int x,y,c;
        cin >> x >> y >> c;
        --x;
        --y;
        graph[x].push_back(edge({y,c}));
        graph[y].push_back(edge({x,c}));
    }

    map<int,i64> count;
    function<void(int,int,int)> dfs=[&](int u,int p,int s){
        ++count[s];
        for(edge &e:graph[u]){
            if(e.to==p) continue;
            dfs(e.to,u,s^e.cost);
        }
    };
    dfs(0,-1,0);
    i64 ans=0;
    for(auto &p:count){
        if(p.first==(p.first^x)) ans+=(p.second-1)*p.second/2;
        else ans+=p.second*count[p.first^x];
        count[p.first^x]=0;
    }
    cout << ans << endl;
}

int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(10);
    solve();
    return 0;
}

Submission Info

Submission Time
Task C - エックスオア多橋君
User walkre
Language C++14 (GCC 5.4.1)
Score 100
Code Size 2641 Byte
Status AC
Exec Time 118 ms
Memory 18816 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 27
Set Name Test Cases
Sample subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt
All 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 AC 1 ms 256 KB
subtask0_sample_02.txt AC 1 ms 256 KB
subtask0_sample_03.txt AC 1 ms 256 KB
subtask1_01.txt AC 1 ms 256 KB
subtask1_02.txt AC 2 ms 384 KB
subtask1_03.txt AC 116 ms 18816 KB
subtask1_04.txt AC 116 ms 18816 KB
subtask1_05.txt AC 118 ms 18816 KB
subtask1_06.txt AC 38 ms 15104 KB
subtask1_07.txt AC 47 ms 6272 KB
subtask1_08.txt AC 46 ms 6272 KB
subtask1_09.txt AC 65 ms 7296 KB
subtask1_10.txt AC 66 ms 7296 KB
subtask1_11.txt AC 1 ms 384 KB
subtask1_12.txt AC 1 ms 384 KB
subtask1_13.txt AC 52 ms 6400 KB
subtask1_14.txt AC 49 ms 6272 KB
subtask1_15.txt AC 9 ms 1536 KB
subtask1_16.txt AC 9 ms 1536 KB
subtask1_17.txt AC 9 ms 1664 KB
subtask1_18.txt AC 9 ms 1536 KB
subtask1_19.txt AC 9 ms 1536 KB
subtask1_20.txt AC 9 ms 1536 KB
subtask1_21.txt AC 9 ms 1664 KB
subtask1_22.txt AC 9 ms 1536 KB
subtask1_23.txt AC 9 ms 1536 KB
subtask1_24.txt AC 9 ms 1536 KB