CF-29D - Ant on the Tree(DFS+路径保存回扫)

全局最小割 一个无向连通网络,去掉一个边集可以使其变成两个连通分量则这个边集就是割集;最小割集当然就权和最小的割集。 可以用最小切割最大流定理: 1.min=MAXINT,确定一个源点 2.枚举汇点 3.计算最大流,并确定当前源汇的最小割集,若比min小更新min 4.转到2直到枚举完毕 5.min即为所求输出min     不难看出复杂度很高:枚举汇点要O(n),最 阅读详情

D - Ant on the Tree
Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Description

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 edges so that there is a path between any pair of vertexes. A leaf is a distinct from root vertex, which is connected with exactly one other vertex.

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 is amount of leaves in the tree. These numbers describe the order in which the leaves should be visited. It is guaranteed that each leaf appears in this order exactly once.

Output

If the required route doesn't exist, output -1. Otherwise, output 2n - 1 numbers, describing the route. Every time the ant comes to a vertex, output it's index.

Sample Input

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

思路:先DFS一遍存路径,然后,在去找按叶子节点遍历的路径。


#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
const int mm=310;
int f[mm],ff[mm],q[mm];
vector<int>g[mm],p;
int n;
void dfs(int u,int fa)
{
  f[u]=fa;///存路径
  for(int i=0;i<g[u].size();i++)
    if(g[u][i]^fa)
    dfs(g[u][i],u);
}
void add(int u,int v)///增加从u到v的路径
{ memset(ff,0,sizeof(ff));
  memset(q,0,sizeof(q));
  int k=0;
  while(v>0){ff[v]=k;q[k++]=v;v=f[v];}///从v开始回找记录
  while(u>0)///有解
  {
    if(ff[u]>0)///从u开始到v有路径
    {
      for(int i=ff[u]-1;i>=0;i--)p.push_back(q[i]);
      break;
    }
    ///如果当前u到v没路径,继续下走
    u=f[u];
    p.push_back(u);
  }
}
int main()
{
  while(cin>>n)
  { int a,b;
     p.clear();
    for(int i=0;i<mm;i++)g[i].clear();
    for(int i=0;i<n-1;i++)
    {
      cin>>a>>b;g[a].push_back(b);g[b].push_back(a);
    }
      dfs(1,-1);///遍历一遍,存遍历路径
      //cout<<"yes";
      int u=1,v;
      for(int i=2;i<=n;i++)
        if(g[i].size()==1)
        { int z;cin>>z;
          add(u,z);u=z;
        }
        ///cout<<'U'<<u<<endl;
        add(u,1);
        n=n+n-1;
        if(p.size()!=n)cout<<"-1\n";
        else
        {
          cout<<1;
          for(int i=0;i<p.size()-1;i++)
            cout<<" "<<p[i];
          cout<<"\n";
       }
  }
}



【信息科学与工程学】计算机科学与技术——第七十七篇 系统架构设计02 编号类型模型配方企业资本运作的方法/规则/条件/依据及所有的步骤(​风险处理/异常处理/危机应对关联知识法律法规和裁决依据​​由发起人(通常是知名投资人、投行或产业资本)先设立一个只有现金、无实际业务的特殊目的收购公司(SPAC),通过IPO募集资金并在交易所上市。然后在规定时间内(通常24个月)寻找一家非上市公司进行合并(De-SPAC),使目标公司迅速实现上市,规避传统IPO的复杂流程和不确定性。1.:在开曼或特拉华注册空壳公司,注入少量种子资金。2. 阅读详情

相关推荐

Cadence Allegro新手避坑:从原理图到PCB,解决‘netrev.lst does not exist’的完整流程

本文详细介绍了Cadence Allegro从原理图设计到PCB导入网表的完整流程,特别针对新手常见的'netrev.lst does not exist'错误提供了系统解决方案。内容包括环境准备、原理图设计规范、网表生成与导入技巧,以及设计数据管理最佳实践,帮助工程师高效完成PCB设计任务。

weixin_42685438的博客 487

CF29D 树的遍历

http://codeforces.com/problemset/problem/29/D D. Ant on the Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard out

ACM,再见! 1180

简单搭建 可视化日志+链路追踪

JAVA 可视化日志、链路追踪、plumeLog、Tlog、Logback

qq_39535439的博客 1204

cf29d 深搜,dfs序

#include<bits/stdc++.h> using namespace std; #define maxn 500 struct Edge{int to,nxt;}edge[maxn<<1]; int n,head[maxn],tot,a[maxn],k; vector<int>v; void init(){ memset(he...

weixin_30765475的博客 124

备战数学建模38-粒子群算法pso番外篇1(攻坚战2)

粒子群算法最初是根据鸟类群体行为得到的启发,对鸟群行为进行仿真的算法,是一种启发式算法,也就是智能优化算法,利用信息共享,通过中间信息进行改进搜索,从而获得问题的可行解。我们简单从下图去体会一下粒子群算法,就是有多个鸟,每个鸟相当于一个粒子,每只鸟的位置受自己认知的历史最佳位置和当前距离食物最近的鸟的位置的影响。我们进一步分析这个图,可以得到两个公式,第一个是速度公式,即每只鸟的当前速度受上一步的速度惯性,自我认知和社会认知三部分影响,第二个是位置公式,即每只鸟的位置受上一位置和上一位置的速度的影响。

nuist_NJUPT的博客 1664

【异常】出现“‘git‘ 不是内部或外部命令,也不是可运行的程序”的错误

出现“‘git’ 不是内部或外部命令,也不是可运行的程序”的错误通常意味着 Git 没有被正确安装在你的系统上,或者其可执行文件的路径没有被添加到系统的环境变量中。

奶爸的编程之路,也就一周冷个三天 1389

CodeForces 29D Ant on the Tree

给一颗树,1为根,要求遍历树上所有点,给出叶子结点的访问顺序,限制每条边至多访问两次。 首先这是一棵树,那么两点之间的路线是确定的,所以我第一遍dfs预处理出从根节点到每个叶子的路径保存,以便后面输出。 那么就按照题目要求输出叶子结点的顺序依次输出,然后从一个叶子到下一个叶子的时候,从他们的最近公共祖先转折,所以我还预处理了相邻两个叶子结点的LCA。 #include

Unknown 1004

【OpenCV】第二十三章: 图像特征检测之SURF、FAST、BRIEF、OBR算法

4、由于在检测特征点时是需要对图像中所有的像素点进行检测,然而图像中的绝大多数点都不是特征点,如果对每个像素点都进行上述的检测过程,那显然是种浪费,所以FAST采用一种进行非特征点判别的方法:首先对候选点的周围90度的点(1,9,5,13)四个点进行测试,而且是先测试1和9,如果它们两个符合阈值要求再测试5和13。继SIFT和SURF后,大家在特征点检测的速度上又进行了很多的改进尝试,但速度的提升效果都不是非常明显,直到FAST算法诞生,特征点检测的速度才有了一个质的飞跃。无法计算描述符的特征点将被删除。

因为热爱所以坚持 2158

Codeforces 29D Ant on the Tree 树的遍历 dfs序

题目链接:点击打开链接 题意: 给定n个节点的树 1为根 则此时叶子节点已经确定 最后一行给出叶子节点的顺序 目标: 遍历树并输出路径,要求遍历叶子节点时按照给定叶子节点的先后顺序访问。 思路: 给每个节点加一个优先级。 把最后一个叶子节点到父节点的路径上的点优先级改为1 把倒数第二个叶子节点到父节点的路径上的点优先级改为2 如此每个点就有一个优先级,每个访问儿子节

九野的博客 1688

最强AI插件Augment,适配idea、vscode,对标cursor!

Augment是一款新兴的AI辅助编码工具,能够无缝集成到IDEA和VS Code等主流IDE中,直接对标专业AI编码工具Cursor。其核心优势包括多IDE适配、无缝集成、代码理解与生成以及对话式编程体验。Augment通过插件形式保留原IDE功能和快捷键习惯,降低学习成本,同时提供深度理解项目上下文和生成高质量代码的能力。此外,Augment还具备审查变更、检查点、代理与代理自动等核心功能,支持从简单配置更改到复杂功能实现的各种开发任务。Augment代表了AI辅助编程工具的新方向,通过增强开发者熟悉的

todoitbo的博客 1万+

VSCode + QMT 量化交易完整解决方案!

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 1327

DFS+路径存储

King Arthur's Knights Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 525Accepted Submission(s): 111Special Judge Problem Descripti...

weixin_30929295的博客 577

利用XGBoost实现短期电力负荷预测

本数据集为某地区2009年-2015年的用电负荷,时间片为每分钟这里我们定义了XGB模型作为实例,来进行模型训练,之后我们会对其使用网格搜索找到最优的参数。

优快云 精品推荐 4495

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

原题链接 D. Ant on the Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Connected undirected

L. 433

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

CodeForces 29D - Ant on the Tree 暴力LCA

题意:                     给一颗树...问能否从根节点出发回到根结点..并且每条边exactly经过两次..并且遍历叶子的顺序为所给的顺序..如果可以输出遍历路径..否则输出-1          题解:                     数据范围很小(N Program: #include #include #include #include #in

zzyzzy12 1304

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Ant on the Tree CodeForces - 29D(Lca+暴力)

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 ...

starlet_kiss的博客 391

codeforces 29D Ant on the Tree (dfs,tree,最近公共祖先)

D. Ant on the Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Connected undirected graph without cycle...

weixin_30314793的博客 208

uva 10917 Walk Through the Forest(最短路+DP路径,4级)

Description Problem C: A Walk Through the Forest Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he lik

nealgavin的专栏 2130

CF-29C - Mail Stamps(DFS+离散化)

C - Mail Stamps Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 29C Description

nealgavin的专栏 1707

hdu 4756 Install Air Conditioning(MST+全边次小树,4级)

Install Air Conditioning Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 174    Accepted Submission(s): 23 Problem Description   

nealgavin的专栏 1443

hdu 4650 Minimum Average Weight Path(最短路,5级)

第4场多校联合训练解题报告——详见杭电ACM微博~ Minimum Average Weight Path Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 94    Accepted Submissio

nealgavin的专栏 1389

UVA1416 Warfare And Logistics(LA4080)(最短路,5级)

Description The army of United Nations launched a new wave of air strikes on terrorist forces. The objective of the mission is to reduce enemy's logistical mobility. Each air strike will destro

nealgavin的专栏 1342

HDU 1068 Girls and Boys(二分图最大独立点集,3级)

Girls and Boys Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status   Appoint description:  System Crawler  (2012-10-30) Description the secon

nealgavin的专栏 1229

uva 1364 - Knights of the Round Table(点双连通分量,5级)

Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising tha

nealgavin的专栏 1224

ZOJ 3780 Paint the Grid Again(topsort)

ZOJ Problem Set - 3780 Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black

nealgavin的专栏 1209
上一篇: CF-27E - Number With The Given Amount Of Divisors(枚举+dfs)
下一篇: CF-29C - Mail Stamps(DFS+离散化)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值