多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > php教程 > (DS 《算法競賽入門經(jīng)典》)LA 3027 Corporative Network(查詢某一個(gè)節(jié)點(diǎn)到根節(jié)點(diǎn)之間的距離)

(DS 《算法競賽入門經(jīng)典》)LA 3027 Corporative Network(查詢某一個(gè)節(jié)點(diǎn)到根節(jié)點(diǎn)之間的距離)

來源:程序員人生   發(fā)布時(shí)間:2015-01-06 08:09:33 閱讀次數(shù):3438次

題目大意:

             查詢某1個(gè)節(jié)點(diǎn)到根節(jié)點(diǎn)之間的距離


解題思路:

            加權(quán)并查集問題。之前做的題目是“查看兩個(gè)或多個(gè)節(jié)點(diǎn)是不是在同1個(gè)集合下”,現(xiàn)在的題目是“查詢某個(gè)節(jié)點(diǎn)到

根節(jié)點(diǎn)之間的距離”。之前只需要使用到father[x]這個(gè)數(shù)組,用來表示x的父親節(jié)點(diǎn)是誰。現(xiàn)在引入dist[x]數(shù)組,用來記錄

x節(jié)點(diǎn)到根節(jié)點(diǎn)的距離

            1)在并查集中,根節(jié)點(diǎn)不懂,其他節(jié)點(diǎn)都可以動。



A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the services, the corporation started to collect some enterprises in clusters, each of them served by a single computing and telecommunication center as follow. The corporation chose one of the existing centers I (serving the cluster A) and one of the enterprises J in some cluster B (not necessarily the center) and link them with telecommunication line. The length of the line between the enterprises I and J is |I ? J|(mod 1000). In such a way the two old clusters are joined in a new cluster, served by the center of the old cluster B. Unfortunately after each join the sum of the lengths of the lines linking an enterprise to its serving center could be changed and the end users would like to know what is the new length. Write a program to keep trace of the changes in the organization of the network that is able in each moment to answer the questions of the users.

Input 

Your program has to be ready to solve more than one test case. The first line of the input file will contains only the number T of the test cases. Each test will start with the number N of enterprises (5≤N≤20000). Then some number of lines (no more than 200000) will follow with one of the commands:
E I ? asking the length of the path from the enterprise I to its serving center in the moment;
I I J ? informing that the serving center I is linked to the enterprise J.
The test case finishes with a line containing the word O. The I commands are less than N.

Output 

The output should contain as many lines as the number of E commands in all test cases with a single number each ? the asked sum of length of lines connecting the corresponding enterprise with its serving center.

Sample Input 

1
4
E 3
I 3 1
E 3
I 1 2
E 3
I 2 4
E 3
O

Sample Output 

0
2
3
5

代碼以下:

/* * LA3027.cpp * * Created on: 2015年1月3日 * Author: Administrator */ #include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 20005; int father[maxn]; int dist[maxn]; int find(int x) { if (x != father[x]) { int fx = find(father[x]); dist[x] += dist[father[x]]; father[x] = fx; } return father[x]; } int main() { int t; scanf("%d", &t); while (t--) { char cmd[9]; int n; scanf("%d", &n); int i; for (i = 1; i <= n; ++i) { father[i] = i; dist[i] = 0; } while (scanf("%s", cmd) == 1, cmd[0] != 'O') { if (cmd[0] == 'I') { int u, v; scanf("%d%d", &u, &v); father[u] = v; dist[u] = abs(u - v) % 1000; } else if (cmd[0] == 'E') { int u; scanf("%d", &u); find(u); printf("%d ", dist[u]); } } } return 0; }




生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 国产孕妇色xxxxx | 成年ssswww日本 | 国产精品亚洲一区二区三区 | 久久精品国产一区二区三区不卡 | yellow中文字幕官网是 | 国产一区精品视频 | 尤物视频免费 | 天堂亚洲| 欧美精品成人a多人在线观看 | 日韩欧美在线观看 | 欧美三级中文字幕hd | 国产乱淫a∨片免费视频 | 亚洲另类图片小说 | 性刺激欧美三级在线观看 | 天堂网在线网站成人午夜网站 | 校园春色亚洲色图 | 九九热在线免费视频 | 亚欧成人毛片一区二区三区四区 | 亚洲精品在线播放视频 | 欧美性猛交xxxx免费看 | 欧美添下面视频免费观看 | videos亚洲| free性欧美69式 | 亚洲黄色高清 | 亚洲第一页国产 | 乌克兰鲜嫩xxxx | 一级做a爱片久久蜜桃 | 久久精品国产一区二区三区不卡 | 久久r这里只有精品 | 欧美激情福利视频在线观看免费 | 牛牛精品国内免费一区 | 日本中文字幕免费 | 国产三区视频李宗瑞 | 717影院理论午夜伦不卡久久 | 国产亚洲精品激情都市 | 国产一二三区在线观看 | 欧美日韩永久久一区二区三区 | 亚洲日韩中文字幕天堂不卡 | 欧美在线观看高清一二三区 | 特级aa一级欧美毛片 | 成人精品一区二区久久久 |