树的深度

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;

int main(){
	int n;
	while (cin >> n){
		vector<int> idxs(n);
		for (int i = 0; i<n; i++){
			idxs[i] = 1;
		}
		for (int i = 0; i < n - 1; i++){
			int r, v;
			cin >> r >> v;
			idxs[v] =idxs[r] + 1;
		}
		int val = -1;
		for (int i = 0; i < idxs.size(); i++){
			if (idxs[i] > val)
				val = idxs[i];
		}

		cout << val << endl;
	}

	return 0;
}
这么写有问题么?#小米#
全部评论
import java.util.*; public class Mainn { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n=sc.nextInt(); int[] m=new int[1000001]; for(int i=0;i<m.length;i++)m[i]=i; int len=0; for(int i=0;i<n-1;i++){ int a=sc.nextInt(); int b=sc.nextInt(); if(a>len)len=a; if(b>len)len=b; m[b]=a; } if(n==1){ System.out.println(1); continue; } int max=0; for(int x=0;x<=len;x++){ int tmp=getD(m,x); //System.out.println(x+"的深度为"+tmp); if(tmp>max)max=tmp; } System.out.println(max); } } private static int getD(int[] m,int i) { int d=1; while(m[i]!=i){ d++; i=m[i]; } return d; } } 100% AC
点赞 回复 分享
发布于 2016-09-24 18:57
#include <iostream> using namespace std; int main(void) { int num, check, high = 1; cin>>num; int tree[num][3]; tree[0][0] = 0; tree[0][1] = 0; tree[0][2] = 1; for(int i=1; i<num; i++){ cin>>tree[i][0]>>tree[i][1]; } for(int i=1; i<num; i++){ for(int j=0; j<num; j++){ if(tree[i][0]==tree[j][1]) tree[i][2] = tree[j][2]+1; } if(high<tree[i][2]) high = tree[i][2]; } cout<<high<<endl; return 0; } 惹惹惹...
点赞 回复 分享
发布于 2016-09-24 18:35
和我一样too young too naive
点赞 回复 分享
发布于 2016-09-23 21:40
import java.util.Scanner; /** * Created by gzd on 2016/9/23. */ public class TestXiaoMiR1 { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); Node[] nodes = new Node[n]; int count = 0; // Node node = new Node(0); if(n <= 1){ System.out.println(n); return; } for(int i = 0; i < n - 1; ++i){ int father = in.nextInt(); int son = in.nextInt(); Node sonNode = new Node(son); Node fatherNode=null; for(Node a : nodes){ if(a != null&&a.value == father){ fatherNode = a; break; } if(a == null) break; } if(fatherNode == null){ fatherNode = new Node(father); nodes[count] = fatherNode; count += 1; } if(fatherNode.left == null){ fatherNode.left = sonNode; }else fatherNode.right = sonNode; nodes[count] = sonNode; count += 1; } System.out.println(FindTreeDeep(nodes[0])); } public static int FindTreeDeep(Node head){ int deep=0; if(head != null){ int lchilddeep=FindTreeDeep(head.left); int rchilddeep=FindTreeDeep(head.right); deep=lchilddeep>=rchilddeep?lchilddeep+1:rchilddeep+1; } return deep; } public static class Node{ int value; Node left; Node right; public Node(int value) { this.value = value; } } } 不知道哪错了,求指正。
点赞 回复 分享
发布于 2016-09-23 21:32
import java.util.Scanner; class Main{ public static void main(String[] args) { Scanner in= new Scanner(System.in); while(in.hasNext()){ int n = in.nextInt(); int[] tree = new int[n]; for (int i = 0; i < tree.length; i++) { tree[i]=-1; } for(int i =0;i<n-1;i++){ int p = in.nextInt(); int c = in.nextInt(); tree[c] =p; } int max=1; int count =0; for (int i = n-1; i >0; --i) { int cur=i; while(cur!=-1) { cur = tree[cur]; count++; } if(count>max) max =count; count=0; } System.out.println(max); } } } //O(n)解   你思路是对的 
点赞 回复 分享
发布于 2016-09-23 21:30
和我一样天真
点赞 回复 分享
发布于 2016-09-23 21:28

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务