核弹剑仙
核弹剑仙
https://ac.nowcoder.com/acm/contest/6874/F
题意转化
我们把它想象成一张图,给你的是a>b,我们就从b向a连一条边,最后的时候每次以i结点遍历整张图。看看能遍历到几个点就行了,最后的时候答案要-1.因为第i个点也算了一遍。
code
#include <bits/stdc++.h> #define N 100010 #define M 2010 using namespace std; int n, m, head[M], add_edge, vis[M], ans; struct node { int next, to; }edge[M]; int read() { int s = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= (ch == '-'), ch = getchar(); while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar(); return f ? -s : s; } void add(int from, int to) { edge[++add_edge].next = head[from]; edge[add_edge].to = to; head[from] = add_edge; } void dfs(int x) { ans++; vis[x] = 1; for (int i = head[x]; i; i = edge[i].next) { int to = edge[i].to; if (vis[to]) continue; dfs(to); } } int main() { n = read(), m = read(); for (int i = 1, x, y; i <= m; i++) { x = read(), y = read(); add(y, x); } for (int i = 1; i <= n; i++) { memset(vis, 0, sizeof vis), ans = 0; dfs(i); cout << ans - 1<< "\n"; } return 0; } ```