Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static const int maxn = 1e5 + 5;
- vector <int> graph[maxn];
- int color[maxn], subtreeSize[maxn], cnt[maxn];
- bool big[maxn];
- // Calculate size of the subtree of every vertices
- void dfs(int u, int p = -1)
- {
- subtreeSize[u] = 1; // Every vertex has itself in its subtree
- for (int v : graph[u])
- {
- if (v == p)
- continue;
- dfs(v, u);
- subtreeSize[u] += subtreeSize[v]; // Add size of child v to its parent(v)
- }
- }
- void add(int u, int p, int x)
- {
- cnt[ color[u] ] += x;
- for (int v : graph[u])
- {
- if (v != p || big[v])
- continue;
- add(v, u, x);
- }
- }
- void dsu(int u, int p, bool keep)
- {
- int mx(-1), bigChild(-1);
- for (int v : graph[u])
- {
- if (v == p)
- continue;
- if (subtreeSize[v] > mx)
- {
- mx = subtreeSize[v];
- bigChild = v;
- }
- }
- for (int v : graph[u])
- {
- if (v == p || v == bigChild)
- continue;
- dsu(v, u, 0); // Run a dfs from small child and clear them from cnt
- }
- if (bigChild != -1)
- {
- dsu(bigChild, u, 1);
- big[bigChild] = 1; // bigChild marked as big and not cleared from cnt
- }
- add(u, p, 1);
- // cnt[c] is the number of vertices in subtree of vertex u that has color c.
- if (bigChild != -1)
- big[bigChild] = 0;
- if (keep == 0)
- add(u, p, -1); // remove
- } static const int maxn = 1e5 + 5;
- vector <int> graph[maxn];
- int color[maxn], subtreeSize[maxn], cnt[maxn];
- bool big[maxn];
- // Calculate size of the subtree of every vertices
- void dfs(int u, int p = -1)
- {
- subtreeSize[u] = 1; // Every vertex has itself in its subtree
- for (int v : graph[u])
- {
- if (v == p)
- continue;
- dfs(v, u);
- subtreeSize[u] += subtreeSize[v]; // Add size of child v to its parent(v)
- }
- }
- void add(int u, int p, int x)
- {
- cnt[ color[u] ] += x;
- for (int v : graph[u])
- {
- if (v != p || big[v])
- continue;
- add(v, u, x);
- }
- }
- void dsu(int u, int p, bool keep)
- {
- int mx(-1), bigChild(-1);
- for (int v : graph[u])
- {
- if (v == p)
- continue;
- if (subtreeSize[v] > mx)
- {
- mx = subtreeSize[v];
- bigChild = v;
- }
- }
- for (int v : graph[u])
- {
- if (v == p || v == bigChild)
- continue;
- dsu(v, u, 0); // Run a dfs from small child and clear them from cnt
- }
- if (bigChild != -1)
- {
- dsu(bigChild, u, 1);
- big[bigChild] = 1; // bigChild marked as big and not cleared from cnt
- }
- add(u, p, 1);
- // cnt[c] is the number of vertices in subtree of vertex u that has color c.
- if (bigChild != -1)
- big[bigChild] = 0;
- if (keep == 0)
- add(u, p, -1); // remove
- } static const int maxn = 1e5 + 5;
- vector <int> graph[maxn];
- int color[maxn], subtreeSize[maxn], cnt[maxn];
- bool big[maxn];
- // Calculate size of the subtree of every vertices
- void dfs(int u, int p = -1)
- {
- subtreeSize[u] = 1; // Every vertex has itself in its subtree
- for (int v : graph[u])
- {
- if (v == p)
- continue;
- dfs(v, u);
- subtreeSize[u] += subtreeSize[v]; // Add size of child v to its parent(v)
- }
- }
- void add(int u, int p, int x)
- {
- cnt[ color[u] ] += x;
- for (int v : graph[u])
- {
- if (v != p || big[v])
- continue;
- add(v, u, x);
- }
- }
- void dsu(int u, int p, bool keep)
- {
- int mx(-1), bigChild(-1);
- for (int v : graph[u])
- {
- if (v == p)
- continue;
- if (subtreeSize[v] > mx)
- {
- mx = subtreeSize[v];
- bigChild = v;
- }
- }
- for (int v : graph[u])
- {
- if (v == p || v == bigChild)
- continue;
- dsu(v, u, 0); // Run a dfs from small child and clear them from cnt
- }
- if (bigChild != -1)
- {
- dsu(bigChild, u, 1);
- big[bigChild] = 1; // bigChild marked as big and not cleared from cnt
- }
- add(u, p, 1);
- // cnt[c] is the number of vertices in subtree of vertex u that has color c.
- if (bigChild != -1)
- big[bigChild] = 0;
- if (keep == 0)
- add(u, p, -1); // remove
- } static const int maxn = 1e5 + 5;
- vector <int> graph[maxn];
- int color[maxn], subtreeSize[maxn], cnt[maxn];
- bool big[maxn];
- // Calculate size of the subtree of every vertices
- void dfs(int u, int p = -1)
- {
- subtreeSize[u] = 1; // Every vertex has itself in its subtree
- for (int v : graph[u])
- {
- if (v == p)
- continue;
- dfs(v, u);
- subtreeSize[u] += subtreeSize[v]; // Add size of child v to its parent(v)
- }
- }
- void add(int u, int p, int x)
- {
- cnt[ color[u] ] += x;
- for (int v : graph[u])
- {
- if (v != p || big[v])
- continue;
- add(v, u, x);
- }
- }
- void dsu(int u, int p, bool keep)
- {
- int mx(-1), bigChild(-1);
- for (int v : graph[u])
- {
- if (v == p)
- continue;
- if (subtreeSize[v] > mx)
- {
- mx = subtreeSize[v];
- bigChild = v;
- }
- }
- for (int v : graph[u])
- {
- if (v == p || v == bigChild)
- continue;
- dsu(v, u, 0); // Run a dfs from small child and clear them from cnt
- }
- if (bigChild != -1)
- {
- dsu(bigChild, u, 1);
- big[bigChild] = 1; // bigChild marked as big and not cleared from cnt
- }
- add(u, p, 1);
- // cnt[c] is the number of vertices in subtree of vertex u that has color c.
- if (bigChild != -1)
- big[bigChild] = 0;
- if (keep == 0)
- add(u, p, -1); // remove
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement