Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 13277 - XOR Path
Source : UVA Online Judge
Category : Linear Algebra, Graph Theory
Algorithm : Fast Walsh-Hadamard transform, dfs
Verdict : Accepted
- #include <bits/stdc++.h>
-
- using namespace std;
-
- #define ll long long
-
- static const int maxn = 1e5 + 5;
- static const int N = 1<<16;
-
- template <typename T> struct FWT
- {
- void fwt(T io[], int n)
- {
- for (int d = 1; d < n; d <<= 1)
- {
- for (int i = 0, m = d<<1; i < n; i += m)
- {
- for (int j = 0; j < d; j++)
- {
- T x = io[i+j], y = io[i+j+d];
- io[i+j] = (x+y), io[i+j+d] = (x-y);
-
-
- }
- }
- }
- }
- void ufwt(T io[], int n)
- {
- for (int d = 1; d < n; d <<= 1)
- {
- for (int i = 0, m = d<<1; i < n; i += m)
- {
- for (int j = 0; j < d; j++)
- {
- T x = io[i+j], y = io[i+j+d];
- io[i+j] = (x+y)>>1, io[i+j+d] = (x-y)>>1;
-
-
- }
- }
- }
- }
- void convolution(T a[], T b[], int n)
- {
- fwt(a, n);
- fwt(b, n);
- for (int i = 0; i < n; i++) a[i] = a[i] * b[i];
- ufwt(a, n);
- }
- void self_convolution(T a[], int n)
- {
- fwt(a, n);
- for (int i = 0; i < n; i++) a[i] = a[i] * a[i];
- ufwt(a, n);
- }
- };
-
- struct node
- {
- int v, w;
- node() {}
- node(int v, int w)
- {
- this->v = v;
- this->w = w;
- }
- };
-
- vector <node> graph[maxn];
- FWT <ll> fwt;
- ll arr[N+5];
-
- void dfs(int u = 1, int p = -1, int x = 0)
- {
- arr[x]++;
- for (auto &it : graph[u])
- {
- int v = it.v;
- int w = it.w;
- if (v == p) continue;
- dfs(v, u, x^w);
- }
- }
-
- int main()
- {
-
-
- int tc;
- scanf("%d", &tc);
- for (int tcase = 1; tcase <= tc; tcase++)
- {
- int tnode;
- scanf("%d", &tnode);
- for (int e = 1; e < tnode; e++)
- {
- int u, v, w;
- scanf("%d %d %d", &u, &v, &w);
- graph[u].push_back(node(v, w));
- graph[v].push_back(node(u, w));
- }
- dfs();
- fwt.self_convolution(arr, N);
- ll zero = (arr[0] - tnode) >> 1;
- printf("Case %d:\n", tcase);
- printf("%lld\n", zero);
- for (int i = 1; i < (1<<16); i++) printf("%lld\n", arr[i]>>1);
- for (int i = 0; i < maxn; i++) graph[i].clear();
- fill(begin(arr), end(arr), 0);
- }
- }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.