关于栈溢出
  • 板块学术版
  • 楼主Fat_Fish
  • 当前回复3
  • 已保存回复3
  • 发布时间2022/8/12 18:09
  • 上次更新2023/10/27 15:44:50
查看原帖
关于栈溢出
238885
Fat_Fish楼主2022/8/12 18:09
#include <bits/stdc++.h>
using namespace std;
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * f;
}
const int N = 1e5 + 100;
const int M = 1e9 + 7;
vector<int> g[N];
int n, ans, siz[N];
void dfs(int x, int from) {
	siz[x] = 1;
	for (auto y : g[x])
		if (y != from) {
			dfs(y, x);
			siz[x] += siz[y];
		}
	for (auto y : g[x])
		if (y != from) 
			ans = (ans + 1ll * siz[y] * (siz[x] - 1 - siz[y])) % M;
	ans = (ans + 1ll * (siz[x] - 1) * (n - siz[x]) * 2 ) % M;
	printf("%d\n", siz[x]);
	return;
}
signed main() {
	n = read();
	for (int i = 1; i < n; ++i) {
		int u = read(), v = read();
		g[u].push_back(v), g[v].push_back(u);
	}
	dfs(1, 0);
	cout << ans << '\n';
	return 0;
}

这段代码 10510^5 一条链的情况疑似栈溢出了

加上 -Wl,--stack=123456789可以跑出来

但是如果只在代码开头加 #pragma comment(linker, "/STACK:123456789,123456789")

就还是不行

2022/8/12 18:09
加载中...