You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
850 B
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 1050;
bool vis[N];
int nxt[N], n;
void dfs(int x)
{
for (int i = 1; i <= n; i++)
if (nxt[i] != i && !vis[nxt[i]])
{
vis[nxt[i]] = true;
dfs(nxt[i]);
}
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int a, b;
scanf("%d%d", &a, &b);
nxt[a] = b;
}
int ans = n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (nxt[i] == j && !vis[j] && j != i)
vis[j] = true, ans--;
//for (int i = 1; i <= n; i++) dfs(i);
//printf("%d", accumulate(vis + 1, vis + n + 1, 0, [](auto a, auto b) { return a + !b; }));
printf("%d", ans);
return 0;
}