zoukankan      html  css  js  c++  java
  • Pet

    版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/liujie619406439/article/details/37725227

    点击打开链接

    bfs,须要自己构图

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    #include<vector>
    
    using namespace std;
    
    const int maxn = 100005;
    int dep[ maxn ];
    int vis[ maxn ];
    int pre[ maxn ];
    vector< int >mapp[ maxn ];
    
    void Bfs( int s ){
    	memset( dep, -1, sizeof( dep ) );
    	dep[ 0 ] = 0;
    	queue< int > q;
    	q.push( s );
    	while( !q.empty() ){
    		int u = q.front();
    		q.pop();
    		int sz = mapp[ u ].size();
    		for( int i = 0; i < sz; ++i ){
    			int v = mapp[ u ][ i ];
    			if( dep[ v ] != -1 )
    				continue;
    			
    			dep[ v ] = dep[ u ] + 1
    			q.push( v );
    		}
    	}
    }
    
    int main(){
    	int Case, n, d, u, v;
    	scanf( "%d", &Case );
    	while( Case-- ){
    		scanf( "%d%d", &n, &d );
    		for( int i = 0; i < n; ++i )
    			mapp[ i ].clear();
    		for( int i = 1; i < n; ++i ){
    			scanf( "%d%d", &u, &v );
    			mapp[ u ].push_back( v );
    			mapp[ v ].push_back( u );
    		}
    		Bfs( 0 );
    		int ans = 0;
    		for( int i = 0; i < n; ++i ){
    			if( dep[ i ] > d ){
    				ans++;
    			}
    		}
    		printf( "%d
    ", ans );
    	}
    	return 0;
    }


查看全文
  • 相关阅读:
    Thrift实现C#调用Java开发步骤详解
    微信小程序项目实战之豆瓣天气
    带有关闭按钮的alertView
    基于olami开放语义平台的微信小程序遥知之源码实现
    iOS-仿智联字符图片验证码
    微信 支付宝支付 友盟登录分享 统计
    优化VMware提高虚拟机运行速度的技巧
    区块链与密码学
    在 Ubuntu 16.04 中安装支持 CPU 和 GPU 的 Google TensorFlow 神经网络软件
    Ubuntu+anaconda环境里安装opencv
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10874527.html
  • Copyright © 2011-2022 走看看