标签:4707
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\n", ans ); } return 0; }
标签:4707
原文地址:http://blog.csdn.net/bo_jwolf/article/details/37725227