1 solutions
-
0
C :
#include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; }Link; void insertNode(Link *head, int data) { Link *p = head; Link *q = (Link *)malloc(sizeof(Link)); q->data = data; q->next = NULL; while(p->next != NULL) p=p->next; p->next = q; } void freeNode(Link *head) { Link *p = head->next; Link *q; head->next = NULL; while(p != NULL){ q = p; p=p->next; free(q); } } int deep(Link ** tree, int start) { int depth = 1; Link *p; if(tree[start]->next == NULL) { return depth; } p= tree[start]->next; while(p!= NULL){ int tmp = deep(tree, p->data - 1); if(tmp > depth) depth = tmp; p=p->next; } return depth + 1; } int main(){ int count, i,j; int a, b; Link **tree; scanf("%d", &count);j=count-1; tree = (Link **)malloc(sizeof(Link*)*count); for(i=0;i<count;++i) { tree[i] = (Link *)malloc(sizeof(Link)); tree[i]->next = NULL; } while(j>0){ scanf("%d%d",&a, &b); if(a>0 && b>0) { insertNode(tree[a-1], b); j--; } } printf("%d\n", deep(tree, 0)); for(i=0;i<count;++i) { freeNode(tree[i]); free(tree[i]); } free(tree); return 0; }
C++ :
#include <iostream> using namespace std; int main() { int a[10000],i,n,z,x,m; cin>>n; a[1]=1; while (cin>>z>>x) a[x]=a[z]+1; m=1; for (i=1;i<=n;++i) if (a[i]>m) m=a[i]; cout<<m; return 0; }
Pascal :
program p2450; var a:array[1..100,1..100] of longint; n,i,j,k,x,y,max:longint; begin readln(n); for i:=1 to n do for j:=1 to n do a[i,j]:=maxint; for i:=1 to n-1 do begin readln(x,y); a[x,y]:=1; a[y,x]:=1; end; for k:=1 to n do for i:=1 to n do for j:=1 to n do if (i<>k) and (j<>k) and (i<>j) then if a[i,k]+a[k,j]<a[i,j] then a[i,j]:=a[i,k]+a[k,j]; max:=0; for i:=2 to n do if a[1,i]>max then max:=a[1,i]; writeln(max+1); end.
Java :
import java.util.Scanner; import java.util.ArrayList; /** * * @author zhenghan33 */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner cin=new Scanner(System.in); int number=cin.nextInt(); ArrayList<Node> a=new ArrayList<>(); a.add(new Node(1,1)); while(cin.hasNext()) { int m=cin.nextInt(); int n=cin.nextInt(); int height=1; for(int i=0;i<a.size();++i) if(a.get(i).getId()==m) { height=a.get(i).getHeight()+1; break; } a.add(new Node(n,height)); } int max=1; for(int i=0;i<a.size();++i) { if(a.get(i).getHeight()>max) max=a.get(i).getHeight(); } System.out.println(max); } } class Node{ private int id; private int height; Node(int a,int b) { id=a; height=b; } public int getId(){return id;} public int getHeight(){return height;} public void setId(int n){id=n;} public void setHeight(int n){height=n;} }
- 1
Information
- ID
- 557
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By