当前位置:编程学习 > C/C++ >>

CF 315A(Sereja and Bottles-开易拉罐)

A. Sereja and Bottles
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Input
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.

Output
In a single line print a single integer — the answer to the problem.

Sample test(s)
input
4
1 1
2 2
3 3
4 4
output
4
input
4
1 2
2 3
3 4
4 1
output
0


水题,直接数。注意b[i]对a[j]。

考场上有一大堆人这题各种卖萌,专业Hack30年……


[cpp]
#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<algorithm>  
#include<functional>  
#include<iostream>  
#include<cmath>  
#include<cctype>  
using namespace std; 
#define For(i,n) for(int i=1;i<=n;i++)  
#define Fork(i,k,n) for(int i=k;i<=n;i++)  
#define Rep(i,n) for(int i=0;i<n;i++)  
#define ForD(i,n) for(int i=n;i;i--)  
#define RepD(i,n) for(int i=n;i>=0;i--)  
#define Forp(x) for(int p=pre[x];p;p=next[p])  
#define MAXN (100+10)  
#define MAXM (1000+10)  
int a[MAXN],b[MAXN],n; 
bool b2[MAXN]={0}; 
int main() 

//  freopen(".in","r",stdin);  
//  freopen(".out","w",stdout);  
    memset(b2,0,sizeof(b2)); 
    cin>>n; 
    For(i,n) cin>>a[i]>>b[i]; 
    For(i,n) 
        For(j,n) 
        { 
            if (i!=j&&b[i]==a[j]) b2[j]=1; 
        } 
    int ans=0; 
    For(i,n) ans+=b2[i]; 
    cout<<n-ans<<endl; 
    return 0; 

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define MAXN (100+10)
#define MAXM (1000+10)
int a[MAXN],b[MAXN],n;
bool b2[MAXN]={0};
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
    memset(b2,0,sizeof(b2));
    cin>>n;
    For(i,n) cin>>a[i]>>b[i];
    For(i,n)
        For(j,n)
        {
            if (i!=j&&b[i]==a[j]) b2[j]=1;
        }
    int ans=0;
    For(i,n) ans+=b2[i];
    cout<<n-ans<<endl;
 return 0;
}

 

 

 

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,