Summer Training day6 codeforces 593D LCA+并查集

D. Happy Tree Party
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bogdan has a birthday today and mom gave him a tree consisting of n vertecies. For every edge of the tree i, some number xi was written on it. In case you forget, a tree is a connected non-directed graph without cycles. After the present was granted, m guests consecutively come to Bogdan's party. When the i-th guest comes, he performs exactly one of the two possible operations:

  1. Chooses some number yi, and two vertecies ai and bi. After that, he moves along the edges of the tree from vertex ai to vertex biusing the shortest path (of course, such a path is unique in the tree). Every time he moves along some edge j, he replaces his current number yi by , that is, by the result of integer division yi div xj.
  2. Chooses some edge pi and replaces the value written in it xpi by some positive integer ci < xpi.

As Bogdan cares about his guests, he decided to ease the process. Write a program that performs all the operations requested by guests and outputs the resulting value yi for each i of the first type.

Input

The first line of the input contains integers, n and m (2 ≤ n ≤ 200 0001 ≤ m ≤ 200 000) — the number of vertecies in the tree granted to Bogdan by his mom and the number of guests that came to the party respectively.

Next n - 1 lines contain the description of the edges. The i-th of these lines contains three integers uivi and xi (1 ≤ ui, vi ≤ nui ≠ vi1 ≤ xi ≤ 1018), denoting an edge that connects vertecies ui and vi, with the number xi initially written on it.

The following m lines describe operations, requested by Bogdan's guests. Each description contains three or four integers and has one of the two possible forms:

  • 1 ai bi yi corresponds to a guest, who chooses the operation of the first type.
  • 2 pi ci corresponds to a guests, who chooses the operation of the second type.
It is guaranteed that all the queries are correct, namely  1 ≤ ai, bi ≤ n1 ≤ pi ≤ n - 11 ≤ yi ≤ 1018 and  1 ≤ ci < xpi, where  xpi represents a number written on edge  pi at this particular moment of time that is not necessarily equal to the initial value  xpi, as some decreases may have already been applied to it. The edges are numbered from  1 to  n - 1 in the order they appear in the input.
Output

For each guest who chooses the operation of the first type, print the result of processing the value yi through the path from ai to bi.

Examples
input
6 6
1 2 1
1 3 7
1 4 4
2 5 5
2 6 2
1 4 6 17
2 3 2
1 4 6 17
1 5 5 20
2 4 1
1 5 1 3
output
2
4
20
3
input
5 4
1 2 7
1 3 3
3 4 2
3 5 5
1 4 2 100
1 5 4 1
2 2 2
1 1 3 4
output
2
0
2
题解:这道题目很有意思,求出亮点的LCA以后,我们从两个点出发向LCA开始遍历,把y来除以边上的权值,如果这些权值全都大于等于2的话,那么只需要进行最多64次就可以完成。但是这里有一个可能卡你的地方,那就是链上一堆长度为1得边,最差情况下每次询问都是O(n)的。我们可以这样想,把所有相邻的长度为1的边用并查集合并到一起,这样的话,往上走的过程中就会跳过大量的权值为1的边。注意,当结果为0的时候直接跳出就好了,不必找到LCA,不然还会超时。


其实我觉得还应该可以用树链剖分来做,估计会更简单,有空的话实现一下。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long  LL;
const int MAXN = 2e5 + 10;
int head[MAXN];
int cnt;
struct edge{ 
    int v; 
    int next; 
    LL cost; 
}Es[MAXN<<1];  

inline void add_edge(int i,int j,LL cost){   
    Es[cnt].v = j; 
    Es[cnt].cost = cost; 
    Es[cnt].next = head[i]; 
    head[i] = cnt++; 
}   
LL dep[MAXN],fa[MAXN],dist[MAXN];
void dfs(int u,int _dep){
	dep[u] = _dep;
	for(int e = head[u];e != -1;e = Es[e].next){
		int v = Es[e].v;
		if(v != fa[u]){
			LL cost = Es[e].cost;
			dist[v] = cost;
			fa[v] = u;
			dfs(v,_dep+1);
		}
		
	}
}
int es_u[MAXN],es_v[MAXN];LL es_w[MAXN];
int parent[MAXN];
int n,m;
int find(int x){
	return x == parent[x] ? x : parent[x] = find(parent[x]);
}
void init(){ 
    cnt = 0; 
    memset(head,-1,sizeof(head)); 
    for(int i = 0;i < MAXN;i++){
    	parent[i] = i; 
	}
}
void query(int u,int v,LL w){
	while(u != v && w){
		if(dep[u] < dep[v]) swap(u,v);
		if(dist[u] == 1){
			u = find(u);
		}
		else{
			w /= dist[u];
			u = fa[u];
		}
	}
	printf("%lld\n",w);
}
int main(){
	init();
	scanf("%d%d",&n,&m);
	for(int i = 1;i <= n-1 ;i++){
		int u,v;LL w ;
		scanf("%d%d%lld",&u,&v,&w);
		es_u[i] = u,es_v[i] = v,es_w[i] = w;
		add_edge(u,v,w);
		add_edge(v,u,w);
	}
	dfs(1,0);
	for(int i = 1;i <= n-1;i++){
		if(es_w[i] != 1) continue;
		int u = es_u[i],v = es_v[i];
		if(dep[u] < dep[v]) swap(u,v);
		parent[u] = find(v);
	}
	for(int i = 0;i < m;i++){
		int op;scanf("%d",&op);
		if(op == 1){
			int u,v;LL w;
			scanf("%d%d%lld",&u,&v,&w);
			query(u,v,w);
		}
		else{
			int id;LL w;
			scanf("%d%lld",&id,&w);
			int u = dep[es_u[id]] > dep[es_v[id]] ? es_u[id] : es_v[id];
			dist[u] = w;
			if(w == 1){
				parent[u] = find(es_u[id] + es_v[id] - u);
			}
		}
	}
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值