HDU 4707 最后还是用了BFS。。。

题目大意就是给你一个连通图,保证没有环,让你计算所有点到点0的距离,如果距离大于D,sum++;最后输出sum的值。

一开始被吓到了,还以为是搜索树。。后来洗了把脸后发现题目本质还是把所有的点遍历一遍(就是BFS    ...Orz)

没脑子的就直接写去了。。

结果第一发内存超了。。难掩我的震惊之情。。Orz

题目要求Memory Limit:32768KB,我好像用了37560K ,2333333333

//果然单纯的队列是没有好结果的。。亏我还打了vector..
// 坏毛病要改,一直不会让queue和vector在一起 =V=   
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<queue>

using namespace std;

struct str {
	int next;
	int count;
};

queue<str> q[100005];
queue<str> Q;

int main()
{
	int tot;
	while(~scanf("%d",&tot))
	{
		int n, d;
		int from;
		int sum ;
	   struct	str s;
		while(tot--)
		{
			sum = 0;
			s.count = 0;
			scanf("%d%d",&n,&d);
			n--;
			while(n--)
			{
				scanf("%d%d",&from,&s.next);
				q[from].push(s);
			}
			
			s.next=0;
			Q.push(s);
		    struct	str node; 
			while(!Q.empty())
			{
				s = Q.front();
				Q.pop();
				if(q[s.next].empty()==1)
				{
					if(s.count > d)
					{
						sum+=s.count-d;
					}
				}
				while(!q[s.next].empty())
				{
					node = q[s.next].front();
					q[s.next].pop();
					node.count=s.count+1;
					Q.push(node);
				}
			}
			
			printf("%d\n",sum);
			
			
		}
	}
	return 0;
}

后来还是死性不改的只用了vector,结果过了233  下面AC代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<queue>

using namespace std;

vector<int> q[100005];//存边没什么好说的 
vector<int> Q[100005];//Q[i]表示Q[i]中的所有的点到点0的距离为i 

int main()
{
	int tot;
	while(~scanf("%d",&tot))
	{
		int n, d;
		int from,next;
		int sum ;
		while(tot--)
		{
			
			for(int i= 0;i<100005; i++)
			{
				q[i].clear();
				Q[i].clear();
			}
			
			sum = 0;
			scanf("%d%d",&n,&d);
			n--;
			while(n--)
			{
				scanf("%d%d",&from,&next);
				q[from].push_back(next);
			}
			
			next=0;
			Q[0].push_back(next);
		    int cnt = 0;
		    int count = 1;
		    for(int i = 0;i < Q[cnt].size(); i++)
		    {
    			for(int j = 0;j < q[Q[cnt][i]].size(); j++)
    			{
    				count++;
			    	Q[cnt+1].push_back(q[Q[cnt][i]][j]);
			    }
			    if(count == n)break;
			    if(i ==Q[cnt].size()-1)
			    {
    				cnt++;
    				i=-1;
    			}
    		}
    		
    		for(int i = d+1;i < 100005; i++)
    		{
		    	sum+=Q[i].size();
		    }
	
			printf("%d\n",sum);
			
			
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值