题目链接:http://codeforces.com/problemset/problem/315/A
题目意思:有n个soda bottles,随后给出这n个soda bottles的信息。已知第 i 个bottle来自品牌ai,你可以用这个品牌 ai 来开所有属于品牌bi 的bottles。注意,other特别用黑色粗体来强调,表明该行的除外,也就是说,假如i = 1(隐含的),ai = 1,bi = 1,这个bottle 1 是不能被打开的。需要找出无论用什么方式都不能打开的bottle的总个数。
另外,有可能同一个bottle可以被多个不同的ai 打开,所以要增加一个额外的vis数组来防止已经打开的bottle不再重新被处理。
这条题目很久才读懂它的意思,真是要加强读题能力啊!!!
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 100 + 10; 8 int a[maxn], b[maxn], vis[maxn]; 9 10 int main() 11 { 12 int i, j, n, cnt; 13 while (scanf("%d", &n) != EOF) 14 { 15 for (i = 1; i <= n; i++) 16 { 17 scanf("%d%d", &a[i], &b[i]); 18 } 19 memset(vis, 0, sizeof(vis)); 20 cnt = 0; 21 for (i = 1; i <= n; i++) // 从bottle 1开始搜索哪些a[j]可以打开它的 22 { 23 for (j = 1; j <= n; j++) 24 { 25 if (i != j && a[j] == b[i] && !vis[j]) 26 { 27 cnt++; // 记录能打开的bottle 数
28 // printf("a[%d] = %d ", j, a[j]); 29 vis[j] = 1; 30 } 31 } 32 33 } 34 printf("%d ", n - cnt); 35 } 36 return 0; 37 }