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)