Hdu3635 dragon bils (with permission and query set)

Source: Internet
Author: User
1/* 2 question: there are n cities, and each city has a Dragon Ball (the same number as the city number). There are two operations: 3 t, b. Move all the dragon beads in the city marked with "A" to the city where "B" is located! 4 5. Train of Thought: and query the set. (when compressing the path, update the number of longzhu mobile operations) 6 */7 # include <iostream> 8 # include <cstring> 9 # include <cstdio> 10 # include <algorithm> 11 # define M 1000512 using namespace STD; 13 14 int f [m]; // indicates the city number of longzhu I. The value 15 int tcnt [m]; // records the number of times each longzhu moves. 16 int SCNT [m]; // record the total number of Dragon beads in each city: 17 18 int getfather (int x) {19 if (x = f [x]) 20 return X; 21 22 int FF = getfather (F [x]); 23 tcnt [x] + = tcnt [f [x]; // the number of times each Dragon Ball moves + = the number of times its parent Dragon Ball moves 24 f [x] = ff; 25 R Eturn f [X]; 26} 27 28 void Union (int A, int B) {29 int fa = getfather (a); 30 int Fb = getfather (B ); 31 if (FA = FB) return; 32 F [fa] = FB; 33 SCNT [FB] + = SCNT [fa]; // move all the dragon beads in the FA city to the FB city! 34 SCNT [fa] = 0; 35 tcnt [fa] + = 1; // number of a ball moves + 1 36} 37 38 int main () {39 int T,, b; 40 int n, m; 41 char ch [2]; 42 scanf ("% d", & T); 43 for (INT cc = 1; CC <= T; ++ CC) {44 printf ("case % d: \ n", CC); 45 scanf ("% d", & N, & M ); 46 memset (tcnt, 0, sizeof (INT) * (n + 1); 47 for (INT I = 1; I <= N; ++ I) 48 f [I] = I, SCNT [I] = 1; 49 while (M --) {50 scanf ("% s", CH ); 51 if (CH [0] = 'T') {52 scanf ("% d", & A, & B); 53 Union (A, B ); 54} 55 else {56 scanf ("% d", & A); 57 int FF = getfather (a); 58 printf ("% d \ n ", FF, SCNT [ff], tcnt [a]); 59} 60} 61} 62 Return 0; 63}

 

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.