Sereja and his friends went to a picnic. The guys had n soda bottles just for it. Sereja forgot the bottle opener as usual, so the guys had to come up with another way to open bottles.
Sereja knows that the i-th bottle is from brand ai, besides, you can use it to open other bottles of brand bi. You can use one bottle to open multiple other bottles. Sereja can open bottle with opened bottle or closed bottle.
Knowing this, Sereja wants to find out the number of bottles they've got that they won't be able to open in any way. Help him and find this number.
The first line contains integer n (1 ≤ n ≤ 100) — the number of bottles. The next n lines contain the bottles' description. The i-th line contains two integers ai, bi (1 ≤ ai, bi ≤ 1000) — the description of the i-th bottle.
In a single line print a single integer — the answer to the problem.
4 1 1 2 2 3 3 4 4
4
4 1 2 2 3 3 4 4 1
0
解题说明:题目的意思虽然是开瓶盖,其实只需要保证每一行中这一对数不同即可,因为第一个瓶子无论是否打开都可以打开后一个瓶子。于是问题转变为判断一组数中,每一行中的一对数不同的个数,再用总数减去即为答案。
#include <iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> using namespace std; int main() { int an=0,n,y,i,j,k,x,a[1001],b[1001]; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d%d",&a[i],&b[i]); } for(i=0;i<n;i++) { for(j=0;j<n;j++) { if((a[i]==b[j])&&(i!=j)) { an++; break; } } } printf("%d\n",n-an); return 0; }