codeforces A. Sereja and Bottles 解题报告
题目链接: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\n", j, a[j]); 29 vis[j] = 1; 30 } 31 } 32 33 } 34 printf("%d\n", n - cnt); 35 } 36 return 0; 37 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步