hihocoder 1490 Tree Restoration】的更多相关文章

构造. 从最后一层开始往上构造.最后一层肯定都是叶子结点,距离为2的肯定是同一个父亲,确定好了父亲之后,可以确定上一层每个节点之间的距离,以及上一层每个节点到还未确定的叶子节点之间的距离. #include<bits/stdc++.h> using namespace std; int n,m,k; vector<]; ],L[],h[]; ][]; ]; int main() { scanf("%d%d%d",&n,&m,&k); ;i<…
微软 2017春招真题 题目 There is a tree of N nodes which are numbered from 1 to N. Unfortunately, its edges are missing so we don't know how the nodes are connected. Instead we know:   Which nodes are leaves   The distance (number of edges) between any pair o…
[题目链接]:https://hihocoder.com/problemset/problem/1490 [题意] 给你一棵树的以下信息: 1.节点个数 2.给出树的每一层从左到右的顺序每个节点的编号 3.哪些节点是叶子节点 然后树中的边被去掉了; 让你复原出原图(树) [题解] 这是道模拟题. 从最下层到上一层,从最左边的节点到右边的节点往上接父亲节点就好; 具体实现的时候; 直接找上一层最左边的且没被其他节点当爸爸的.且不为叶子节点的节点当爸爸就好; 当了爸爸之后 更新那个爸爸到其他所有节点…
There is a tree of n vertices. For each vertex a list of all its successors is known (not only direct ones). It is required to restore the tree or to say there is no such tree. Input The first line contains a single integer n (1 ≤ n ≤ 1000) — the num…
There is a tree of N nodes which are numbered from 1 to N. Unfortunately, its edges are missing so we don't know how the nodes are connected. Instead we know: 1. Which nodes are leaves 2. The distance (number of edges) between any pair of leaves 3. T…
输入n m km个数,表示每层的节点个数接下来m行是每层的节点,节点顺序是从左往右的k个叶子节点k*k个矩阵,表示叶子节点之间的距离 输出:每个节点的父亲节点编号,root节点是0 题解:1.很明显,相邻两个节点的距离如果是2,那么便是同一个父亲节点.2.第一个点的父亲节点u,必定是上一层第一个非叶子节点fa3.u左边的节点v 如果dis[u][v]==2,则fa[v]=fa[u] 否则,fa[v]为fa的下一个非叶子节点所以很明显,dis矩阵得为n*n的大小,而不仅仅是k*k4.由于一开始只知…
题意: 一棵树,给出每个点的后代们,问你这棵树是否存在,存在就给出这棵树 n<=1000 思路: 对祖先->后代建立有向图,跑拓扑排序.跑的时候不断更新父亲并判断答案的存在性,同时注意一种情况:一个点他儿子是他的后代,同样也得是他父亲的后代,这样传递下去就一定是所有祖宗的后代. 代码: 代码参考网上的 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #…
[hihoCoder#1381]Little Y's Tree 试题描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每次小J会删掉这个树中的k条边,这棵树被分成k+1个连通块.小J想知道每个连通块中最远点对距离的和. 这里的询问是互相独立的,即每次都是在小Y的原树上进行操作. 输入 第一行一个整数n,接下来n-1行每行三个整数u,v,w,其中第i行表示第i条边边权为wi,连接了ui,vi两点. 接下来一行一个整数q,表示有q组询问. 对于每组询问,第一行一个正整数k,接下来一…
http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int pp = 998244353; const int N = 503; int…
题目链接 #1381 : Little Y's Tree 时间限制:24000ms 单点时限:4000ms 内存限制:512MB 描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每次小J会删掉这个树中的k条边,这棵树被分成k+1个连通块.小J想知道每个连通块中最远点对距离的和. 这里的询问是互相独立的,即每次都是在小Y的原树上进行操作. 输入 第一行一个整数n,接下来n-1行每行三个整数u,v,w,其中第i行表示第i条边边权为wi,连接了ui,vi两点. 接下来一行一个整数q…