Codeforces 455C C. Civilization (diameter of tree-shaped dp+ tree + and check set)

Source: Internet
Author: User

Topic Links:

Codeforces 455C

Main topic:

Given some points, the initial existence of some edges between them, gives two operations, the first one is to query the diameter of the tree in which the point resides, and the other is to merge two trees, requiring the smallest diameter of the merged tree.

Topic Analysis:
  • First calculate the diameter of the tree in the connected block before the operation, that is, Dfs once, find the deepest point of depth, and then search from this point, find the furthest distance is the diameter of this tree, because it can be proved from the root of the deepest point of the depth of the tree must be the diameter of an endpoint, Because it can reach the point of the sub-large depth or find the diameter of the tree with its common ancestor not at the root.
  • And then every time we merge, we can know that the diameter of the new tree is max{max{ L 1 , L 2 },l 1 + 1 2 +l 2 + 1 2 +1}
  • Then use and check the record connected block.
AC Code:
#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <algorithm>#define MAX 300007using namespace STD;intN,m,q; vector<int>E[max];intFa[max];intAns[max];voidClear () { for(inti =0; i < MAX; i++) e[i].clear (); for(inti =1; i < MAX; i++) Fa[i] = i;}voidAdd (intU,intV) {e[u].push_back (v); E[v].push_back (u);}int_find (intx) {returnx = = Fa[x]?x:fa[x] = _find (Fa[x]);}void_union (intXintYintf) {x = _find (x); y = _find (y);if(x > Y) Swap (x, y); Fa[y] = x;if(f)return;intxx = ans[x];intyy = Ans[y]; ANS[X] = max (max (xx, yy), (xx+1)/2+ (yy+1)/2+1);}BOOLUsed[max];BOOLMark[max];intPp,dis;voidDFS (intU,intDintP) {if(d > Dis)        {dis = D;    pp = u; } for(inti =0; I < e[u].size (); i++) {intv = e[u][i];if(v = = p)Continue; DFS (V, d+1, u); }}intMain () { while( ~scanf("%d%d%d", &n, &m, &q)) {clear ();intX, Y, Z for(inti =0; I < m; i++) {scanf("%d%d", &x, &y);            Add (x, y); _union (x, Y,1); }memset(Used,0,sizeof(used)); for(inti =1; I <= N; i++) {intx = _find (i);if(Used[x])Continue; pp = dis =-1; USED[X] =true; DFS (x,1, -1); dis =0; DFS (PP,0, -1);        Ans[x] = dis; } for(inti =0; i < Q; i++) {scanf("%d", &z);if(Z = =1)            {scanf("%d", &x);printf("%d\n", Ans[_find (x)]); }Else{scanf("%d%d", &x, &y);if(_find (x) = = _find (y))Continue; _union (x, Y,0); }        }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 455C C. Civilization (diameter of tree-shaped dp+ tree + and check set)

Related Article

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.