2015 programming beauty first round a color tree

Source: Internet
Author: User

Time limit:2000msSingle Point time limit:1000msMemory Limit:256MB
Describe

Given a tree of n nodes, the node number is 1, 2, ..., N. There is a n-1 edge in the tree, and there is exactly one path between any of the two nodes. This is a tree of color, each node can dye a color. Initially, all nodes have a color of 0. You now need to implement two actions:

1. Change the color of node x to Y;

2. Ask the whole tree how many trees are divided into the same color. That is, the nodes in each subtrees tree have the same color, and the neighboring subtree has different colors.

Input

The first line is an integer t, which represents the number of data groups, and the following is the T Group data.

The first row of each group of data is N, which represents the number of nodes in the tree. The next line of N-1 is two numbers I and j for each row, indicating that there is an edge between the nodes I and J. Next is a number Q, which represents the operand. After the Q line, each line represents one of the following two actions:

1. If "1", ask for the number of sub-trees divided.

2. For "2 x y", change the color of node x to Y.

Output

The first behavior of each group of data "case #X:", X is the test data number, starting from 1.

The next line, for each query, outputs an integer that is divided into the number of subtrees.

Data range

1≤t≤20

0≤y≤100000

Small Data

1≤n, q≤5000

Big Data

1≤n, q≤100000

Sample input
231 22 3312 2 1151 22 32 42 5412 2 12 3 21
Sample output
Case #1:13Case #2:15

2. Problem-Solving ideas: Today's game with DFS only a small data = =. When the tree degenerated into a chain, it would definitely be tle. Here's how to learn the AC code.

First set up the diagram, and then take 1 as the root into a root tree, at the time of conversion, the statistics of all nodes of different color of the number of sub-nodes. Because the color may be more, you can use map to store the number of different colors for each node. Let's consider what changes the color of the node I will bring.

First think about how the new subtree will be produced. (1) If node i is a leaf node, when all leaf nodes are color 0 o'clock, and the junction I is modified to color 1, will naturally produce more than one subtrees tree. We can calculate this new tree in this way: 0 of the leaves have num before the change, and there will be Num--。 Then the difference between the front and back is the number of new subtrees. This is the first source of a subtree. (2) If node I is an intermediate node, assume that it has a color of 0 and that it has NUM nodes with a color of 0. If I change the color of node I to 1, naturally we know that the new generation of num subtrees tree. At this point we can also get this value by comparing before and after: Modify the sub-node of the former node I 0 of the color of the cs[i][0] (Cs[i][j] is the node I color is the number of sub-nodes of J, that is, num==cs[i][0]), the node I color modified to 1, cs[i][1 ] is 0, then the newly created subtree is cs[i][0]-cs[i][1]. This is the second source of a subtree.

In conclusion, we find that the new subtree can always be obtained by changing the number of sub-nodes of the node I . However, for the second case, you should also pay attention to modify Fa[i] corresponding sub-node situation. Because the color of I modifies the condition that the Fa[i] node is affected.

The Place Worth Learning: (1) write the tree and diagram in advance to save the template, including Edge, record the father, record the number of sub-nodes and other basic operations. facilitate subsequent processing. (2) Observe the change and the constant after performing an operation. It is often more efficient to calculate the result by mathematical deduction.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define ME (x) memset (x,0,sizeof (x)) # Define SY System ("pause") #define MAXN 100005using namespace Std;struct edge{int to, NX;}; Edge ES[MAXN * 2];//edge set int ST[MAXN], en;//en indicates the number of edges, st[x] is the head node of the list map<int, int> cs[maxn];//cs[i][j] The number of the node that represents the node I is the root and the color is J. int N, q, Ans;int FA[MAXN], Color[maxn];//fa[i] represents the parent node of node I, Color[i] represents the node i's color void d__add (int x, int y) { Edge e;e.to = Y;e.nx = St[x];es[++en] = e;st[x] = en;} void Add (int x, int y)//plus edge operation {d__add (x, y);d __add (y, x);} void Dfs (int x)//No root tree is converted to a root tree with x root {int I, tot = 0;for (i = st[x]; i; i = es[i].nx) if (es[i].to! = Fa[x]) {fa[es[i].to] = X;to t++;d FS (es[i].to);}cs[x][0] = tot;//son node number}void change (int x, int y)//Modify the color of node x to Y{if (color[x] = = Color[fa[x]]) ans++;//assume that the color of the parent-child node will be different after the modification. Pre-add 1ans + = cs[x][color[x]];//First Add all the original color of the son node number if (Fa[x])//If the parent node of x is present, update fa[x] child node condition {cs[fa[x]][color[x]]--;// The reduction of the color of color[x] in the child nodes of the parent node is one cs[fa[x]][y]++;//the color of y increases by one}color[x] = y;//modifies the color if (color[x] = = Color[fa[x]]) ans--;// If the modified color is consistent with the parent node's color, the result is minus one ans-= cs[x][color[x]];///minus all the current colors of the son node number}void solve (int cas) {int I, A, b;scanf ("%d", &n); Me (ST); En = 0;for (i = 1; i<n; i++) {scanf ("%d%d", &a, &b); Add (A, b);} for (i = 1; I <= n; i++) cs[i].clear (); fa[1] = 0; Me (color); Color[0] = -10000097;dfs (1); Ans = 1;scanf ("%d", &q);p rintf ("Case #%d:\n", CAs), for (i = 0; i<q; i++) {scanf ("%d", &a), if (a = = 1) printf ("%d \ n ", ans); ELSE{SCANF ("%d%d ", &a, &b); Change (a, b);}} int main () {//freopen ("T.txt", "R", stdin), int T, i;scanf ("%d", &t), for (i = 1; I <= t; i++) solve (i); return 0;}



2015 programming beauty first round a color tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.