20pts求条
  • 板块学术版
  • 楼主__Function__
  • 当前回复2
  • 已保存回复2
  • 发布时间2025/1/18 20:43
  • 上次更新2025/1/19 08:12:31
查看原帖
20pts求条
1324175
__Function__楼主2025/1/18 20:43

题目链接

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 100;
int n, m, ans, f[N];
bool vis[N];
vector<int> g[N];
int d(int x) {
	if(f[x]) return f[x];
	if(g[x].empty()) return 1;
	for (int i = 0; i < g[x].size(); i ++) {
		f[x] += d(g[x][i]);
	}
	return f[x];
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= m; i ++) {
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		vis[v] = 1;
	}
	for (int i = 1; i <= n; i ++) {
		if(vis[i] == 0) {
			ans += d(i);
		}
	}
	cout << ans;
	return 0;
}
2025/1/18 20:43
加载中...