首页 > 其他分享 >Codeforces Beta Round #29 (Div. 2, Codeforces format)-D. Ant on the Tree

Codeforces Beta Round #29 (Div. 2, Codeforces format)-D. Ant on the Tree

时间:2023-06-12 17:31:32浏览次数:49  
标签:pre format int route Tree Codeforces ++ k1 节点


原题链接


D. Ant on the Tree



time limit per test



memory limit per test



input



output



Connected undirected graph without cycles is called a tree. Trees is a class of graphs which is interesting not only for people, but for ants too.

An ant stands at the root of some tree. He sees that there are n vertexes in the tree, and they are connected by n - 1

The ant wants to visit every vertex in the tree and return to the root, passing every edge twice. In addition, he wants to visit the leaves in a specific order. You are to find some possible route of the ant.



Input



The first line contains integer n (3 ≤ n ≤ 300) — amount of vertexes in the tree. Next n - 1 lines describe edges. Each edge is described with two integers — indexes of vertexes which it connects. Each edge can be passed in any direction. Vertexes are numbered starting from 1. The root of the tree has number 1. The last line contains k integers, where k



Output



If the required route doesn't exist, output -1. Otherwise, output 2n - 1



Examples



input



3
1 2
2 3
3



output



1 2 3 2 1



input



6
1 2
1 3
2 4
4 5
4 6
5 6 3



output



1 2 4 5 4 6 4 2 1 3 1



input



6
1 2
1 3
2 4
4 5
4 6
5 3 6



output



-1







通过深搜求出树上每个节点孩子的数目d[]和父节点pre[].叶子节点按顺序存在k数组中,先保存从根节点1到k[0]的路径,再遍历k[0]的父节点,遇到1个父节点d[]--,直到减完之后孩子数目大于1的第一个节点e, 在记录k[0]到e的路径,判断e是否为k[1]的父节点(通过pre[]数组)若没有结束程序输出-1,若有记录e到k[1]的路径,以此类推

#include <bits/stdc++.h>
#define maxn 100005
#define MOD 1000000007
using namespace std;
typedef long long ll;

int route[1005], d[305], k[305], pre[305], p[305];
vector<int> v[305];
int cnt = 0;
void dfs(int i, int f){
	for(int j = 0; j < v[i].size(); j++){
		if(v[i][j] != f){
			pre[v[i][j]] = i;
			dfs(v[i][j], i);
		}
	}
}
int solve(int k1, int k2, int t){\\判断k2是否为k1父节点,并且记录路径
	
	int j = 0, sign = 0;
	p[j++] = k1;
	while(pre[k1]){
		k1 = pre[k1];
		p[j++] = k1;
		if(k1 == k2){
			sign = 1;
			break;
		}
	}
	if(sign == 0)
	 return 0;
	if(t == 1){
		for(int i = 0; i < j; i++){
			route[cnt++] = p[i];
		}
	}
	else{
		for(int i = j-1; i >= 0; i--){
			route[cnt++] = p[i];
		}
	}
	return 1;
}
int main(){
	
   // freopen("in.txt", "r", stdin);
	int n, a, b;
	
	scanf("%d", &n);
	memset(d, -1, sizeof(d));
	for(int i = 0; i < n-1; i++){
		scanf("%d%d", &a, &b);
		d[a]++;
		d[b]++;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	d[1]++;
	dfs(1, -1);
	int e = 0;
	while(1){
		scanf("%d", k+e);
		e++;
		if(getchar() == '\n')
		 break;
	}
	solve(k[0], 1, 2);
	for(int i = 1; i < e; i++){
		
		int m = k[i-1];
		while(1){
			m = pre[m];
			d[m]--;
			if(d[m] > 0)
			 break;
		}
		solve(k[i-1], m, 1);
		if(solve(k[i], m, 2) == 0){
			puts("-1");
			return 0;
		}
	}
	solve(k[e-1], 1, 1);
	printf("%d", route[0]);
	for(int i = 1; i < cnt; i++){
		if(route[i] != route[i-1]){
			printf(" %d", route[i]);
		}
	}
	puts("");
	return 0;
}





标签:pre,format,int,route,Tree,Codeforces,++,k1,节点
From: https://blog.51cto.com/u_16158872/6464289

相关文章

  • CodeForces 4B Before an Exam(DP)
    思路:令dp[i][j]为做了i天用了j时间,然后随便转移一下就可以了#include<cstdio>#include<queue>#include<cstring>#include<iostream>#include<cstdlib>#include<algorithm>#include<vector>#include<map>#include<string>#......
  • CodeForces 4D Mysterious Present(DP)
    题意:你有一张长宽为x,y的卡片同时有n个盒子,长宽分别为xi,yi。然后问你卡片最多塞多少层盒子并且把这些盒子按照从里到外输出。思路:由于数据给小了,所以n^2的DP也是可以水过的~#include<iostream>#include<cstdio>usingnamespacestd;constintmaxn=5005;intx[maxn],y[maxn]......
  • CodeForces 645A Amity Assessment
    题意:给你一个2X2的华容道你,问能不能通过初始给出的棋盘然后变换到最后的棋盘思路:由于是一个2X2的...所以怎么做都可以..留意到每个棋子的移动其实都是顺时针或者逆时针的就好做了。#include<cstdio>#include<queue>#include<cstring>#include<iostream>#include<cstdlib>......
  • AtCoder Beginner Contest 258 F Main Street
    洛谷传送门AtCoder传送门发现这题有个远古的WA就来改了(发现走法很多种,不想分类讨论,考虑最短路。设起点编号为\(1\),终点为\(11\)。\(x=Bn\)和\(y=Bn\)把坐标系分成了若干块。考虑过起点作一条平行于\(Ox\)的直线,与左右两条\(x=Bn\)的线有两个交点,给它们编号......
  • 决策树DecisionTree
    模型亮点数据清洗方式得当由于模型、数据集太小,没有什么好调的,就当练习吧~-----------------------------------------以下为模型具体实现-----------------------------------------Step1.数据读取importpandasaspddf=pd.read_csv('bankdebt.csv',index_col=0,header......
  • Codeforces Round 877 (Div. 2)
    Preface补题这场补题的时候直接成腐乳了,A题挂两发,B题挂两发,C题挂一发是真的抽象虽然D是个套路题看一眼就秒了,但如果真的比赛时候打真要罚时爆炸了的说后面的EF还是做不来啊岂可修,不过都很有启发性让人感觉神清气爽,不像傻逼蓝桥杯花钱买罪受A.BlackboardList刚开始想错方......
  • mac下gitLab、sourceTree的配合使用
         1、认识一下gitLab这个版本管理工具。说到版本管理工具,大家会想到svn,git和svn还是有差别的。svn是集中化的版本控制系统,只有一个单一的集中管理的服务器,保存所有文件的修订版本,而协同工作的人们都通过客户端连到这台服务器,取出最新的文件或者提交更新。git是分布式......
  • Codeforces Round 876 Div2 A-D题解
    CodeforcesRound876Div2A-D题解A.TheGoodArray这个题就是问你对于\(i\leqn\),要求前面后面至少\(ceil(\frac{i}{k})\)个1那我们就贪心的每k个放一个1,或者直接用数学算一下就好了AC代码#include<bits/stdc++.h>usingnamespacestd;constexprintlimit=(......
  • 2023年6月11日,TreeSet,Comparable,HashMap
    1.Set1.TreeSetTreeSet1、存储Integer的元素,升序排列2、存储String的元素,字典排列TreeSet根据元素的不同类型使用不同的排序规则publicclasstest01{/***知识点:TreeSet*1、存储Integer的元素,升序排列*2、存储String的元素,字典排列*......
  • DSU on tree
    DSUonTreeDSUonTree是指在树上利用类似于并查集的启发式合并的方法处理一类统计的问题。比如这些问题:对于每一个子树T,计算:1)T中结点的值组成的众数2)T中不同的结点的值的个数3)T结点的值从小到大排序后最接近T的根结点的值以子树中不同的结点的值的个数为例,如果直接对每一......