天梯赛:L2-016 愿天下有情人都是失散多年的兄妹 (25 分)

本文介绍了一个基于层序遍历的算法来判断两人的家族树中是否存在公共祖先,从而确定他们是否可以结婚。通过构建节点结构并使用集合存储遍历过程中的ID,最终比较两个集合是否有交集来得出结论。

题目详情 - L2-016 愿天下有情人都是失散多年的兄妹 (25 分) (pintia.cn)

题解:对于每个给出的编号进行层序遍历,把遍历到的元素存进集合里。如果两个集合存在交集,就代表它们存在公共祖先,否则可以结婚。

不过感觉这个有点BUG,编号为0的情况貌似没有考虑到。

Code Segment:

#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=1e5+1;
struct node{
    int father,mother;
    char sex;
}p[maxn];
void bfs(int id,set<int> &a)
{
    queue<int> q,e;
    int d,i;
    q.push(id);
    a.insert(id);
    for(i=0;i<4;i++)
    {
        while(!q.empty())
        {
            d=q.front();
            q.pop();
            if(p[d].father!=-1&&p[d].father)
            {
                a.insert(p[d].father);
                e.push(p[d].father);
            }
            if(p[d].mother!=-1&&p[d].mother)
            {
                a.insert(p[d].mother);
                e.push(p[d].mother);
            }
        }
        while(!e.empty())
        {
            q.push(e.front());
            e.pop();
        }
    }
}
int main()
{
    int n,k,id,a,b;
    set<int> s,t;
    vector<int> v;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&id);
        getchar();
        scanf("%c%d%d",&p[id].sex,&p[id].father,&p[id].mother);
        if(p[id].father!=-1)
            p[p[id].father].sex='M';
        if(p[id].mother!=-1)
            p[p[id].mother].sex='F';
    }
    scanf("%d",&k);
    while(k--)
    {
        scanf("%d%d",&a,&b);
        if(p[a].sex==p[b].sex)
        {
            printf("Never Mind\n");
            continue;
        }
        bfs(a,s);
        bfs(b,t);
        set_intersection(s.begin(),s.end(),t.begin(),t.end(),inserter(v,v.begin()));
        if(v.size())
            printf("No\n");
        else
            printf("Yes\n");
        v.clear();
        s.clear();
        t.clear();
    }
}

### L2-016 愿天下有情人都是失散多年兄妹 Java 实现 L2-016 是一个典型的图论问题,要求根据给定的家族成员关系判断某些人物之间是否存在特定的关系。以下是基于题目描述的 Java 实现代码: #### 代码实现 以下代码实现了通过广度优先搜索(BFS)或深度优先搜索(DFS)来验证家族成员之间的关系。 ```java import java.util.*; public class Main { static final int MAX_N = 100; static List<Integer>[] relations = new ArrayList[MAX_N]; static boolean[] visited; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); // 家族成员数量 int M = scanner.nextInt(); // 关系对数量 int K = scanner.nextInt(); // 查询对数量 // 初始化关系数组 for (int i = 0; i < MAX_N; i++) { relations[i] = new ArrayList<>(); } // 构建无向图 for (int i = 0; i < M; i++) { int a = scanner.nextInt() - 1; int b = scanner.nextInt() - 1; relations[a].add(b); relations[b].add(a); } // 处理查询 for (int i = 0; i < K; i++) { int c = scanner.nextInt(); int a = scanner.nextInt() - 1; int b = scanner.nextInt() - 1; if (checkRelation(c, a, b)) { System.out.println("Yes"); } else { System.out.println("No"); } } } // BFS 检查关系是否满足条件 private static boolean checkRelation(int c, int a, int b) { Queue<int[]> queue = new LinkedList<>(); visited = new boolean[MAX_N]; queue.add(new int[]{a, 0}); visited[a] = true; while (!queue.isEmpty()) { int[] current = queue.poll(); int person = current[0]; int distance = current[1]; if (person == b) { return distance <= c; } for (int neighbor : relations[person]) { if (!visited[neighbor]) { visited[neighbor] = true; queue.add(new int[]{neighbor, distance + 1}); } } } return false; } } ``` #### 代码解析 上述代码实现了家族成员关系的验证功能[^1]: 1. 使用 `ArrayList` 数组存储每个节点的邻居节点,构建无向图。 2. 输入数据包括家族成员数量、关系对数量以及查询对数量。 3. 对于每次查询,使用广度优先搜索(BFS)验证两个节点之间的距离是否小于等于给定值 `c`。 4. 如果满足条件,输出 `Yes`;否则输出 `No`。 #### 注意事项 - 题目中提到的关系是双向的,因此需要构建无向图。 - BFS 的时间复杂度为 O(V + E),其中 V 表示节点数,E 表示边数。由于题目规模较小,此方法完全适用[^2]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值