bzoj2759
题解:
lct+解线性方程组
首先先把每一个环搞出来,然后再建立一个额外的点
然后解方程。。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N=,M=;
int n,Q,tim,fa[N],dir[N],x,k,p,b,vis[N],ch[N][],ni[N];
char s[];
int which(int x){return ch[fa[x]][]==x;}
struct node
{
int k,b;
node(){}
node(int k,int b):k(k),b(b){}
friend node operator + (const node &r1,const node &r2)
{return node(r1.k*r2.k%M,(r2.k*r1.b%M+r2.b)%M);}
}a[N],sum[N];
void dfs(int x)
{
if (vis[x])return;
vis[x]=tim;
if (vis[fa[x]]==tim)
{
dir[x]=fa[x];
fa[x]=;
}
else dfs(fa[x]);
}
int isroot(int x){return !fa[x]||ch[fa[x]][which(x)]!=x;}
void pushup(int x)
{
sum[x]=a[x];
if (ch[x][])sum[x]=sum[ch[x][]]+a[x];
if (ch[x][])sum[x]=sum[x]+sum[ch[x][]];
}
void rotate(int x)
{
int y=fa[x],k=which(x);
ch[y][k]=ch[x][k^];
ch[x][k^]=y;
if (!isroot(y))ch[fa[y]][which(y)]=x;
fa[x]=fa[y];fa[y]=x;
fa[ch[y][k]]=y;
pushup(y);pushup(x);
}
void splay(int x)
{
for (int y=fa[x];!isroot(x);rotate(x),y=fa[x])
if (!isroot(y))rotate((ch[y][]==x)==(ch[fa[y]][]==y)?y:x);
}
void access(int x)
{
int t=;
while(x)
{
splay(x);
ch[x][]=t;
pushup(x);
t=x;x=fa[x];
}
}
int findroot(int x)
{
access(x);splay(x);
while(ch[x][])x=ch[x][];
splay(x);
return x;
}
int inc(int x,int y)
{
access(dir[y]);splay(dir[y]);
splay(x);
return x==dir[y]||!isroot(dir[y]);
}
void cut(int x){access(x);splay(x);ch[x][]=fa[ch[x][]]=;pushup(x);}
int main()
{
ni[]=;
for (int i=;i<M;i++)ni[i]=(M-M/i)*ni[M%i]%M;
scanf("%d",&n);
for (int i=;i<=n;i++)scanf("%d%d%d",&a[i].k,&fa[i],&a[i].b);
for (int i=;i<=n;i++)
if (!vis[i])tim++,dfs(i);
scanf("%d",&Q);
while (Q--)
{
scanf("%s",s+);
if (s[]=='A')
{
scanf("%d",&x);
access(x);splay(x);
node t1=sum[x];
int t=findroot(x);
access(dir[t]);splay(dir[t]);
node t2=sum[dir[t]];
if (t2.k==)
{
if (t2.b)puts("-1");
else puts("-2");
continue;
}
int v1=ni[(-t2.k+M)%M]*t2.b%M;
printf("%d\n",(t1.k*v1%M+t1.b)%M);
}
else
{
scanf("%d%d%d%d",&x,&k,&p,&b);
access(x);splay(x);
a[x]=node(k,b);pushup(x);
int t=findroot(x);
if (t==x)
{
if (findroot(p)==t)dir[t]=p;
else dir[t]=,fa[t]=p;
}
else
{
if (inc(x,t))
{
cut(x);
access(t);splay(t);
fa[t]=dir[t];dir[t]=;
if (findroot(p)==x)dir[x]=p;
else fa[x]=p;
}
else
{
cut(x);
if (findroot(p)==x)dir[x]=p;
else fa[x]=p;
}
}
}
}
return ;
}
bzoj2759的更多相关文章
- 【bzoj2759】一个动态树好题
Portal -->bzoj2759 Solution 哇我感觉这题真的qwq是很好的一题呀qwq 很神qwq反正我真的是自己想怎么想都想不到就是了qwq 首先先考虑一下简化版的问题应该怎么解决 ...
- BZOJ2759: 一个动态树好题
BZOJ2759: 一个动态树好题 Description 有N个未知数x[1..n]和N个等式组成的同余方程组:x[i]=k[i]*x[p[i]]+b[i] mod 10007其中,k[i],b[i ...
- BZOJ2759 一个动态树好题 LCT
题解: 的确是动态树好题 首先由于每个点只有一个出边 这个图构成了基环内向树 我们观察那个同余方程组 一旦形成环的话我们就能知道环上点以及能连向环上点的值是多少了 所以我们只需要用一种结构来维护两个不 ...
- BZOJ2759一个动态树好题 LCT
题如其名啊 昨天晚上写了一发忘保存 只好今天又码一遍了 将题目中怕$p[i]$看做$i$的$father$ 可以发现每个联通块都是一个基环树 我们对每个基环删掉环上一条边 就可以得到一个森林了 可以用 ...
- LCT专题练习
[bzoj2049]洞穴勘测 http://www.cnblogs.com/Sdchr/p/6188628.html 小结 (1)LCT可以方便维护树的连通性,但是图的连通性的维护貌似很麻烦. [bz ...
随机推荐
- Python开发【Django】:Model操作(二)
Model操作 1.操作汇总: # 增 # # models.Tb1.objects.create(c1='xx', c2='oo') 增加一条数据,可以接受字典类型数据 **kwargs # obj ...
- 使用linuxbridge + vlan网络模式
#openstack pike 使用 linuxbridge + vlan openstack pike 集群高可用 安装部署 汇总 http://www.cnblogs.com/elvi/p/76 ...
- Web开发:URL编码与解码(转)
原文:http://www.cnblogs.com/greatverve/archive/2011/12/12/URL-Encoding-Decoding.html 通常如果一样东西需要编码,说明这样 ...
- mysql 数据操作 单表查询 where约束 练习
create table employee( id int not null unique auto_increment, name ) not null, sex enum('male','fema ...
- [WorldWind学习]18.High-Performance Timer in C#
In some applications exact time measurement methods are very important. 一些应用程序中精确的时间测量是非常重要的. The of ...
- [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...
- linux进程间通讯的几种方式的特点和优缺点
# 管道( pipe ):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用.进程的亲缘关系通常是指父子进程关系.# 有名管道 (named pipe) : 有名管道也是 ...
- DNS(bind)添加A、CNAME、MX、PTR记录、智能DNS(ACL)
1.添加一条A记录(记得更改serial): vim /var/named/chroot/etc/lnh.com.zone 重启一下: rndc reload 查看从服务器: 测试结果: master ...
- 排序问题Java
package zhuzhuangtu; import java.util.*; import java.io.*; public class Main{ public static void mai ...
- Bootstrap fileinput v2.0(ssm版)
前言bootstrap fileinput是一个很好的文件上传插件.但是官方不出api,这就尴尬了.百度一下,每个人写法都不相同,好多代码本身都是错的.我修改后才能跑起来.综上所述:所以今天我摸索了一 ...