【跪求调试】代码陷入死循环
查看原帖
【跪求调试】代码陷入死循环
206814
封禁用户楼主2022/7/22 08:26

这个程序对于样例(甚至是所有数据点)都会陷入无限递归的状态。

对于样例:

154351345...1\rightarrow 5 \rightarrow 4 \rightarrow 3 \rightarrow 5 \rightarrow 1 \rightarrow 3 \rightarrow 4 \rightarrow 5 \rightarrow ...

按理来说要是数据是个树就不存在环了吧,我看题解都没有带bookbook一类标记数组

代码:

#include <bits/stdc++.h>
#define int long long
#ifdef LOCAL
	#define I64 "%I64d"
	#define U64 "%I64u"
#else
	#define I64 "%lld"
	#define U64 "%llu"
#endif
using namespace std;
vector<vector<int> > tree;
int depth[50005], father[50005][20], lg[50005];
int cnt[50005], ans = 0;
void dfs(int now, int fa){
	father[now][0] = fa;
	depth[now] = depth[fa] + 1;
	for(int i = 1; i <= lg[depth[now]]; i++){
		father[now][i] = father[father[now][i - 1]][i - 1];
	}
	for(vector<int>::iterator it = tree[now].begin(); it != tree[now].end(); it++){
		if(*it != fa){
			dfs(*it, now);
		}
	}
}
int LCA(int x, int y){
	if(depth[x] < depth[y]) swap(x, y);
	while(depth[x] > depth[y]) x = father[x][lg[depth[x] - depth[y]] - 1];
	if(x == y) return y;
	for(int k = lg[depth[x]] - 1; k >= 0; k--){
		if(father[x][k] != father[y][k]){
			x = father[x][k], y = father[y][k];
		}
	}
	return father[x][0];
}
void dfs2(int now, int fa){
	for(vector<int>::iterator it = tree[now].begin(); it != tree[now].end(); it++){
		if(*it == fa) continue;
		dfs2(*it, now);
		cnt[now] += cnt[*it];
	}
	ans = max(ans, cnt[now]);
}
signed main(){
	int n, k;
	scanf(I64 I64, &n, &k);
	tree.resize(n + 1);
	for(int i = 1, s, t; i <= k; i++){
		scanf(I64 I64, &s, &t);
		tree[s].push_back(t);
		tree[t].push_back(s);
	}
	for(int i = 1; i <= n; i++){
		lg[i] = lg[i - 1] + (1 << lg[i - 1] == i);
	}
	dfs(1, 0);
	for(int i = 0; i < k; i++){
		int u, v;
        scanf(I64 I64, &u, &v);
        int o = LCA(u, v);
		cnt[u]++, cnt[v]++, cnt[o]--, cnt[father[o][0]]--;
 }
	dfs2(1, 0);
	printf(I64"\n", ans);
	return 0;
}

求求大佬了qq_emoji: kk

2022/7/22 08:26
加载中...