程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> CF 315A(Sereja and Bottles-開易拉罐)

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

編輯:C++入門知識

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;
}

 

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved