前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ACMSGURU 398 - Friends of Friends

ACMSGURU 398 - Friends of Friends

作者头像
Reck Zhang
发布2021-08-11 10:50:49
2250
发布2021-08-11 10:50:49
举报
文章被收录于专栏:Reck Zhang

Friends of Friends

Problem Description

Social networks are very popular now. They use different types of relationships to organize individual users in a network. In this problem friendship is used as a method to connect users. For each user you are given the list of his friends. Consider friendship as a symmetric relation, so if user a is a friend of user b then b is a friend of a.

A friend of a friend for a is such a user c that c is not a friend of a, but there is such b that b is a friend of a and c is a friend of b. Obviously c ≠ a.

Your task is to find the list of friends of friends for the given user x.

Input

The first line of the input contains integer numbers N and x (1 ≤ N ≤ 50, 1 ≤ x ≤ N), where N is the total number of users and x is user to be processed. Users in the input are specified by their numbers, integers between 1 and N inclusive. The following N lines describe friends list of each user. The i-th line contains integer di (0 ≤ di ≤ 50) — number of friends of the i-th user. After it there are di distinct integers between 1 and N — friends of the i-th user. The list doesn’t contain i. It is guaranteed that if user a is a friend of user b then b is a friend of a.

Output

You should output the number of friends of friends of x in the first line. Second line should contain friends of friends of x printed in the increasing order.

Example(s)

sample input

sample output

4 21 22 1 32 4 21 3

14

sample input

sample output

4 13 4 3 23 1 3 43 1 2 43 1 2 3

0

Solution

代码语言:javascript
复制
#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);

    int n, x;
    std::cin >> n >> x;
    std::vector<std::set<int>> friends(n + 1, std::set<int>{});

    for(int i = 1; i <= n; i++) {
        int num;
        std::cin >> num;
        while(num--) {
            int j;
            std::cin >> j;
            friends[i].insert(j);
        }
    }

    std::vector<int> res{};
    for(const auto& index : friends[x]) {
        for(const auto& second_index : friends[index]) {
            if(second_index != x && friends[x].find(second_index) == friends[x].end()) {
                res.push_back(second_index);
            }
        }
    }

    std::sort(res.begin(), res.end());
    res.erase(std::unique(res.begin(), res.end()), res.end());

    std::cout << res.size() << std::endl;
    for(const auto& num : res) {
        std::cout << num << " ";
    }

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-12-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Friends of Friends
    • Problem Description
      • Input
        • Output
          • Example(s)
            • Solution
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档