Sereja and Bottles

http://codeforces.com/problemset/problem/315/A

题目意思是第ai的瓶子能开bi的瓶子。给你相应的数据,求无法用其他瓶子打开的数量(即需要外力)。
一开始看错题了,以为是并查集。。。
AC代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
struct node
{
int a,b; //a能开b
} q[108];
bool cmp(node x,node y)
{
return x.a<y.a;
}
int w[108]; //标记第i个瓶子是否被开过
int main()
{
int n,i,j,sum;
while(~scanf("%d",&n) && n)
{
sum=0;
for(i=1; i<=n; i++)
{
scanf("%d%d",&q[i].a,&q[i].b);
}
sort(q+1,q+1+n,cmp);
memset(w,0,sizeof(w));
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(j != i && q[i].b==q[j].a && w[j] == 0)
{
sum++;
w[j]=1;
}
}
}
printf("%d\n",n-sum);
}
return 0;
}
上一篇:小白的白话日记2——RectTransform的理解、应用、难点


下一篇:面试题之java 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 要求不能出现截半的情况