标签:des style blog http color os io ar strong
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18598 | Accepted: 9860 |
Description
Input
Output
Sample Input
2 16 1 14 8 5 10 16 5 9 4 6 8 4 4 10 1 13 6 15 10 11 6 7 10 2 16 3 8 1 16 12 16 7 5 2 3 3 4 3 1 1 5 3 5
Sample Output
4 3
Source
1 #include<iostream> 2 #include<vector> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 const int maxnum = 10000; 7 8 vector<int> Son[maxnum]; //存储每个节点的孩子 9 int level[maxnum]; // 存储每个节点的层次 10 int parent[maxnum]; //存储每个节点的父亲节点 11 12 13 void DFS(int number,int deep) 14 { 15 level[number]=deep; 16 for(vector<int>::iterator it = Son[number].begin();it!=Son[number].end();it++) 17 DFS(*it,deep+1); 18 } 19 20 int main() 21 { 22 int t; 23 while(scanf("%d",&t)!=EOF) 24 { 25 while(t--) 26 { 27 int num; 28 scanf("%d",&num); 29 int dad,son; 30 for(int i=0;i<maxnum;i++) 31 Son[i].clear(); 32 memset(parent,-1,sizeof(parent)); 33 for(int i=0;i<num-1;i++) 34 { 35 scanf("%d%d",&dad,&son); 36 Son[dad-1].push_back(son-1); 37 parent[son-1] = dad-1; //存储每个节点的上一个节点 38 } 39 int t=dad-1; 40 while(parent[t]!=-1) 41 t=parent[t]; 42 DFS(t,0); //遍历分层 43 44 int x,y; 45 scanf("%d%d",&x,&y); 46 x=x-1; 47 y=y-1; 48 while(x!=y) 49 { 50 if(level[x]<level[y]) 51 y=parent[y]; 52 else 53 x = parent[x]; 54 } 55 printf("%d\n",x+1); 56 57 58 } 59 } 60 return 0; 61 }
标签:des style blog http color os io ar strong
原文地址:http://www.cnblogs.com/jhldreams/p/3956665.html