#43486: 各位大神,可以


s11131045@nhsh.tp.edu.tw (10309彭鈺翔)

學校 : 不指定學校
編號 : 215218
來源 : [114.34.219.25]
最後登入時間 :
2024-10-21 20:40:03
c463. apcs 樹狀圖分析 (Tree Analyses) -- apcs | From: [114.34.219.25] | 發表日期 : 2024-10-19 15:27

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <stack>
#define Max 100001
using std::cin;
using std::cout;
using std::endl;
using std::stack;
using std::vector;
int height[Max];
vector<int> edge[Max];
stack<int> leaf;
/*
int re_dfs(int node){
if (edge[node].size()==0) return 0;
int fnode = edge[node][0];
if(height[node]+1<height[fnode]) return 0; 
height[fnode] = height[node]+1;
re_dfs(fnode);
 
 
}
*/
 
int main(){
int N,n=0;
while (cin>>N){
//Initialization
for(int i=0;i<N;i++){
edge[i].clear();
height[i] = 0;
}
//Reciving Tesing context
for(int i = 1;i<N+1;i++){
cin>>n;
for(int j = 0,temp;j<n;j++){
cin>>temp,edge[temp].push_back(i);
}
if (n==0) leaf.push(i);
}
//Start with leaf
while(!leaf.empty()){
int node = leaf.top();
leaf.pop();
while(edge[node].size() != 0){
int fnode = edge[node][0];
if(height[node]+1 >height[fnode]) height[fnode] = height[node]+1;
node = fnode;
}
 
 
}
int index = 0,sum = 0,max_height=height[0];
for(int i = 1;i<N;i++){
sum+=height[i];
if(height[i]>max_height) index=i,max_height = height[i];
}
cout << index << endl << sum<<endl;
}
}
 
ZeroJudge Forum