Codeforces 455c civilization tree diameter + query set

Source: Internet
Author: User

Question link: Click the open link

Question:

K queries for graphs with n vertices and M undirected Edges
No duplicate edge, auto ring, or ring
Define two points to belong to one country: when these two points are connected
Operation 1 X: output the longest route length in the country where X is located

Operation 2 x Y: Ignore if x y belongs to a country
If it does not belong to a country, an edge is connected between the two sets to minimize the longest link after the connection.

The longest path of two sets. The first is to find the midpoint of the longest path of the Two Sets for connection.

Then the longest path length after connection is path [x]/2 + path [y]/2 + 1 (except for 2 rounded up)


Then we can prepare the diameter of each tree.

Because you only need to find the length of the tree's diameter and do not care about the path of the tree's diameter

Therefore, for all the sub-trees of BFS (x), the length of the two longest sub-trees is the diameter. BFS: once

#include <cstdio>#include <cstring>#include<iostream>#include <queue>#include <set>using namespace std;#define inf 10000000#define N 300005struct Edge{int to, nex;}edge[N<<1];int head[N], edgenum;void add(int u, int v){Edge E = {v, head[u]};edge[edgenum] = E;head[u] = edgenum++;}int f[N], path[N];int find(int x){return x==f[x]?x:f[x] = find(f[x]);}void Union(int x, int y){int fx = find(x), fy = find(y);if(fx == fy)return ;if(fx>fy)swap(fx, fy);f[fx] = fy;int now = path[fx]/2 + path[fy]/2 +1;if(path[fx]&1)now++;if(path[fy]&1)now++;path[fx] = path[fy] = max(max(path[fx], path[fy]), now);}int n, m;int dis[N];vector<int>G[N];int BFS(int x){ int E = x;queue<int>q;  for(int i = 0; i < G[f[x]].size(); i++)dis[G[f[x]][i]] = inf;q.push(x);dis[x]=0;while(!q.empty())  {  int u = q.front(); q.pop(); for(int i = head[u]; ~i ;i = edge[i].nex)  {  int v = edge[i].to; if(dis[v] > dis[u]+1){dis[v] = dis[u]+1;if(dis[v]>dis[E])E = v;q.push(v);}}}return E;  }void work(int x){int S = BFS(x);S = BFS(S);path[x] = dis[S];}set<int>s;void init(){s.clear();memset(head, -1, sizeof head); edgenum = 0;memset(path, 0, sizeof path);for(int i = 0; i <= n; i++)f[i] = i, G[i].clear();}int main(){  int i, u, v, q, op;while(cin>>n>>m>>q){init();while(m--){scanf("%d %d",&u,&v);add(u,v);add(v,u);Union(u,v);}for(i = 1; i <= n; i++)find(i);for(i = 1; i <= n; i++) {s.insert(f[i]);G[f[i]].push_back(i);}for(set<int>::iterator it = s.begin(); it!=s.end(); it++)work(*it);while(q--){scanf("%d %d",&op, &u);if(op==1){u = find(u);printf("%d\n", path[u]);}else {scanf("%d",&v);Union(u, v);}}}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.