E. DFS Trees

E. DFS Trees
树上差分

/*input
10 11
1 2
2 5
3 4
4 2
8 1
4 5
10 5
9 5
8 2
5 7
4 6
 
*/
 
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef pair<int,int> pii;
int fa[N],ds[N];
int len[N],xl[N],num[N];
bool in[N];
vector<int> eage1[N],eage0[N];
 
int get(int x){
    if(x!=fa[x]) return fa[x]=get(fa[x]);
    return x;
}
void link(int x,int y){
    int i=get(x);
    int j=get(y);
    if(ds[i]>ds[j]) swap(i,j);
    fa[i]=j;
    ds[j]+=ds[i];
}
void dfs1(int root,int fa){
    in[root]=1;
    len[root]=len[fa]+1;
    xl[len[root]]=root;
    for(auto i:eage0[root]){
        int node=i;
        if(!in[node]){
            if(!len[node])
                num[root]++,num[node]++;
        }  
        else{
            num[1]++,num[node]--,
            num[xl[len[node]+1]]--;
        }
    }
    for(auto i:eage1[root]){
        int node=i;
        if(node!=fa)
            dfs1(node,root);
    }
    in[root]=0;
}
void dfs2(int root,int fa){
    num[root]+=num[fa];
    for(auto i:eage1[root]){
        int node=i;
        if(node!=fa)
            dfs2(node,root);
    }
}
void solve(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        ds[i]=1;
        fa[i]=i;
        num[i]=0;
    }
    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        if(get(x)!=get(y)){
            link(x,y);
            eage1[x].push_back(y);
            eage1[y].push_back(x);
        }
        else{
            eage0[x].push_back(y);
            eage0[y].push_back(x);
        }
    }
    dfs1(1,0);
    dfs2(1,0);
    for(int i=1;i<=n;i++){
        if(num[i]==m-n+1) cout<<1;
        else cout<<0;
    }
    cout<<endl;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    t=1;
    while(t--) solve();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是由于递归调用dfs()方法时,栈空间不足而导致的。我们可以通过增加栈空间的方式来解决这个问题。 修改方法如下: 在执行程序时,增加栈空间的大小,例如: ``` java -Xss4m RiverCrossingProblem ``` 这将增加栈空间的大小为4MB。 另外,注意在dfs()方法中,如果当前状态已经存在于已访问的状态中,则不再递归调用dfs()方法,避免重复搜索。 修改后的代码如下: ``` import java.util.*; public class RiverCrossingProblem { static final int WOLF = 1; static final int SHEEP = 2; static final int VEGETABLES = 3; static final int EMPTY = 0; static final int LEFT = 0; static final int RIGHT = 1; static final int[][] STATE = { { 0, 0, 0 }, { WOLF, SHEEP, VEGETABLES } }; static boolean isValid(int[] state) { if (state[WOLF] == state[SHEEP] && state[WOLF] != state[EMPTY]) return false; if (state[SHEEP] == state[VEGETABLES] && state[SHEEP] != state[EMPTY]) return false; return true; } static boolean isFinalState(int[] state) { return Arrays.equals(state, STATE[1]); } static void dfs(int[] state, int side, List<String> solution, Set<String> visited) { if (!isValid(state)) return; if (isFinalState(state)) return; String key = Arrays.toString(state) + side; if (visited.contains(key)) return; visited.add(key); int[] newState = new int[state.length]; for (int i = 0; i < state.length; i++) { if (state[i] == side) { newState = Arrays.copyOf(state, state.length); newState[i] = 1 - side; String action = ""; switch (i) { case WOLF: action = "wolf"; break; case SHEEP: action = "sheep"; break; case VEGETABLES: action = "vegetables"; break; } solution.add(action + " -> " + (side == LEFT ? "right" : "left")); dfs(newState, 1 - side, solution, visited); if (isFinalState(newState)) return; solution.remove(solution.size() - 1); } } } public static void main(String[] args) { int[] state = { WOLF, SHEEP, VEGETABLES, 1 }; List<String> solution = new ArrayList<>(); Set<String> visited = new HashSet<>(); dfs(state, 1, solution, visited); for (String step : solution) { System.out.println(step); } } } ``` 在dfs()方法中,增加了一个Set<String>类型的visited,用于存储已经访问过的状态。在递归调用dfs()方法之前,先检查当前状态是否已经存在于visited集合中,如果是,则不再递归调用dfs()方法。 这样做可以避免重复搜索,从而减少栈空间的使用,避免StackOverflowError错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值