森林中动态树结构与路径操作算法问题研究.pdfVIP

  • 0
  • 0
  • 约3.04千字
  • 约 4页
  • 2025-10-22 发布于北京
  • 举报

森林中动态树结构与路径操作算法问题研究.pdf

ProblemH

HappyPainting!

Thereisaforestofcolorfulrootedtreescontainingnnodes.Youaregivenm

operations.Executethemonebyone,andoutputtheresults.

1xyc

Changexsfathertoy.Ifx=yorxisaancestorofy,simplyignoreit.Theedge

weenxanditsoldfatherisremoved,andthenewedgeshouldbepaintedwith

colorc.

2xyc

Paintalltheedgesalongthepathx-ywithcolorc.Ifthereisnopathweenx

andy,simplyignoreit.

3xy

Countthenumberofedgesalongthepathx-y,andthetotalnumberofcolorsamong

theseedges.

Input

Theinputcontainsseveraltestcases.Thefirstlineofeachtestcasecontains

twointegersnandm(1=n=50,000,1=m=200,000).Nodesarenumberedfrom1to

n.ThesecondlinecontainsnintegersF[i](0=F[i]=n),thefatherofeachnode

(F[i]=0meansthenodeistherootofatree).Thenextlinecontainsnintegers

C[i](1=C[i]=30),thecolorsoftheedgesweeneachnodeanditsfather(for

rootnodes,thecorrespondingcolorshouldbeignored).Eachofthenextmlines

containsanoperation.Foralloperations,1=x,y=n,foreachtype-2operation,

1=c=30.Theinputisterminatedbyend-of-file(EOF).Thesizeofinputfile

doesnotexceed5MB.

Output

Foreachtype-3operation,outputtwointegers:thenumberofedgesandthenumber

ofcolorsamongtheseedges.

SampleInput

66

346

问题H

绘画快乐!

有一个由彩色根树组成的森林,其中包含n个节点。给你m次操作。一一执行,并输出结果。

1xyc

将x的父亲改为y。如果x=y或x是y的祖先,则忽略它。x和它的旧父亲之间的边被删除,

新的边应该涂上颜色c。

2xy坐标

用颜色c沿路径x‑y绘制所有边。如果x和y之间没有路径,则忽略它。

3xy

计算沿路径x‑y的边数,以及这些边中的颜色总数。

输入

文档评论(0)

1亿VIP精品文档

相关文档