Uvalive-3027corporative Network (with right and check set)

Source: Internet
Author: User

Title: Uvalive-3027corporative Network (with right and check set)


The main topic: There are N and nodes, at the beginning of each node's parent node does not exist, and then there are two kinds of operations: I operation I, A and a, the parent of a will become B. E operates e A and queries the distance of a to its parent node.


Problem-solving ideas: Take the right and check the set. Note that the distance here is a-B, so the distance from a to the root node is the absolute value of the distance from A to B, and the distance from B to its root node.


Code:

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 2e5 + 5;int P[MAXN], C[maxn];int n;void init () {for (int i = 1; I <= n; i++) {p[i] = i;c[i] = 0;}} int getParent (int a) {if (a = = P[a]) return a;int t = p[a];p [A] = GetParent (P[a]); C[a] + = C[t];return P[a];}  int main () {int t;int A, B;char str[10];scanf ("%d", &t), while (t--) {scanf ("%d", &n); init (); while (scanf ("%s", str) = EOF) {if (str[0] = = ' O ') break;if (str[0] = = ' E ') {scanf ("%d", &a), int q1 = getParent (a);p rintf ("%d\n", C[a ]);} else {scanf ("%d%d", &a, &b);p [A] = B;c[a] = ABS (A-B)% 1000;}} return 0;}

Uvalive-3027corporative Network (with right and check set)

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.