ZJNU 2133 - 认亲大会
将辈分差距转为数字
例如 A 是 B son
A=B-1
A 是 B grandfather
A=B+2
然后编号1数字设置为0,建图bfs
最后搜索编号2到100是否存在>0的数即可
#include<bits/stdc++.h> using namespace std; typedef pair<int,int> P; vector<P> v[105]; queue<int> q; int dis[105]; bool vis[105]; int main(){ memset(dis,0x3f,sizeof dis); int N,i,now,cnt,a,b; char s[15]; scanf("%d",&N); while(N--){ scanf("%d is %d's %s",&a,&b,s); if(strcmp(s,"father.")==0){ v[a].push_back(P(b,-1)); v[b].push_back(P(a,1)); } else if(strcmp(s,"son.")==0){ v[a].push_back(P(b,1)); v[b].push_back(P(a,-1)); } else if(strcmp(s,"brother.")==0){ v[a].push_back(P(b,0)); v[b].push_back(P(a,0)); } else if(strcmp(s,"grandson.")==0){ v[a].push_back(P(b,2)); v[b].push_back(P(a,-2)); } else if(strcmp(s,"grandfather.")==0){ v[a].push_back(P(b,-2)); v[b].push_back(P(a,2)); } } vis[1]=true; dis[1]=0; q.push(1); while(!q.empty()){ now=q.front(); q.pop(); cnt=v[now].size(); for(i=0;i<cnt;i++) if(!vis[v[now][i].first]){ vis[v[now][i].first]=true; dis[v[now][i].first]=dis[now]+v[now][i].second; q.push(v[now][i].first); } } bool flag=true; for(i=2;i<=100;i++) if(dis[i]!=0x3f3f3f3f&&dis[i]>0){ flag=false; break; } puts(flag?"2333333...":"ku jiu ru hou xin zuo tong."); return 0; }