D题,LCA是很明显的。要注意的是,因为是除法,所以最多可以除x>2的有64次,当大于64时可以直接返回0。而且注意到可能会有很多值为1的边,可以使用路径压缩,把边为1的边压缩掉,类似于并查集的路径压缩。

之前只压缩到LCA,一直TLE,可以直接压缩到它们的根节点。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define LL long long
using namespace std; const int MAX=200050; int head[MAX],tot;
struct Edge{
int u,v,next;
LL w;
}edge[MAX*2];
LL w[MAX]; int pre[MAX],depth[MAX],edno[MAX],par[MAX][20];
bool vis[MAX];
queue<int>que; void addedge(int u,int v,LL w){
edge[tot].u=u;
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
} int n,m; void BFS(int u){
que.push(u);
depth[u]=1;
edno[u]=0;
while(!que.empty()){
u=que.front();
vis[u]=true;
que.pop();
for(int e=head[u];e!=-1;e=edge[e].next){
int v=edge[e].v;
if(vis[v]) continue;
depth[v]=depth[u]+1;
par[v][0]=u;
if(w[edno[u]]==1){
pre[v]=pre[u];
edno[v]=edge[e].w;
}
else{
pre[v]=u;
edno[v]=edge[e].w;
}
que.push(v);
}
}
} void init(){
int i,j;
for(j=1;(1<<j)<=n;j++)
for(i=1;i<=n;i++)
if(par[i][j-1]!=-1)
par[i][j]=par[par[i][j-1]][j-1];
} int LCA(int a,int b)//最近公共祖先
{
int i,j;
if(depth[a]<depth[b])swap(a,b);
for(i=0;(1<<i)<=depth[a];i++);
i--;
//使a,b两点的深度相同
for(j=i;j>=0;j--)
if(depth[a]-(1<<j)>=depth[b])
a=par[a][j];
if(a==b)return a;
//倍增法,每次向上进深度2^j,找到最近公共祖先的子结点
for(j=i;j>=0;j--){
if(par[a][j]!=-1&&par[a][j]!=par[b][j]){
a=par[a][j];
b=par[b][j];
}
}
return par[a][0];
} LL anum[70];
LL bnum[70]; LL query(int a,int b,LL y){
int ret=(int)(log((double)y)/log(2.0));
int lca=LCA(a,b);
/// cout<<lca<<" "<<ret<<endl;
int ac=0,bc=0;
while(a!=-1&&depth[a]>depth[lca]){
//// cout<<a<<" "<<w[edno[a]]<<endl;
if(w[edno[a]]>1){
y/=w[edno[a]];
if(y==0) return 0;
}
int tp=pre[a];
while(pre[tp]!=-1&&w[edno[tp]]==1){
tp=pre[tp];
}
int rt=tp; tp=a;
while(pre[tp]!=rt){
int tmp=pre[tp];
pre[tp]=rt;
tp=tmp;
}
a=rt;
}
LL ty=y;
while(b!=-1&&depth[b]>depth[lca]){
/// cout<<b<<" "<<edno[b]<<endl;
if(w[edno[b]]>1){
ty/=w[edno[b]];
bnum[bc++]=w[edno[b]];
if(ty==0) return 0;
}
int tp=pre[b];
while(pre[tp]!=-1&&w[edno[tp]]==1){
tp=pre[tp];
}
int rt=tp; tp=b;
while(pre[tp]!=rt){
int tmp=pre[tp];
pre[tp]=rt;
tp=tmp;
}
b=rt;
}
/// cout<<ac<<" "<<bc<<endl;
/// cout<<bnum[bc-1]<<endl;
/// for(int i=0;i<ac;i++) y/=anum[i];
for(int i=bc-1;i>=0;i--) y/=bnum[i];
return y;
} int main(){
int u,v,op,a,b,p,c;
LL y;
while(scanf("%d%d",&n,&m)!=EOF){
memset(pre,-1,sizeof(pre));
memset(head,-1,sizeof(head));
memset(depth,0,sizeof(depth));
memset(vis,false,sizeof(vis));
memset(w,0,sizeof(w));
memset(par,-1,sizeof(par));
memset(edno,0,sizeof(edno));
tot=0;
for(int i=1;i<n;i++){
cin>>u>>v>>w[i];
addedge(u,v,i);
addedge(v,u,i);
}
BFS(1);
/// cout<<pre[2]<<" "<<edno[2]<<endl;
init();
for(int i=1;i<=m;i++){
scanf("%d",&op);
if(op==1){
cin>>a>>b>>y;
cout<<query(a,b,y)<<endl;
}
else{
cin>>p>>y;
w[p]=y;
}
}
}
return 0;
}

  

CF #329 D的更多相关文章

  1. CF #329 C

    C题我还以为是拉格朗日插值... 其实可以想象到,必须有这样一个函数,经过某一点时,其它圆相关的函数要为0. 于是,可以构造这样的一个函数,对于x有 (x/2)*(1-abs(t-i)+abs(1-a ...

  2. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  3. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  4. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  5. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  6. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  7. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  8. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

  9. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

随机推荐

  1. [Swift通天遁地]五、高级扩展-(3)日期和时间类型的扩展方法

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. [Swift通天遁地]七、数据与安全-(15)使用单元测试进行代码的性能分析

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. PHP网站 通过js方式判断是否是手机访问,若是 跳转到手机版网址!

    <script type="text/javascript" src="http://i3.dukuai.com/ui/js/jquery-1.32pack.js& ...

  4. ACM_百度的面试(单调栈)

    百度的面试 Time Limit: 2000/1000ms (Java/Others) Problem Description: 在一个二维平面,从左到右竖立n根高度分别为:a[1],a[2],... ...

  5. Spring-Security-OAuth2微信网页授权

    @Controller public class Controller1 { @Autowired private OAuth2ClientContext context; @Bean @Scope( ...

  6. JQuery 记第N次被坑 - ajax请求字符集问题

    前言:两个功能差不多的页面,都是使用$.post()请求后台,页面A传递到后台的中文参数正常,页面B传递到后台的中文参数则为乱码 分析过程: ①使用chrome的开发者工具,分析两个页面的ajax请求 ...

  7. Moto P30(XT1943-1) 免解锁BL 免rec 保留数据 Magisk Xposed ROOT 救砖 ZUI 4.0.374

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  8. 注释及Scriptlet

    1.显式注释 <!--xxxxxx-->,在正常页面上也看不出来,右键鼠标查看源代码可以看到 2.隐式注释 //xxxxx /*xxxxxx*/ <%--xxxxxxx--> ...

  9. anguar相关

    1.创建组件 在某目录下创建组件  ng g c content/membersManage 2.创建服务 在某目录下创建服务  ng g service services/storage 2.创建模 ...

  10. vsftpd:500OOPS:vsftpd:refusingtorunwithwritablerootinsidechroot()错误的解决方法

    当我们限定了用户不能跳出其主目录之后,使用该用户登录FTP时往往会遇到这个错误: 500 OOPS: vsftpd: refusing to run with writable root inside ...