Coderforces 455C and check the diameter of the tree

Source: Internet
Author: User

Click to open link

Test instructions: There are two operations, 1 is to ask the length of the longest path with U, 2 is to merge the two sets of U and V, but to make the longest path of the merged set minimum

Idea: Because the subsequent operation will change the path length, you can first preprocess all the length, in a set of elements of the longest path of the same, and then perform the query operation, is this preprocessing is really drunk, that is, two times BFS to find the maximum value, but it is possible to timeout, because if two points in a set, Will go 150,000 times, and then judge the maximum value of each point, so definitely time out, we will set the elements of this collection to determine the elements of the line, it will not time out, and then is the collection of merged parts, two sets a,b,a the longest path is 8,b the longest path is 3, So the longest path after the merger is how much, because the longest path is the shortest, then we will be two lengths are binary merge on the line, then this length is (8+1)/2+ (3+1)/2=6, but the longest length or 8, understand it, compare the three cases on the line

#include <queue> #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h > #include <iostream> #include <algorithm>using namespace std;typedef long long ll;typedef unsigned long Long Ull;const int inf=0x3f3f3f3f;const ll Inf=0x3f3f3f3f3f3f3f3fll;const int maxn=300010;vector<int>g[maxn]; Vector<int>gg[maxn];int f[maxn],num[maxn],used[maxn],vis[maxn],dis[maxn];int n;int find1 (int x) {if (x!=f[x]) F    [X]=find1 (F[x]); return f[x];}    void Unite (int a,int b) {int aa=find1 (a);    int Bb=find1 (b);    if (AA==BB) return; F[aa]=bb;num[bb]=max (Max (NUM[AA],NUM[BB]), (num[aa]+1)/2+ (num[bb]+1)/2+1);}    void BFs (int s) {int bbq=find1 (s);    int kkk=gg[bbq].size ();        for (int i=0;i<kkk;i++) {int ll=gg[bbq][i];    dis[ll]=0;vis[ll]=0;    } vis[s]=1;dis[s]=0;    queue<int>que;    Que.push (s);        while (!que.empty ()) {int U=que.front (); Que.pop ();        Used[u]=1; for (unsigned int i=0;i<g[u].sizE (); i++) {int v=g[u][i];                if (vis[v]==0) {vis[v]=1;                dis[v]=dis[u]+1;            Que.push (v);    }}}}int slove (int s) {int max1=0,pos=s;    BFS (s);    int Bbq=find1 (s);    int kkk=gg[bbq].size ();        for (int i=0;i<kkk;i++) {int ll=gg[bbq][i];        if (DIS[LL]&GT;MAX1) {max1=dis[ll];p os=ll;    }} BFS (POS);    Bbq=find1 (POS);    Kkk=gg[bbq].size ();    int ans=0;        for (int i=0;i<kkk;i++) {int ll=gg[bbq][i];    if (Dis[ll]>ans) ans=dis[ll]; } return ans;    int main () {int m,q,u,v,so;        while (scanf ("%d%d%d", &n,&m,&q)!=-1) {for (int i=0;i<maxn;i++) g[i].clear (), gg[i].clear ();        for (int i=0;i<=n;i++) f[i]=i;        memset (num,0,sizeof (num));            for (int i=0;i<m;i++) {scanf ("%d%d", &u,&v);            G[u].push_back (v);            G[v].push_back (U);            int u1=find1 (U), V1=find1 (v); if (U1!=V1) F[U1]=v1;            } for (int i=1;i<=n;i++) {int t=find1 (i);        Gg[t].push_back (i);                } for (int i=1;i<=n;i++) {if (I==f[i]) {int ans=slove (i);            Num[i]=ans;            }} for (int i=0;i<q;i++) {scanf ("%d", &so);                if (so==2) {scanf ("%d%d", &u,&v);            Unite (U,V);                }else if (so==1) {scanf ("%d", &u);                int ans=find1 (u);            printf ("%d\n", Num[ans]); }}} return 0;}


Coderforces 455C and check the diameter of the tree

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.