Problem 3. seqmod
Input file: seqmod.in
Output file: seqmod.out
Time limit: 2 seconds
Memory limit: 256 MB
给你一棵无根树,边有边权,且是[0; 9] 之间的整数,给你m 个询问,每次询问两个点u; v 之间的路径
的边的边权顺次连接起来后构成的那个数字取模于31。
Input
第1 行2 个整数:n m,表示树的节点个数和询问数。
接下来n �� 1 行,每行3 个数:u v d 表示点u 和点v 之间有一条边权为d 的边。
接下来m 行,每行2 个整数:u v 表示一个询问。
Output
对于每个询问输出其答案。
Sample
seqmod.in seqmod.out
5 3
1 2 2
1 3 8
3 4 9
3 5 2
1 4
2 5
5 2
27
24
6
样例中三条路径对应的数字分别是:89,582,285,它们被31 取模后为:27,24,6。
Note
• 对于30% 的数据,1 n;m 103
• 对于100% 的数据,1 n;m 105,1 u; v n,u ̸= v,0 d 9
3.1 30%
随便怎么暴力都可以...
3.2 100%
就是有序链剖,细节我课上讲过了,大家看一下代码可以看懂的.与普通链剖
的区别就是这个东西要在乎顺序,所以不能用swap 那种写法,两个各跳各的,
还要注意合并的顺序.
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=100000 + 10;
const int M=N<<1;
const int Mod=31;
template<class T>inline void readin(T &res){
static char ch;
while((ch=getchar())<'0'||ch>'9');res=ch-48;
while((ch=getchar())<='9'&&ch>='0')res=res*10+ch-48;
}
struct Point{
int v,len;
Point(){}
Point(int v,int len):v(v),len(len){}
};
int head[N],wght[M],dest[M],last[M],etot;
int depf[N],siz[N],son[N],top[N],father[N],val[N],in[N],rank[N],deps[N],pos_id;
struct Node{
Point ab,ba;
Node *ls,*rs;
}point_pool[N*4],*tail=point_pool,*root;
void init(){
deps[0]=1;
for(register int i=1;i<N;i++)
deps[i]=deps[i-1]*10%Mod;
}
Point link(const Point &a,const Point &b){
return Point((a.v * deps[b.len] + b.v) % Mod,a.len + b.len);
}
Node *build(int lf,int rg){
Node *nd=++tail;
if(lf == rg){
nd->ab=nd->ba=Point(val[rank[lf]],1);
} else{
int mid=(lf + rg)>>1;
nd->ls=build(lf,mid);
nd->rs=build(mid + 1,rg);
nd->ab=link(nd->ls->ab,nd->rs->ab);
nd->ba=link(nd->rs->ba,nd->ls->ba);
}
return nd;
}
Point query_ab(Node *nd,int lf,int rg,int L,int R){
if(L <= lf && rg <= R) return nd->ab;
int mid=(lf + rg)>>1;
if(R <= mid) return query_ab(nd->ls,lf,mid,L,R);
else if(L>mid) return query_ab(nd->rs,mid+1,rg,L,R);
else return link(
query_ab(nd->ls,lf,mid,L,R),
query_ab(nd->rs,mid+1,rg,L,R));
}
Point query_father(Node *nd,int lf,int rg,int L,int R){
if(L <= lf && rg <= R) return nd->ba;
int mid=(lf + rg)>>1;
if(R <= mid) return query_father(nd->ls,lf,mid,L,R);
else if(L>mid) return query_father(nd->rs,mid+1,rg,L,R);
else return link(
query_father(nd->rs,mid+1,rg,L,R),
query_father(nd->ls,lf,mid,L,R));
}
void adde(int u,int v,int d){
etot++;
dest[etot]=v;
last[etot]=head[u];
wght[etot]=d;
head[u]=etot;
}
void dfs1(int u){
siz[u]=1;
for(register int t=head[u]; t; t=last[t]){
int v=dest[t];
if(v == father[u]) continue;
father[v]=u;
depf[v]=depf[u] + 1;
val[v]=wght[t];
dfs1(v);
siz[u] += siz[v];
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
void dfs2(int u){
in[u]=++pos_id;
rank[pos_id]=u;
if(son[u]){
top[son[u]]=top[u];
dfs2(son[u]);
}
for(register int t=head[u]; t; t=last[t]){
int v=dest[t];
if(v == father[u] || v == son[u]) continue;
top[v]=v;
dfs2(v);
}
}
int query(int u,int v){
Point y_x(0,0),x_y(0,0);
while(top[u]!=top[v]){
if(depf[top[u]]>depf[top[v]]){
y_x=link(y_x,query_father(root,1,pos_id,in[top[u]],in[u]));
u=father[top[u]];
} else{
x_y=link(query_ab(root,1,pos_id,in[top[v]],in[v]),x_y);
v=father[top[v]];
}
}
if(depf[u]>depf[v]){
y_x=link(y_x,query_father(root,1,pos_id,in[v]+1,in[u]));
} else if(depf[v]>depf[u]){
x_y=link(query_ab(root,1,pos_id,in[u]+1,in[v]),x_y);
}
Point ans=link(y_x,x_y);
return ans.v;
}
int n,m;
int main(){
freopen("seqmod.in","r",stdin);
freopen("seqmod.out","w",stdout);
readin(n),readin(m);
for(register int i=1;i<n;i++){
int u,v,d;
readin(u),readin(v),readin(d),
adde(u,v,d);
adde(v,u,d);
}
init();
father[1]=1;
depf[1]=0;
dfs1(1);
top[1]=1;
dfs2(1);
root=build(1,pos_id);
while(m--){
int u,v;
readin(u),readin(v);
printf("%d\n",query(u,v));
}
return 0;
}