树剖求公共祖先啊

1、

我们将A省简化为由N个城市组成,某些城市之间存在双向道路,而且A省的交通有一个特点就是任意两个城市之间都能通过道路相互到达,且在不重复经过城市的情况下任意两个城市之间的到达方案都是唯一的。聪明的你一定已经发现,这些城市构成了树这样一个结构。 

现在百度陆续开了许许多多的子公司。每家子公司又会在各城市中不断兴建属于该子公司的办公室。 

由于各个子公司之间经常有资源的流动,所以公司员工常常想知道,两家子公司间的最小距离。
我们可以把子公司看成一个由办公室组成的集合。那么两个子公司A和B的最小距离定义为min(dist(x,y))(x∈A,y∈B)。其中dist(x,y)表示两个办公室之间的最短路径长度。 

现在共有Q个询问,每次询问分别在两个子公司间的最小距离。 

Input

第一行一个正整数T,表示数据组数。 

对于每组数据: 

第一行两个正整数N和M。城市编号为1至N,子公司编号为1至M。 

接下来N-1行给定所有道路的两端城市编号和道路长度。 

接下来M行,依次按编号顺序给出各子公司办公室所在位置,每行第一个整数G,表示办公室数,接下来G个数为办公室所在位置。 

接下来一个整数Q,表示询问数。 

接下来Q行,每行两个正整数a,b(a不等于b),表示询问的两个子公司。 


【数据范围】 

0<=边权<=100 

1<=N,M,Q,工厂总数<=100000

Output

对于每个询问,输出一行,表示答案。

Sample Input

1
3 3
1 2 1
2 3 1
2 1 1
2 2 3
2 1 3
3
1 2
2 3
1 3

Sample Output

1
0
0
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std ;
const int N = 100000 + 5;
struct data {
    int to, next, w;
} e[N << 1];
int head[N], size[N], son[N], bl[N], cnt, n, m, fa[N], d[N], dis[N], dd[N];
void ins(int u, int v, int w) {
    e[++cnt].to = v;
    e[cnt].next = head[u];
    e[cnt].w = w;
    head[u] = cnt;
}
void insert(int x, int y, int w) {
    ins(x, y, w);
    ins(y, x, w);
}
void dfs1(int x) {
    size[x] = 1;
    son[x] = 0;
    for(int i = head[x]; i; i = e[i].next) {
        if(e[i].to != fa[x]) {
            fa[e[i].to] = x;
            dis[e[i].to] = dis[x] + e[i].w;
            d[e[i].to] = d[x] + 1;
            dfs1(e[i].to);
            size[x] += size[e[i].to];
            if(size[e[i].to] > size[son[x]])
                son[x] = e[i].to;
        }
    }
}
void dfs2(int x, int chain) {
    bl[x] = chain;
    if(son[x])
        dfs2(son[x], chain);
    for(int i = head[x]; i; i = e[i].next)
        if(e[i].to != son[x] && e[i].to != fa[x])
            dfs2(e[i].to, e[i].to);
}
int lca(int x, int y) {
    for(; bl[x] != bl[y]; d[bl[x]] > d[bl[y]] ? x = fa[bl[x]] : y = fa[bl[y]]);
    return d[x] < d[y] ? x : y;
}
void init() {
    cnt = 0;
    memset(head, 0, sizeof head);
    memset(fa, 0, sizeof fa);
    memset(d, 0, sizeof(d));
    memset(son, 0, sizeof(son));
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        init();
        cin >> n >> m;
        vector<int> vs[100010];
        memset(dis, 0, sizeof dis);
        memset(dd, 0, sizeof dd);
        for(int i = 1; i < n; i++) {
            int u, v, w;
            cin >> u >> v >> w;
            insert(u, v, w), dd[v] = 1;
        }
        for(int i = 1; i <= m; i++) {
            int g, z;
            cin >> g;
            for(int j = 0; j < g; j++) {
                cin >> z;
                vs[i].push_back(z);
            }
        }
        int S;
        for(int i = 1; i <= n; i++)
            if(!dd[i])
                S = i;
        dfs1(S), dfs2(S, S);
        int q, a, b;
        cin >> q;
        for(int i = 0; i < q; i++) {
            cin >> a >> b;
            int minn = 0x3f3f3f3f;
            for(int i = 0; i < vs[a].size(); i++) {
                for(int j = 0; j < vs[b].size(); j++) {
                    int leng = dis[vs[a][i]] + dis[vs[b][j]] - 2 * dis[lca(vs[a][i], vs[b][j])];
                    minn = min(minn, leng);
                }
            }
            cout << minn << endl;
        }
    }
    return 0;
}

2、

The ALPC company is now working on his own network system, which is connecting all N ALPC department. To economize on spending, the backbone network has only one router for each department, and N-1 optical fiber in total to connect all routers. 
The usual way to measure connecting speed is lag, or network latency, referring the time taken for a sent packet of data to be received at the other end. 
Now the network is on trial, and new photonic crystal fibers designed by ALPC42 is trying out, the lag on fibers can be ignored. That means, lag happened when message transport through the router. ALPC42 is trying to change routers to make the network faster, now he want to know that, which router, in any exactly time, between any pair of nodes, the K-th high latency is. He needs your help. 

Input

There are only one test case in input file. 
Your program is able to get the information of N routers and N-1 fiber connections from input, and Q questions for two condition: 1. For some reason, the latency of one router changed. 2. Querying the K-th longest lag router between two routers. 
For each data case, two integers N and Q for first line. 0<=N<=80000, 0<=Q<=30000. 
Then n integers in second line refer to the latency of each router in the very beginning. 
Then N-1 lines followed, contains two integers x and y for each, telling there is a fiber connect router x and router y. 
Then q lines followed to describe questions, three numbers k, a, b for each line. If k=0, Telling the latency of router a, Ta changed to b; if k>0, asking the latency of the k-th longest lag router between a and b (include router a and b). 0<=b<100000000. 
A blank line follows after each case. 

Output

For each question k>0, print a line to answer the latency time. Once there are less than k routers in the way, print "invalid request!" instead.

思路:lca求最近公共祖先,注意是第k大。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std ;
const int N = 500000 + 5;
struct data {
    int to, next, w;
} e[N << 1];
int head[N], size[N], son[N], bl[N], cnt, n, q, fa[N], d[N], ping[N], ancestors[N], temp[N];
void ins(int u, int v) {
    e[++cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt;
}
void insert(int x, int y) {
    ins(x, y);
    ins(y, x);
}

void dfs1(int x) {
    size[x] = 1;
    son[x] = 0;
    for(int i = head[x]; i; i = e[i].next) {
        if(e[i].to != fa[x]) {
            fa[e[i].to] = x;
            d[e[i].to] = d[x] + 1;
            dfs1(e[i].to);
            size[x] += size[e[i].to];
            if(size[e[i].to] > size[son[x]])
                son[x] = e[i].to;
        }
    }
}
void dfs2(int x, int chain) {
    bl[x] = chain;
    if(son[x])
        dfs2(son[x], chain);
    for(int i = head[x]; i; i = e[i].next)
        if(e[i].to != son[x] && e[i].to != fa[x])
            dfs2(e[i].to, e[i].to);
}
int lca(int x, int y) {
    for(; bl[x] != bl[y]; d[bl[x]] > d[bl[y]] ? x = fa[bl[x]] : y = fa[bl[y]]);
    return d[x] < d[y] ? x : y;
}
void init() {
    cnt = 0;
    memset(head, 0, sizeof head);
    memset(fa, 0, sizeof fa);
    memset(d, 0, sizeof(d));
    memset(son, 0, sizeof(son));
}
int main() {
    cin >> n >> q;
    for(int i = 1; i <= n; i++)
        cin >> ping[i];
    for(int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        insert(a, b);
        ancestors[b] = 1;
    }
    int S = 0;
    for(int i = 1; i <= n; i++)
        if(ancestors[i]) {
            S = i;
            break;
        }
    dfs1(S), dfs2(S, S);
    for(int i = 0; i < q; i++) {
        int k, a, b;
        cin >> k >> a >> b;
        if(k == 0)
            ping[a] = b;
        else {
            int zu = lca(a, b), t = 0;
            temp[t++] = ping[zu];
            while(a != S && a != zu) {
                temp[t++] = ping[a];
                a = fa[a];
            }
            while(b != S && b != zu) {
                temp[t++] = ping[b];
                b = fa[b];
            }
            sort(temp, temp + t);
            if(k > t)
                cout << "invalid request!" << endl;
            else
                cout << temp[t - k] << endl;
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值