[BZOJ 3319] 黑白树

时间:2023-03-08 23:27:29
[BZOJ 3319] 黑白树

3319: 黑白树

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 557  Solved: 194
[Submit][Status][Discuss]

Description

给定一棵树,边的颜色为黑或白,初始时全部为白色。维护两个操作:
1.查询u到根路径上的第一条黑色边的标号。
2.将u到v    路径上的所有边的颜色设为黑色。
Notice:这棵树的根节点为1

Input

第一行两个数n,m分别表示点数和操作数。
接下来n-?    1行,每行2个数u,v.表示一条u到v的边。
接下来m行,每行为以下格式:
1 v 表示第一个操作
2 v u 表示第二种操作

Output

对于每个询问,输出相应答案。如果不存在,输出0。

Sample Input

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

Sample Output

0
2
1

HINT

对于    100%    的数据:n,m<=10^6

看见这道题之后似乎可以用树剖来打但是$10^6$的数据范围显然对于树剖的巨大常数$O(nlogn)$是无法承受的

这题在$HZOJ$上的数据极其坑爹,卡几乎所有正解。。。网上找的$BZOJ$标程都$TLE$了。。。

然而不卡暴力..不卡暴力...暴力...(╯‵□′)╯︵┻━┻

正解似乎是线段树套平衡树=w=

目测数据是个菊花图。。。深度极其的浅导致依靠子树大小来减少时间消耗的正解被时间与深度相关的暴力程序力压。。。

最后弃疗怂一波用暴力A掉了这题QwQ

暴力袋马如下:

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> const int MAXE=;
const int MAXV=; struct Edge{
int from;
int to;
int id;
Edge* next;
}; Edge E[MAXE];
Edge* head[MAXV];
Edge* top=E; int n;
int m;
int id[MAXV];
int prt[MAXV];
int deep[MAXV];
bool color[MAXV]; void Initialize();
void Insert(int,int,int);
int Query(int);
void DFS(int,int,int,int);
void Modify(int,int); int main(){
int a,b,c;
Initialize();
DFS(,,,);
for(int i=;i<m;i++){
scanf("%d%d",&a,&b);
if(a==){
printf("%d\n",Query(b));
}
else if(a==){
scanf("%d",&c);
Modify(b,c);
}
}
return ;
} void Modify(int x,int y){
while(x!=y){
if(deep[x]<deep[y])
std::swap(x,y);
color[id[x]]=true;
x=prt[x];
}
} int Query(int x){
while(x!=){
if(color[id[x]])
return id[x];
else
x=prt[x];
}
return ;
} void DFS(int root,int prt,int deep,int id){
::id[root]=id;
::prt[root]=prt;
::deep[root]=deep;
for(Edge* i=head[root];i!=NULL;i=i->next){
if(i->to==prt)
continue;
DFS(i->to,root,deep+,i->id);
}
} void Initialize(){
int a,b;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%d%d",&a,&b);
Insert(a,b,i);
Insert(b,a,i);
}
} inline void Insert(int from,int to,int id){
top->id=id;
top->to=to;
top->from=from;
top->next=head[from];
head[from]=top;
top++;
}

Code

彪乘袋马可能会在我的GitHub Repository里更新QwQ

UPD 2017/07/31 补图w

[BZOJ 3319] 黑白树