CodeForces - 593D (lHappy Tree Party lca+并查集 路径压缩)

CodeForces - 593D (lHappy Tree Party lca+并查集 路径压缩)

参考文章:https://blog.csdn.net/wzq_QwQ/article/details/49678155
看完这篇博客的思路 秒懂 对lca 并查集他们的强大之处很是佩服
牛逼
废话不多说 上题
题目描述
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:
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 bi using 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.
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.
输入
The first line of the input contains integers, n and m (2 ≤ n ≤ 200 000, 1 ≤ 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 ui, vi and xi (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ 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 ≤ n, 1 ≤ pi ≤ n - 1, 1 ≤ 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.
输出
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.
样例输入
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
样例输出
2
4
20
3
题意
给定一棵无根树,每条边有边权。
两种操作。
第一种询问给定值y(<=1018)y(<=1018)除以点u到点v的所有边的边权的值,注意每一次除都是下取整。
第二种是修改某边边权,并且每一次修改后该边边权相对原来一定减小。
边权为正整数(<=1018)(<=1018)
n<=2∗105n<=2∗105,询问次数m<=2∗105

心路历程
看的第一眼 想到了lca暴力向上搜,但是却没有想到路径压缩 压缩之后最多最多跑30条边 根跟不需要担心超时。。。太强了。
做题还是太少。。。并查集也是很巧妙
代码如下

#pragma GCC optimize(3,"Ofast","inline")  	//G++
#pragma comment(linker, "/STACK:102400000,102400000")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#include <functional>
#define TEST freopen("in.txt","r",stdin);
#define mem(a,x) memset(a,x,sizeof(a))
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull; // %llu
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod=1e9+7;
const ll INF = -1u>>1;
const ll maxn = 5e5+5;
ll n,m,root;
ll depth[maxn],father[maxn][30],lg[maxn],len[maxn],re[maxn];
ll from[maxn],to[maxn],val[maxn];
struct node
{
    ll to,len;
};
vector<node>edge[maxn];
ll fin(ll x)            //并查集 路径压缩 牛逼
{
    if(re[x]==x)
        return x;
    return re[x]=fin(re[x]);
}
void add(ll x,ll y,ll len)
{
    edge[x].push_back(node{y,len});
    edge[y].push_back(node{x,len});
}
void dfs(ll u,ll fa,ll lenth)       //fa到u的权值为lenth
{
    len[u]=lenth;           //爹到u的权值
    depth[u]=depth[fa]+1;     //深度
    father[u][0]=fa;            //倍增预处理
    for(ll i=1; (1<<i)<=depth[u]; i++)
    {
        father[u][i]=father[father[u][i-1]][i-1];
    }
    for(ll i=0; i<edge[u].size(); i++)
    {
        node v=edge[u][i];
        if(v.to!=fa)
        {
            dfs(v.to,u,v.len);
            if(v.len==1)//在回溯的时候递归路径压缩  儿子连上爹
            {
                ll fx=fin(v.to),fy=fin(u);
                re[fx]=fy;          
            }
        }
    }
}
ll LCA(ll x,ll y)           //模板
{
    if(depth[x]<depth[y])
        swap(x,y);
    while(depth[x]>depth[y])
    {
        x=father[x][lg[depth[x]-depth[y]]-1];
    }
    if(x==y)return x;
    for(ll i=lg[depth[x]]-1; i>=0; i--)
    {
        if(father[x][i]!=father[y][i])
        {
            x=father[x][i];
            y=father[y][i];
        }
    }
    return father[x][0];
}
ll solve(ll a,ll b,ll lca,ll y)
{
//    ll cnt=0;
    while(depth[a]>depth[lca])
    {
        ll aa=fin(a);
        if(depth[aa]<=depth[lca]) break;    //自己做肯定会忘记
        y/=len[aa];
//        cnt++;
//        if(cnt>64) return 0;          //其实没有必要加这个条件 思路就是告诉我们及时跳出 避免超时
        if(y==0) return 0;
        a=father[aa][0];            //jump
    }
    while(depth[b]>depth[lca])
    {
        ll bb=fin(b);
        if(depth[bb]<=depth[lca]) break;
        y/=len[bb];
//        cnt++;
//        if(cnt>64)return 0;
        if(y==0) return 0;
        b=father[bb][0];
    }
    return y;
}
void update(ll pos,ll val)
{
    ll x=from[pos],y=to[pos];
    if(father[y][0]==x) swap(x,y);      //必须保证x的爹是y 很重要
    len[x]=val;
    if(val==1)              //路径压缩  太吊了吧
    {
        ll fx=fin(x),fy=fin(y);     //如果爹到儿子的距离为1 儿子的祖先连上爹的祖先 重点 重点 
        re[fx]=fy;      //因为查询时时从下往上遍历 也就是从儿子找爹的方式遍历 路径压缩 不压缩会超时
    }
}
main()
{
//    TEST
    ios;
    cin>>n>>m;
    for(ll i=1; i<=n; i++)
    {
        lg[i]=lg[i-1]+(1<<lg[i-1]==i);
        re[i]=i;
        if(i!=n)
        {
            cin>>from[i]>>to[i]>>val[i];
            add(from[i],to[i],val[i]);
        }
    }
    dfs(1,0,0);         //设1为根节点1的爹为0 爹到儿子的距离为0
    while(m--)
    {
        ll op;
        cin>>op;
        if(op==1)
        {
            ll a,b,val;
            cin>>a>>b>>val;
            ll lca=LCA(a,b);
            ll ans=solve(a,b,lca,val);
            cout<<ans<<"\n";
        }
        else
        {
            ll a,b;
            cin>>a>>b;
            update(a,b);
        }
    }
}

如何看不懂 就看下那个博客 启发很大 换句话说 还是自己太菜
参考文章:https://blog.csdn.net/wzq_QwQ/article/details/49678155

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值