• [SPOJ10707]Count on a tree II


    luogu

    题意

    给定一个n个节点的树,每个节点表示一个整数,问u到v的路径上有多少个不同的整数。

    sol

    也就是路径数颜色。树上莫队板子题。
    我这种分块的姿势貌似是假的。
    所以跑的是最慢的QAQ。

    update 2018.4.5:真的是假的明明不带修改我块的大小还设的(n^{0.6})(sqrt n)即可。

    code

    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int gi()
    {
    	int x=0,w=1;char ch=getchar();
    	while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    	if (ch=='-') w=0,ch=getchar();
    	while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    	return w?x:-x;
    }
    const int N = 1e5+5;
    int n,m,block,col[N],o[N],len,to[N<<1],nxt[N<<1],head[N],cnt;
    int fa[N],dep[N],sz[N],son[N],top[N],dfn[N];
    int Stack[N],tp,bl[N],ccnt,vis[N],tong[N],ans[N],Ans;
    struct query{
    	int u,v,id;
    	bool operator < (const query &b) const
    		{return bl[u]==bl[b.u]?bl[v]<bl[b.v]:bl[u]<bl[b.u];}
    }q[N];
    void link(int u,int v){to[++cnt]=v;nxt[cnt]=head[u];head[u]=cnt;}
    void dfs1(int u,int f)
    {
    	fa[u]=f;dep[u]=dep[f]+1;sz[u]=1;
    	for (int e=head[u];e;e=nxt[e])
    		if (to[e]!=f)
    		{
    			dfs1(to[e],u);
    			sz[u]+=sz[to[e]];
    			if (sz[to[e]]>sz[son[u]]) son[u]=to[e];
    		}
    }
    void dfs2(int u,int up)
    {
    	top[u]=up;dfn[u]=++cnt;int ttp=tp;
    	if (son[u]) dfs2(son[u],up);
    	if (tp-ttp>=block) {++ccnt;while (tp>ttp) bl[Stack[tp--]]=ccnt;}
    	for (int e=head[u];e;e=nxt[e])
    		if (to[e]!=fa[u]&&to[e]!=son[u])
    		{
    			dfs2(to[e],to[e]);
    			if (tp-ttp>=block) {++ccnt;while (tp>ttp) bl[Stack[tp--]]=ccnt;}
    		}
    	Stack[++tp]=u;
    }
    int getlca(int u,int v)
    {
    	while (top[u]!=top[v])
    	{
    		if (dep[top[u]]<dep[top[v]]) swap(u,v);
    		u=fa[top[u]];
    	}
    	return dep[u]<dep[v]?u:v;
    }
    void update(int x)
    {
    	if (!vis[x])
    	{
    		vis[x]=1;++tong[col[x]];
    		if (tong[col[x]]==1) ++Ans;
    	}
    	else
    	{
    		vis[x]=0;--tong[col[x]];
    		if (tong[col[x]]==0) --Ans;
    	}
    }
    void change(int u,int v)
    {
    	while (u!=v)
    		if (dep[u]>dep[v]) update(u),u=fa[u];
    		else update(v),v=fa[v];
    }
    int main()
    {
    	n=gi();m=gi();block=pow(n,0.5);
    	for (int i=1;i<=n;++i) o[i]=col[i]=gi();
    	sort(o+1,o+n+1);len=unique(o+1,o+n+1)-o-1;
    	for (int i=1;i<=n;++i) col[i]=lower_bound(o+1,o+n+1,col[i])-o;
    	for (int i=1;i<n;++i)
    	{
    		int u=gi(),v=gi();
    		link(u,v);link(v,u);
    	}
    	dfs1(1,0);cnt=0;dfs2(1,1);
    	while (tp) bl[Stack[tp--]]=ccnt;
    	for (int i=1;i<=m;++i)
    	{
    		q[i]=(query){gi(),gi(),i};
    		if (bl[q[i].u]>bl[q[i].v]) swap(q[i].u,q[i].v);
    	}
    	sort(q+1,q+m+1);
    	change(q[1].u,q[1].v);
    	int gg=getlca(q[1].u,q[1].v);
    	update(gg);ans[q[1].id]=Ans;update(gg);
    	for (int i=2;i<=m;++i)
    	{
    		change(q[i].u,q[i-1].u);change(q[i].v,q[i-1].v);
    		gg=getlca(q[i].u,q[i].v);
    		update(gg);ans[q[i].id]=Ans;update(gg);
    	}
    	for (int i=1;i<=m;++i) printf("%d
    ",ans[i]);
    	return 0;
    }
    
  • 相关阅读:
    Activiti系列——如何在eclipse中安装 Activiti Designer插件
    C语言 二维数组与指针笔记
    Ubuntu linux设置从当前目录下加载动态库so文件
    Ubuntu14.04安装nfs服务器
    Ubuntu14.04 搭建FTP服务器
    Linux备忘命令
    Java实现对xml文件的增删改查
    Java利用jacob实现打印Excel文件
    git操作列表
    swiper 窗口宽度变化,页面宽度高度变化 导致自动滑动 解决方案
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/8711046.html
Copyright © 2020-2023  润新知