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-sum);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/riskyer/p/3263041.html