这题很明显可以用倍增最近公共祖先求解,但是用java的话很容易导致栈溢出StackOverFlow,关键是怎么去解决递归太深导致内存不够用的问题。那么如何解决虚拟机中栈帧过多的问题呢?我们可以手动定义一个栈,
static Stack<int[]> stack = new Stack<>();
然后就可以将递归的dfs变成这样:
static void dfs(int now,int fath){
stack.push(new int[]{now,fath});
while(!stack.empty()){
int[] pop = stack.pop();
int u=pop[0],father=pop[1];
fa[u][0]=father;
depth[u]=depth[father]+1;
if(cow[u]=='G') dis[u][0]=dis[u][0]|1; else dis[u][0]=dis[u][0]|2;
if(cow[father]=='G') dis[u][0]=dis[u][0]|1; else dis[u][0]=dis[u][0]|2;
for(int i=1;i<=lg[depth[u]];i++){
dis[u][i]=dis[u][i-1]|dis[fa[u][i-1]][i-1];
fa[u][i]=fa[fa[u][i-1]][i-1];
}
for(int i=he[u];i>0;i=to[i])
if(vv[i]!=father) stack.push(new int[]{vv[i],u});
}
}
稍微懂一点虚拟机的小伙伴应该很容易知道原因,用递归去初始化图的话,最大可能同时存在100000左右的栈帧(题目中n最大值为100000),这将导致StackOverFloe,除非你改了你改了idea栈容量。但我们是无法更改出题方的电脑配置的,那么递归的最近公共祖先是行不通的。但我们自己定义Stack后,把局部变量都存入Stack,可以把100000左右的栈帧减小到10左右,而堆和方法区的容量是完全够我们用的(堆默认最大4G左右,虚拟机栈默认1m,windows下,-XX:MetaspaceSize是21M,-XX:MaxMetaspaceSize 的值是-1,即没有限制,也就是说方法区,也叫静态区的容量也够用,反正比栈的1m大很多),而Stack实在方法区存储的,这样就避免了StackOverFlow。
下面上源码:
(注:标注一条线路上可以碰到哪种牛,我采用了较为复杂的方法,当时是随想随写的代码,更为简单的方法是定义两个数组,分别代表哪种牛可以碰到)
import java.io.*;
import java.util.*;
public class Main {
static int maxn=100001;
static int n,m,cnt;
static int[] cow=new int[maxn]; //0更赛牛G 第一位 1 H 二进制第二位
static int[] vv=new int[maxn<<1],to=new int[maxn<<1];
static int[] he=new int[maxn],lg=new int[maxn+1];
static int[] depth=new int[maxn];
static int[][] fa = new int[maxn][20],dis=new int[maxn][20];
public static void main(String[] args) throws IOException {
n=nextInt();m=nextInt();
String line = nextString();
for(int i=1;i<=n;i++) lg[i]=lg[i-1]+(1<<lg[i-1]==i?1:0);
for(int i=0;i<line.length();i++)
cow[i+1]=line.charAt(i);
for(int i=1;i<n;i++){
int u=nextInt(),v=nextInt();
addEdge(u,v);
addEdge(v,u);
}
dfs(1,0);
int x,y,z;
for(int i=1;i<=m;i++){
x=nextInt();y=nextInt();z=nextString().charAt(0);
int ans = lca(x, y);
if(z=='G') ans=ans&1;else ans=ans&2;
if(ans>0) out.print(1);
else out.print(0);
}
out.flush();
}
static Stack<int[]> stack = new Stack<>();
static void dfs(int now,int fath){
stack.push(new int[]{now,fath});
while(!stack.empty()){
int[] pop = stack.pop();
int u=pop[0],father=pop[1];
fa[u][0]=father;
depth[u]=depth[father]+1;
if(cow[u]=='G') dis[u][0]=dis[u][0]|1; else dis[u][0]=dis[u][0]|2;
if(cow[father]=='G') dis[u][0]=dis[u][0]|1; else dis[u][0]=dis[u][0]|2;
for(int i=1;i<=lg[depth[u]];i++){
dis[u][i]=dis[u][i-1]|dis[fa[u][i-1]][i-1];
fa[u][i]=fa[fa[u][i-1]][i-1];
}
for(int i=he[u];i>0;i=to[i])
if(vv[i]!=father) stack.push(new int[]{vv[i],u});
}
}
static int lca(int x,int y){
int ans=0;
if(depth[x]<depth[y]){
int t=x;x=y;y=t;
}
while(depth[x]>depth[y]){
ans=ans|dis[x][lg[depth[x]-depth[y]]-1];
x=fa[x][lg[depth[x]-depth[y]]-1];
}
if(x==y) {
if(cow[x]=='G') ans|=1;
else ans|=2;
return ans;
}
for(int k=lg[depth[x]]-1;k>=0;k--){
if(fa[x][k]!=fa[y][k]){
ans=ans|dis[x][k];
ans=ans|dis[y][k];
x=fa[x][k];
y=fa[y][k];
}
}
ans=ans|dis[x][0];
ans=ans|dis[y][0];
return ans;
}
static void addEdge(int u,int v){
vv[++cnt]=v;
to[cnt]=he[u];
he[u]=cnt;
}
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static int nextInt() throws IOException{
in.nextToken();
return (int)in.nval;
}
public static String nextString() throws IOException {
in.nextToken();
return in.sval;
}
}