A8857 | Suggested Friends
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Polycarpus works as a programmer in a start-up social network. His boss gave his a task to develop a mechanism for determining suggested friends. Polycarpus thought much about the task and came to the folowing conclusion.
Let's say that all friendship relationships in a social network are given as $m$ username pairs $a_{i},b_{i}$ $(a_{i}≠b_{i})$ . Each pair $a_{i},b_{i}$ means that users $a_{i}$ and $b_{i}$ are friends. Friendship is symmetric, that is, if $a_{i}$ is friends with $b_{i}$ , then $b_{i}$ is also friends with $a_{i}$ . User $y$ is a suggested friend for user $x$ , if the following conditions are met:
1. $x≠y$ ;
2. $x$ and $y$ aren't friends;
3. among all network users who meet the first two conditions, user $y$ has most of all common friends with user $x$ . User $z$ is a common friend of user $x$ and user $y$ $(z≠x,z≠y)$ , if $x$ and $z$ are friends, and $y$ and $z$ are also friends.
Your task is to help Polycarpus to implement a mechanism for determining suggested friends.
Let's say that all friendship relationships in a social network are given as $m$ username pairs $a_{i},b_{i}$ $(a_{i}≠b_{i})$ . Each pair $a_{i},b_{i}$ means that users $a_{i}$ and $b_{i}$ are friends. Friendship is symmetric, that is, if $a_{i}$ is friends with $b_{i}$ , then $b_{i}$ is also friends with $a_{i}$ . User $y$ is a suggested friend for user $x$ , if the following conditions are met:
1. $x≠y$ ;
2. $x$ and $y$ aren't friends;
3. among all network users who meet the first two conditions, user $y$ has most of all common friends with user $x$ . User $z$ is a common friend of user $x$ and user $y$ $(z≠x,z≠y)$ , if $x$ and $z$ are friends, and $y$ and $z$ are also friends.
Your task is to help Polycarpus to implement a mechanism for determining suggested friends.
输入格式
The first line contains a single integer $m$ $(1<=m<=5000)$ — the number of pairs of friends in the social network. Next $m$ lines contain pairs of names of the users who are friends with each other. The $i$ -th line contains two space-separated names $a_{i}$ and $b_{i}$ $(a_{i}≠b_{i})$ . The users' names are non-empty and consist of at most 20 uppercase and lowercase English letters.
It is guaranteed that each pair of friends occurs only once in the input. For example, the input can't contain $x$ , $y$ and $y$ , $x$ at the same time. It is guaranteed that distinct users have distinct names. It is guaranteed that each social network user has at least one friend. The last thing guarantees that each username occurs at least once in the input.
It is guaranteed that each pair of friends occurs only once in the input. For example, the input can't contain $x$ , $y$ and $y$ , $x$ at the same time. It is guaranteed that distinct users have distinct names. It is guaranteed that each social network user has at least one friend. The last thing guarantees that each username occurs at least once in the input.
输出格式
In the first line print a single integer $n$ — the number of network users. In next $n$ lines print the number of suggested friends for each user. In the $i$ -th line print the name of the user $c_{i}$ and the number of his suggested friends $d_{i}$ after a space.
You can print information about the users in any order.
You can print information about the users in any order.
输入输出样例
输入 #1
5 Mike Gerald Kate Mike Kate Tank Gerald Tank Gerald David
输出 #1
5 Mike 1 Gerald 1 Kate 1 Tank 1 David 2
输入 #2
4 valera vanya valera edik pasha valera igor valera
输出 #2
5 valera 0 vanya 3 edik 3 pasha 3 igor 3
In the first test case consider user David. Users Mike and Tank have one common friend (Gerald) with David. User Kate has no common friends with David. That's why David's suggested friends are users Mike and Tank.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted