【HDOJ 5326】Work

【HDOJ 5326】Work

暴力搜。。纯暴力。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
#include <list>
#define ll long long
#define INF 0x3f3f3f3f
#define esp 1e-8

using namespace std;

bool mp[111][111],vis[111];
int k,n;

bool Bfs(int p)
{
    int i,u,v,cnt;
    memset(vis,0,sizeof(vis));
    queue <int> q;
    q.push(p);
    vis[p] = 1;
    cnt = 0;
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        for(i = 1; i <= n; ++i)
        {
            if(mp[u][i] && !vis[i])
            {
                cnt++;
                q.push(i);
                vis[i] = 1;
            }
        }
    }
    if(cnt == k) return 1;
    return 0;
}


int main()
{
    int i,u,v,cnt;
    while(~scanf("%d %d",&n,&k))
    {
        cnt = 0;
        memset(mp,0,sizeof(mp));
        for(i = 0; i < n-1; ++i)
        {
            scanf("%d %d",&u,&v);
            mp[u][v] = true;
        }
        for(i = 1; i <= n; ++i)
        {
            if(Bfs(i)) cnt++;
        }
        printf("%d\n",cnt);
    }
    return 0;
}

你可能感兴趣的:(bfs)