#include <iostream>
#include <queue>
#include <assert.h>
/*
调用的时候要有头文件: #include<stdlib.h> 或 #include<cstdlib> +
#include<queue> #include<queue>
详细用法:
定义一个queue的变量 queue<Type> M
查看是否为空范例 M.empty() 是的话返回1,不是返回0;
从已有元素后面增加元素 M.push()
输出现有元素的个数 M.size()
显示第一个元素 M.front()
显示最后一个元素 M.back()
清除第一个元素 M.pop()
*/
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
queue <int> myQ;
cout<< "现在 queue 是否 empty? "<< myQ.empty() << endl;
for(int i =0; i<10 ; i++)
{
myQ.push(i);
}
for(int i=0; i<myQ.size(); i++)
{
printf("myQ.size():%d\n",myQ.size());
cout << myQ.front()<<endl;
myQ.pop();
}
system("PAUSE");
return 0;
}
输出结果:
现在 queue 是否 empty? 1
myQ.size():10
0
myQ.size():9
1
myQ.size():8
2
myQ.size():7
3
myQ.size():6
4
请按任意键继续. . .
下面给出一个例子:ZOJ 3516
Now we have a tree and some queries to deal with. Every node in the tree has a value on it. For one node A, we want to know the largest three values in all the nodes of the subtree whose root is node A. Node 0 is root of the tree, except it, all other nodes have a parent node.
Input
There are several test cases. Each test case begins with a line contains one integer n(1 ≤ n ≤ 10000), which indicates the number of the node in the tree. The second line contains one integer v[0], the value on the root. Then for the following n - 1 lines(from the 3rd line to the (n + 1)th line), let i + 2 be the line number, then line i + 2 contains two integers parent and v[i], here parent is node i's parent node, v[i] is the value on node i. Here 0 ≤ v[i] ≤ 1000000. Then the next line contains an integer m(1 ≤ m ≤ 10000), which indicates the number of queries. Following m lines, each line contains one integer q, 0 ≤ q < n, it meas a query on node q.
Output
For each test case, output m lines, each line contains one or three integers. If the query asked for a node that has less than three nodes in the subtree, output a "-1"; otherwise, output the largest three values in the subtree, from larger to smaller.
Sample Input
510 100 52 72 8501234
Sample Output
10 8 7-18 7 5-1-1
解题思路:这个题目刚开始的时候没想到要用队列来做。。一直在想树。。到后来才想到。。
这个题是给你一颗树,然后问你这些节点上是否有大于3个子孙,如果有就输出最大的3个,
如果没有就输出-1;
下面是代码:
#include#include #include #include #include using namespace std; int cmp(int a,int b) { if(a>b) return 1; else return 0; } vector que[10005]; int n,a[10005],s[10005],x,y,j; void dfs(int node) { int i; s[j++]=a[node]; for(i=0;i