UVa 1329-corporative Network Union Find

Source: Internet
Author: User

The problem with UVA is that the application of data structure is the application of union find and check set. Use path compression primarily. You don't even need to merge the tree because there are no duplicate lines and modifications to the single-parent node operation.

Depressed is not too familiar with this OJ system, incredibly use the ABS in the library will WA, wrote an ABS small function on the past.

Title: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 4075

#include <stdio.h>const int max_n = 20005;const int MOD = 1000;//in UVA using the library ABS incredibly WA, wasted a lot of time inline int abs (int a) {retur N A < 0? -A:A; }struct Subset{int P, W;}; Subset Subs[max_n];int findparent (int x) {if (SUBS[X].P! = x) {int p = SUBS[X].P;SUBS[X].P = Findparent (SUBS[X].P); Subs[x]. W = (SUBS[X].W + SUBS[P].W);} return SUBS[X].P;} void initsubs (int N) {for (int i = 1; I <= N; i++) {subs[i].p = I;SUBS[I].W = 0;}} int main () {int T, N, I, J;char cmd;scanf ("%d", &t), while (t--) {scanf ("%d", &n), GetChar (); Initsubs (n); while (cmd = GetChar ()) && cmd! = ' O ') {if (cmd = = ' E ') {scanf ("%d", &i), SUBS[I].P = Findparent (i);p rintf ("%d\n", SUBS[I].W );} ELSE{SCANF ("%d%d", &i, &j), SUBS[I].W = (ABS (j-i))%MOD;SUBS[I].P = j;//There is no duplicate connection and changes to the parent, therefore directly connected to Ok}getchar ();}} return 0;}

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.