Codeforces 804D Expected diameter of a tree

D. Expected diameter of a tree
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem.ios

We have a forest (acyclic undirected graph) with n vertices and m edges. There are q queries we should answer. In each query two vertices v and u are given. Let V be the set of vertices in the connected component of the graph that contains v, and U be the set of vertices in the connected component of the graph that contains u. Let's add an edge between some vertex  and some vertex in  and compute the value d of the resulting component. If the resulting component is a tree, the value d is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of d, if we choose vertices a and b from the sets uniformly at random?dom

Can you help Pasha to solve this problem?ide

The diameter of the component is the maximum distance among some pair of vertices in the component. The distance between two vertices is the minimum number of edges on some path between the two vertices.this

Note that queries don't add edges to the initial forest.spa

Input

The first line contains three integers nm and q(1 ≤ n, m, q ≤ 105) — the number of vertices, the number of edges in the graph and the number of queries.rest

Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n), that means there is an edge between vertices ui and vi.code

It is guaranteed that the given graph is a forest.component

Each of the next q lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the vertices given in the i-th query.orm

Output

For each query print the expected value of d as described in the problem statement.blog

Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Let's assume that your answer is a, and the jury's answer is b. The checker program will consider your answer correct, if .

Examples
input
3 1 2
1 3
3 1
2 3
output
-1
2.0000000000
input
5 2 3
2 4
4 3
4 2
4 1
2 5
output
-1
2.6666666667
2.6666666667
Note

In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1 - 2, the other one is 2 - 3. In both ways the resulting diameter is 2, so the answer is 2.

In the second example the answer for the first query is obviously -1. The answer for the second query is the average of three cases: for added edges 1 - 2 or 1 - 3 the diameter is 3, and for added edge 1 - 4 the diameter is 2. Thus, the answer is .

 

 題意:

給出一個森林,q次詢問,每次問把x,y兩點所屬的樹之間任意鏈接一條邊造成新的樹的直徑的指望,若是x和y在同一棵樹中輸出-1;

代碼:

//這題算出複雜度也就解出來了。先枚舉一棵樹中的節點而後二分找另外一棵樹中的節點知足兩個節點之間的距離不小於max(樹1直徑,
//樹2直徑),他們的貢獻就是各自在本身樹中最遠能到達的端點的距離相加再+1,不然貢獻就是max(樹1直徑,樹2直徑),這樣看似是
//q*n*long(n),可是注意到全部的樹的大小總和是n因此最壞是sqrt(n)棵樹每棵樹大小是sqrt(n),因此是q*sqrt(n)*long(n);
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN=100009;
int fa[MAXN],head[MAXN],tot,n,cnt,m,q,d[MAXN],f[MAXN],deep,o,oo,root[MAXN];
int a[MAXN],aa;
double size[MAXN];
map<pair<int,int>,double>mp;
vector<ll>v[MAXN],vv[MAXN];
struct Edge { int u,v,next; }edge[MAXN*2];
void init()
{
    tot=cnt=0;
    memset(head,-1,sizeof(head));
    memset(fa,-1,sizeof(fa));
    memset(f,0,sizeof(f));
    memset(d,-1,sizeof(d));
}
void add(int x,int y)
{
    edge[tot].u=x;edge[tot].v=y;
    edge[tot].next=head[x];
    head[x]=tot++;
    edge[tot].u=y;edge[tot].v=x;
    edge[tot].next=head[y];
    head[y]=tot++;
}
void dfs1(int x,int father,int p)
{
    v[p].push_back(x);
    for(int i=head[x];i!=-1;i=edge[i].next){
        int y=edge[i].v;
        if(y==father) continue;
        fa[y]=p;
        dfs1(y,x,p);
    }
}
void dfs2(int x,int father,int sum,bool w)
{
    if(w!=0) f[x]=max(f[x],sum);
    if(sum>=deep){
        deep=sum;
        if(w==0) o=x;
        else if(w==1) oo=x;
    }
    for(int i=head[x];i!=-1;i=edge[i].next){
        int y=edge[i].v;
        if(y==father) continue;
        dfs2(y,x,sum+1,w);
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    init();
    scanf("%d%d%d",&n,&m,&q);
    for(int i=0;i<m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
    }
    for(int i=1;i<=n;i++){
        if(fa[i]!=-1) continue;
        fa[i]=++cnt;
        v[cnt].clear();vv[cnt].clear();
        dfs1(i,0,cnt);
        root[cnt]=i;
    }
    for(int i=1;i<=cnt;i++){
        deep=0;
        dfs2(root[i],0,0,0);
        deep=0;
        dfs2(o,0,0,1);
        d[i]=deep;
        dfs2(oo,0,0,2);
        aa=v[i].size();
        for(int j=0;j<aa;j++) a[j]=f[v[i][j]];
        v[i].clear();
        for(int j=0;j<aa;j++) v[i].push_back(a[j]);
        sort(v[i].begin(),v[i].end());
        a[aa]=0;
        for(int j=aa-1;j>=0;j--) a[j]=a[j+1]+v[i][j]+1;
        for(int j=0;j<=aa;j++) vv[i].push_back(a[j]);
    }
    while(q--){
        int x,y;
        scanf("%d%d",&x,&y);
        if(fa[x]==fa[y]) printf("-1\n");
        else{
            pair<int,int>p1(fa[x],fa[y]);
            if(mp[p1]>0) printf("%.6f\n",mp[p1]);
            else{
                double ans=0;
                int xx=fa[x],yy=fa[y];
                if(v[xx].size()<=v[yy].size()){
                    for(int i=0;i<v[xx].size();i++){
                        ll tmp=lower_bound(v[yy].begin(),v[yy].end(),max(d[xx],d[yy])-v[xx][i]-1)-v[yy].begin();
                        ans+=(vv[yy][tmp]+v[xx][i]*(v[yy].size()-tmp))+tmp*max(d[xx],d[yy]);
                    }
                }else{
                    for(int i=0;i<v[yy].size();i++){
                        ll tmp=lower_bound(v[xx].begin(),v[xx].end(),max(d[xx],d[yy])-v[yy][i]-1)-v[xx].begin();
                        ans+=(vv[xx][tmp]+v[yy][i]*(v[xx].size()-tmp))+tmp*max(d[xx],d[yy]);
                    }
                }
                double tmp1=v[xx].size(),tmp2=v[yy].size();
                ans/=(tmp1*tmp2);
                printf("%.6f\n",ans);
                mp[p1]=ans;
            }
        }
    }
    return 0;
}
相關文章
相關標籤/搜索