[2016-03-19] [Uvalive] [3027] [Corporative Network]

Source: Internet
Author: User

    • Time: 2016-03-19-13:24:23 Saturday
    • Title Number: [2016-03-19][uvalive][3027][corporative Network]
    • The main topic: given n nodes, I u v means the parent node of the U node is set to V, the distance is |u-v|%1000,e u to ask u to the root node distance, given a number of I-E operations, output the corresponding answer
    • Analysis: Take right and check the set
    • Method: D[MAXN] Maintenance to the parent node distance, each time the query, the current node compression to the root node output corresponding distance can be
  
 
  1. #ifdef _WORK_
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include<cstring>
  5. using namespace std;
  6. #define CLR(x,y) memset((x),(y),sizeof((x)))
  7. #define for ( x y z ) for ( int ( x ) = ( y );( x z x
  8. const int maxn = 20000 +10;
  9. int fa[maxn],d[maxn];
  10. inline void ini(int n){
  11. CLR(d,0);
  12. FOR(i,0,n+1) fa[i] = i;
  13. }
  14. int fnd(int x){
  15. if(x == fa[x]) return x;
  16. int tmp = fa[x];
  17. fa[x] = fnd(fa[x]);
  18. d[x] = d[x] + d[tmp];
  19. return fa[x];
  20. }
  21. int main(){
  22. int t,n,p,q;
  23. char str[10];
  24. scanf("%d",&t);
  25. while(t--){
  26. scanf("%d",&n);
  27. ini(n);
  28. while(~scanf("%s",str) && str[0] != ‘O‘){
  29. if(str[0] == ‘I‘){
  30. scanf("%d%d",&p,&q);
  31. fa[p] = q;
  32. d[p] = abs(p - q)%1000;
  33. }else{
  34. scanf("%d",&p);
  35. fnd(p);
  36. printf("%d\n",d[p]);
  37. }
  38. }
  39. }
  40. return 0;
  41. }


From for notes (Wiz)

[2016-03-19] [Uvalive] [3027] [Corporative Network]

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.