Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 43 Accepted Submission(s) : 20
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,则A,B是C的grandparent,C是A,B的grandchild,如果A,B是C的(外)曾祖父,曾祖母,则A,B是C的great-grandparent,C是A,B的great-grandchild,之后再多一辈,则在关系上加一个great-。
Input
输入包含多组测试用例,每组用例首先包含2个整数n(0<=n<=26)和m(0<m<50), 分别表示有n个亲属关系和m个问题, 然后接下来是n行的形式如ABC的字符串,表示A的父母亲分别是B和C,如果A的父母亲信息不全,则用-代替,例如A-C,再然后是m行形式如FA的字符串,表示询问F和A的关系。
当n和m为0时结束输入。
当n和m为0时结束输入。
Output
如果询问的2个人是直系亲属,请按题目描述输出2者的关系,如果没有直系关系,请输出-。
具体含义和输出格式参见样例.
具体含义和输出格式参见样例.
Sample Input
3 2 ABC CDE EFG FA BE 0 0
Sample Output
great-grandparent -
构图,求最短距离确定两者之间的亲疏关系
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; #define inf 0x7777777 const int N=105; int mp[N][N]; int min(int a,int b) { return a<b?a:b; } void init() { for(int i=0; i<N; i++) for(int j=0; j<N; j++) { if(i==j) mp[i][j]=0; else mp[i][j]=inf; } } void floyd() { for(int k=0; k<=N; k++) for(int i=0; i<=N; i++) for(int j=0; j<=N; j++) { if(mp[i][j]>mp[i][k]+mp[k][j]) mp[i][j]=mp[i][k]+mp[k][j]; } } int main() { int n,m; while(cin>>n>>m,n||m) { char a,b,c; init(); for(int i=1; i<=n; i++) { cin>>a>>b>>c; if(b!='-') mp[a][b]=1; if(c!='-') mp[a][c]=1; } floyd(); for(int i=1; i<=m; i++) { char p,q; cin>>p>>q; int ans=min(mp[p][q],mp[q][p]); if(ans == inf || !ans) { puts("-"); continue; } if(ans== 1) { if(mp[p][q] == 1) puts("child"); else puts("parent"); } else { if(mp[p][q] != inf) { ans -= 2; for(int j = 1 ; j <=ans ; j ++) printf("great-"); puts("grandchild"); } else { ans -= 2; for(int j = 1 ; j <=ans ; j ++) printf("great-"); puts("grandparent"); } } } } return 0; }