SPOJ PT07J Query on a tree II

You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3…N-1. Each edge has an integer value assigned to it, representing its length.

We will ask you to perfrom some instructions of the following form:

DIST a b : ask for the distance between node a and node b
or
KTH a b k : ask for the k-th node on the path from node a to node b
Example:
N = 6
1 2 1 // edge connects node 1 and node 2 has cost 1
2 4 1
2 5 2
1 3 1
3 6 2

Path from node 4 to node 6 is 4 -> 2 -> 1 -> 3 -> 6
DIST 4 6 : answer is 5 (1 + 1 + 1 + 2 = 5)
KTH 4 6 4 : answer is 3 (the 4-th node on the path from node 4 to node 6 is 3)
Input

The first line of input contains an integer t, the number of test cases (t <= 25). t test cases follow.

For each test case:

In the first line there is an integer N (N <= 10000)
In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 100000)
The next lines contain instructions “DIST a b” or “KTH a b k”
The end of each test case is signified by the string “DONE”.
There is one blank line between successive tests.

Output

For each “DIST” or “KTH” operation, write one integer representing its result.

Print one blank line after each test.

Example

Input:
1

6
1 2 1
2 4 1
2 5 2
1 3 1
3 6 2
DIST 4 6
KTH 4 6 4
DONE

Output:
5
3

第一问。。。!
第二问找这个点直接倍增就好了嘛。。。。

#include <cstdio> 
#include <cstring> 
#include <algorithm> 
#define LOG 20
#define N 10010
#define M 20010
using namespace std;  

typedef long long LL;

#define logn(i, a, b) for(int i =(a);(1 << i)<=(b); ++ i) 
#define repp(e, H, u) for(Edge* e = H[u]; e; e = e -> next) 
#define rep(i, a, b) for(int i =(a); i < (b); ++ i) 
#define rev(i, a, b) for(int i =(a); i >=(b); -- i) 
#define FOR(i, a, b) for(int i =(a); i <=(b); ++ i) 
#define clr(a, x) memset(a, x, sizeof a) 
#define cpy(a, x) memcpy(a, x, sizeof a) 
#define root 1, 2, n 
#define mid ((l + r) >> 1) 

struct Edge
{  
    int v, c;  
    Edge* next;  
} E[M], *H[N], *e;  

int anc[N][LOG];  
int dist[N];  
int pre[N];  
int dep[N];  
int n;  

void clear()
{
    e = E;  
    clr(H, 0);  
    clr(dep, 0);  
    clr(dist, 0);  
}  

void adde(int u, int v, int c)
{
    e -> v = v;  
    e -> c = c;  
    e -> next = H[u];  
    H[u] = e ++;  
    e -> v = u;  
    e -> c = c;  
    e -> next = H[v];  
    H[v] = e ++;  
}  

void dfs(int u, int fa = -1)
{  
    pre[u] = fa;  
    repp(e, H, u)
    {
        int v = e -> v;  
        if(v == fa)continue;  
        dist[v] = dist[u] + e -> c;  
        dep[v] = dep[u] + 1;  
        dfs(v, u);
    }  
}  

void preprocess()
{
    FOR(i, 1, n)
        anc[i][0] = pre[i];  
    FOR(i, 1, n)
        logn(j, 1, n)
            anc[i][j] = -1;  
    logn(j, 1, n)
        FOR(i, 1, n)
            if(~anc[i][j - 1])
                anc[i][j] = anc[anc[i][j - 1]][j - 1];  
}  

int lca(int x, int y)
{
    if(dep[x] < dep[y])swap(x, y);  
    int log = 0;  
    logn(i, 1, dep[x])++ log;  
    rev(i, log, 0)
        if(dep[x] -(1 << i)>= dep[y])x = anc[x][i];  
    if(x == y)return x;  
    rev(i, log, 0)
    if(~anc[x][i] && anc[x][i] != anc[y][i])
    {  
        x = anc[x][i];  
        y = anc[y][i];  
    }  
    return pre[x];  
}  

int kth(int x, int y, int k)
{
    int cp = lca(x, y);  
    if(dep[x] - dep[cp] + 1 >= k)
    {
        int deep = dep[x] - k + 1, log = 0;  
        logn(i, 1, dep[x])++ log;  
        rev(i, log, 0)if(dep[x] -(1 << i)>= deep)x = anc[x][i];  
        return x;  
    } 
    else
    {  
        int deep = k -(dep[x] - dep[cp])+ dep[cp] - 1, log = 0;  
        logn(i, 1, dep[y])++ log;  
        rev(i, log, 0)if(dep[y] -(1 << i)>= deep)y = anc[y][i];  
        return y;  
    }  
}  

void solve()
{  
    char s[10];  
    int x, y, c;  
    clear();  
    scanf("%d",&n);  
    rep(i, 1, n)
    {  
        scanf ("%d%d%d", &x, &y, &c);  
        adde(x, y, c);  
    }  
    dfs(1);  
    preprocess();  
    while(scanf("%s", s)&& s[1] != 'O')  
        if(s[0] == 'D')
        {  
            scanf("%d%d", &x, &y);  
            int cp = lca(x, y);  
            printf("%d\n", dist[x] + dist[y] - 2 * dist[cp]);  
        } 
        else 
        {  
            scanf("%d%d%d", &x, &y, &c);  
            printf("%d\n", kth(x, y, c));  
        }  
}  

int main()
{  
    int T;  
    scanf("%d", &T);  
    while(T --)solve();  
    return 0;  
}  
全部评论

相关推荐

05-11 11:48
河南大学 Java
程序员牛肉:我是26届的双非。目前有两段实习经历,大三上去的美团,现在来字节了,做的是国际电商的营销业务。希望我的经历对你有用。 1.好好做你的CSDN,最好是直接转微信公众号。因为这本质上是一个很好的展示自己技术热情的证据。我当时也是烂大街项目(网盘+鱼皮的一个项目)+零实习去面试美团,但是当时我的CSDN阅读量超百万,微信公众号阅读量40万。面试的时候面试官就告诉我说觉得我对技术挺有激情的。可以看看我主页的美团面试面经。 因此花点时间好好做这个知识分享,最好是单拉出来搞一个板块。各大公司都极其看中知识落地的能力。 可以看看我的简历对于博客的描述。这个帖子里面有:https://www.nowcoder.com/discuss/745348200596324352?sourceSSR=users 2.实习经历有一些东西删除了,目前看来你的产出其实很少。有些内容其实很扯淡,最好不要保留。有一些点你可能觉得很牛逼,但是面试官眼里是减分的。 你还能负责数据库表的设计?这个公司得垃圾成啥样子,才能让一个实习生介入数据库表的设计,不要写这种东西。 一个公司的财务审批系统应该是很稳定的吧?为什么你去了才有RBAC权限设计?那这个公司之前是怎么处理权限分离的?这些东西看着都有点扯淡了。 还有就是使用Redis实现轻量级的消息队列?那为什么这一块不使用专业的MQ呢?为什么要使用redis,这些一定要清楚, 就目前看来,其实你的这个实习技术还不错。不要太焦虑。就是有一些内容有点虚了。可以考虑从PR中再投一点产出
投递美团等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务