hihocoder 1490 Tree Restoration
构造。
从最后一层开始往上构造。最后一层肯定都是叶子结点,距离为2的肯定是同一个父亲,确定好了父亲之后,可以确定上一层每个节点之间的距离,以及上一层每个节点到还未确定的叶子节点之间的距离。
#include<bits/stdc++.h>
using namespace std; int n,m,k;
vector<int>v[];
int A[],L[],h[];
int D[][];
int f[]; int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=m;i++) scanf("%d",&A[i]);
for(int i=;i<=m;i++)
{
while(A[i]--)
{
int x; scanf("%d",&x);
v[i].push_back(x);
}
}
for(int i=;i<=k;i++) scanf("%d",&L[i]),h[L[i]]=;
for(int i=;i<=k;i++) for(int j=;j<=k;j++) scanf("%d",&D[L[i]][L[j]]); for(int i=m;i>=;i--)
{
int last = ;
int now = ;
while()
{
if(now==v[i].size()) break; while()
{
if(h[v[i-][last]]==) last++;
else break;
} f[v[i][now]] = v[i-][last]; now++;
while()
{
if(now==v[i].size()) break;
if(D[v[i][now]][v[i][now-]]!=) {last++; break;}
f[v[i][now]] = v[i-][last]; now++;
}
} for(int j=;j<v[i].size();j++)
for(int t=;t<v[i].size();t++)
D[f[v[i][j]]][f[v[i][t]]]=D[v[i][j]][v[i][t]]-; for(int j=;j<v[i].size();j++)
for(int t=;t<=k;t++)
D[f[v[i][j]]][L[t]]=D[L[t]][f[v[i][j]]]=D[v[i][j]][L[t]]-;
} for(int i=;i<=n;i++)
{
printf("%d",f[i]);
if(i<n) printf(" ");
else printf("\n");
} return ;
}
hihocoder 1490 Tree Restoration的更多相关文章
- #1490 : Tree Restoration
微软 2017春招真题 题目 There is a tree of N nodes which are numbered from 1 to N. Unfortunately, its edges a ...
- 【微软2017年预科生计划在线编程笔试 B】Tree Restoration
[题目链接]:https://hihocoder.com/problemset/problem/1490 [题意] 给你一棵树的以下信息: 1.节点个数 2.给出树的每一层从左到右的顺序每个节点的编号 ...
- Tree Restoration Gym - 101755F (并查集)
There is a tree of n vertices. For each vertex a list of all its successors is known (not only direc ...
- hihocoder1490 Tree Restoration 模拟
There is a tree of N nodes which are numbered from 1 to N. Unfortunately, its edges are missing so w ...
- #1490 : Tree Restoration-(微软2017在线笔试)
输入n m km个数,表示每层的节点个数接下来m行是每层的节点,节点顺序是从左往右的k个叶子节点k*k个矩阵,表示叶子节点之间的距离 输出:每个节点的父亲节点编号,root节点是0 题解:1.很明显, ...
- Codeforces gym101755F Tree Restoration(拓扑排序)
题意: 一棵树,给出每个点的后代们,问你这棵树是否存在,存在就给出这棵树 n<=1000 思路: 对祖先->后代建立有向图,跑拓扑排序.跑的时候不断更新父亲并判断答案的存在性,同时注意一种 ...
- [hihoCoder#1381]Little Y's Tree
[hihoCoder#1381]Little Y's Tree 试题描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每次小J会删掉这个树中的k条边,这棵树被分成k+1个连通块.小J ...
- 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II
http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...
- DFS序+线段树 hihoCoder 1381 Little Y's Tree(树的连通块的直径和)
题目链接 #1381 : Little Y's Tree 时间限制:24000ms 单点时限:4000ms 内存限制:512MB 描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每 ...
随机推荐
- [LeetCode] 28. Implement strStr() ☆
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- codevs 1491 取物品
1491 取物品 http://codevs.cn/problem/1491/ 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 现在有n个物品(有可能 ...
- MySQl学习-——Mysql体系结构与Mysql存储引擎
Mysql体系结构与Mysql存储引擎 Mysql体系结构 mysql体系结构图:
- [POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)
[POJ3370]&[HDU1808]Halloween treats Description -Every year there is the same problem at Hallowe ...
- MOD - Power Modulo Inverted(SPOJ3105) + Clever Y(POJ3243) + Hard Equation (Gym 101853G ) + EXBSGS
思路: 前两题题面相同,代码也相同,就只贴一题的题面了.这三题的意思都是求A^X==B(mod P),P可以不是素数,EXBSGS板子题. SPOJ3105题目链接:https://www.spoj. ...
- Redis数据类型之字符串(string)
1. string类型简介 string类型是二进制安全的,能够存储任意类型的字符串. string类型是最常用到的数据类型,一种常用的用法就是将对象格式化为JSON字符串然后放到redis中,取出来 ...
- AngularJs 文件上传(实现Multipart/form-data 文件的上传)
<!-- 上传yml文件 --> <div class="blackBoard" ng-show="vm.showUpop==true"> ...
- linux驱动开发:用户空间操作LCD显示简单的图片【转】
转自:http://blog.csdn.net/changliang7731/article/details/53074616 上一章我们简单介绍了LCD的一些基本原理.当然更深奥的还有,比如gamm ...
- linux kernel make构建分析
前言 之前对uboot的构建进行了分析,现在再对linux kernel的构建进行分析.几年前的确也分析过,但是只是停留在笔记层面,没有转为文章,这次下定决定来完善它. 环境 同样,采用的还是zynq ...
- 做Mysql主从时,注意使用replicate_wild_do_table和replicate-wild-ignore-table【转】
做Mysql主从时,注意使用replicate_wild_do_table和replicate-wild-ignore-table 浓缩版: 使用replicate_do_db和replicate_i ...