Uvalive 3027 dedicated ative Network

Source: Internet
Author: User

SOL: it is still a collection of water and water. However, we need to maintain a DIST array here (Dist is the distance from each node to its father's day point. It can be maintained during path compression !!!)




# Include <cstdio> # include <algorithm> # include <cmath> using namespace STD; const int maxn = 20000 + 10; int pa [maxn], DIST [maxn]; inline int findset (int x) {If (PA [x]! = X) {int root = findset (PA [x]); Dist [x] + = DIST [PA [x]; return pa [x] = root ;} else return X;} int main () {int t; scanf ("% d", & T); While (t --) {int N, U, V; char cmd [10]; scanf ("% d", & N); For (INT I = 1; I <= N; I ++) {pa [I] = I; Dist [I] = 0;} while (~ Scanf ("% s", CMD) & cmd [0]! = 'O') {If (CMD [0] = 'E') {scanf ("% d", & U); findset (U ); printf ("% d \ n", DIST [u]);} If (CMD [0] = 'I') {scanf ("% d ", & U, & V); PA [u] = V; Dist [u] = ABS (u-v) % 1000 ;}} 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.