Snnu (1110) transmission network (and check set + path compression + offline operation)

Source: Internet
Author: User

1110: Transmission Network time limit:3 Sec Memory limit:512 MB
submit:43 solved:18
[Submit] [Status] [Web Board] [Edit] Description

Byteland country's network one-way transmission system can be regarded as the capital Bytetown as the center of the direction of the tree, at first only Bytetown built a base station, all the other city signals are transmitted from the Bytetown. Now they are starting to build new base stations in other cities, and command "C X" to create a new base station in the city X, not to build multiple base stations in the same city, with the city numbered from 1 to N, of which the city 1 is the capital Bytetown. In the process of building the base station, they will also ask the city where the network signal is transmitted from which city, and the command "Q X" represents the city where the city X is queried.

Input

Input has multiple sets of test data. The first row of each group of data contains two positive integers n and m (1 <= n,m <= 100,000), respectively, representing the number of cities and the number of commands. Next n-1 lines, two positive integers per line, u and V, represent a network transport channel from city u to City v. After the M line, each line is a command "C x" or "Q X".
The sum of all inputs N and M is not more than 500,000, and the two sets of input data are separated by a blank line.

Output

For each query command, output an integer y that represents the source city. Each of the two sets of data is separated by a space.

Sample Input
3 3Q 3C 2Q 2Q 3
Sample Output
122
HINT Source

2015 College student Program design competition in Shaanxi Province

Topic: Given a relationship tree, there are two operations, one is to inquire about the root node of the tree, and one is to divide the relationship tree.

can use and check the set, because the tree may degenerate into a chain, each query may query the entire chain, consider path compression, but there may be a C operation later,

For this kind of problem, a large number of inquiry operations and segmentation operations, cross, similar to the previous one, to find the sum of the different numbers in an interval, all read into the query operation,

Numbering, then answering in turn the query operation at the end, and then according to the number answer, the offline operation is mapped again, this problem assumes that read all the C operation, build a tree

The ultimate tree, seemingly unable to answer before the C operation of the query, is not possible?

We can do it backwards, answer the last query after C, then restore the tree to the State before C, answer again, and so on, restore the state of the tree to its original state,

For each state we can carry out a query with road compression, reducing the complexity of time.

#include <iostream>#include<cstdio>#include<string>#include<cstdlib>#include<algorithm>#include<vector>using namespacestd;#defineMAXN 110000intn,m;intQ[MAXN],C[MAXN];//The C array is numbered from the start to the end of the base stationintFATHER[MAXN];//signal launch point prior to base stationintUSET[MAXN];intRANK[MAXN];intANSWER[MAXN];//The answer to Q from the end to the beginningVector <int>G[MAXN];intCnum;intMM;voidinit () {Cnum=0; mm=0; memset (q,0,sizeof(q)); Memset (c,0,sizeof(c)); memset (father,0,sizeof(father)); memset (Answer,0,sizeof(answer));  for(intI=0; i<maxn;i++) G[i].clear ();}voidMakeSet () { for(intI=1; i<=n;i++) {Uset[i]=i; Rank[i]=0; }}intFind1 (intx) {    if(x!=Uset[x])     Find1 (Uset[x]); returnuset[x];}voidUnionset (intXinty) {    intfx=Find1 (x); intfy=Find1 (y); if(fx!=FY) {       if(rank[fx]>Rank[fy]) Uset[fy]=FX; if(rank[fx]<Rank[fy]) USET[FX]=fy; if(rank[fx]==Rank[fy]) {USET[FX]=fy; RANK[FX]++; }    }}intFind2 (intx) {   if(X!=uset[x]) uset[x]=Find2 (uset[x]); returnuset[x];}voidinput () { for(intI=1; i<=n-1; i++)    {        intFa,son; scanf ("%d%d",&fa,&son); Uset[son]=FA; }     for(intI=1; i<=m;i++)    {        Chars[Ten];intnum; scanf ("%s", s); if(s[0]=='Q') {scanf ("%d",&num);        G[cnum].push_back (num); }        if(s[0]=='C') {scanf ("%d",&num); g[++cnum].push_back (num); C[cnum]=num; Father[num]=Find1 (Uset[num]); Uset[num]=num;//Split Open        }    }     /*cout<< "Cnum" <<cnum<<endl;          for (int i=cnum;i>=0;i--) {printf ("%d", g[i].size ());             for (int j=g[i].size () -1;j>=0;j--) {printf ("%d", g[i][j]);     } printf ("\ n"); }*/}voidsolve () {//cout<< "Cnum" <<cnum<<endl;     for(inti=cnum;i>=0; i--)    {         if(i==0)         {             /*for (int j=g[i].size () -1;j>=1;j--) {printf ("%d", g[i][j]); } cout<<endl;*/           //cout<< "0" <<g[i].size () <<endl;00             for(intJ=g[i].size ()-1; j>=0; j--)             {                 intx=G[i][j]; ANSWER[MM++]=Find2 (x); //cout<< "0" <<G[i][j]<< "" <<answer[MM-1]<<endl;             }         }         Else         {            /*for (int j=g[i].size () -1;j>=1;j--) {printf ("%d", g[i][j]); } cout<<endl;*/              for(intJ=g[i].size ()-1; j>=1; j--)             {                 intx=G[i][j]; ANSWER[MM++]=Find2 (x); //cout<< "1" <<answer[MM-1]<<endl;             }           //cout<< "G[i][0" <<G[i][0]<<endl; //cout<< "father[g[i][0]" <<father[G[i][0]]<<endl;uset[g[i][0]]=father[g[i][0]]; }    }     for(inti=mm-1; i>=0; i--) printf ("%d\n", Answer[i]);}intMain () { while(~SCANF ("%d%d",&n,&m)) {init ();        MakeSet ();        Input ();    Solve (); }    return 0;}
View Code

Snnu (1110) transmission network (and check set + path compression + offline operation)

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.