G. Suggested Friends
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ai, bi (ai ≠ bi). Each pair ai, bi means that users ai and bi are friends. Friendship is symmetric, that is, if ai is friends with bi, then bi is also friends with ai. 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.

Input

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 ai and bi (ai ≠ bi). 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 xy and yx 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.

Output

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 ci and the number of his suggested friends di after a space.

You can print information about the users in any order.

题意

定义A是B的推荐朋友,当且仅当A拥有B最多的共同朋友,且A和B不是朋友。问每个人有多少推荐朋友

题解

由于边很少,只有5000条,那么可以暴力枚举两个点,再暴力判一下就好,这样绝对不会超时,至于为什么,大家可以脑补脑补。

代码

#include<iostream>
#include<bitset>
#include<vector>
#include<cstring>
#include<string>
#include<map>
#include<cstdio>
#define MAX_N 2*5555
using namespace std; map<string,int> ma; string name[MAX_N]; int n,m; bitset<MAX_N> bi[MAX_N];
vector<int> G[MAX_N]; char cc[]; int main() {
scanf("%d", &m);
for (int i = ; i < m; i++) {
string u, v;
scanf("%s", cc);
u = cc;
scanf("%s", cc);
v = cc;
if (ma.find(u) == ma.end())ma[u] = n++;
if (ma.find(v) == ma.end())ma[v] = n++;
int s = ma[u], t = ma[v];
name[s] = u;
name[t] = v;
bi[s][t] = bi[t][s] = ;
G[t].push_back(s);
G[s].push_back(t);
}
printf("%d\n", n);
for (int i = ; i < n; i++) {
printf("%s ", name[i].c_str());
int c = ;
int mf = ;
for (int j = ; j < n; j++) {
if (bi[i][j] || i == j)continue;
int tmp = ;
for(int k=;k<G[j].size();k++)
if(bi[i][G[j][k]])tmp++;
if (tmp > mf) {
mf = tmp;
c = ;
}
else if (tmp == mf)c++;
}
printf("%d\n", c);
}
return ;
}

Codeforces 245G Suggested Friends 暴力乱搞的更多相关文章

  1. VIJOS1476 旅行规划(树形Dp + DFS暴力乱搞)

    题意: 给出一个树,树上每一条边的边权为 1,求树上所有最长链的点集并. 细节: 可能存在多条最长链!最长链!最长链!重要的事情说三遍 分析: 方法round 1:暴力乱搞Q A Q,边权为正-> ...

  2. Codeforces 538G - Berserk Robot(乱搞)

    Codeforces 题目传送门 & 洛谷题目传送门 一道很神的乱搞题 %%% 首先注意到如果直接去做,横纵坐标有关联,不好搞.这里有一个非常套路的技巧--坐标轴旋转,我们不妨将整个坐标系旋转 ...

  3. Codeforces 306D - Polygon(随机化+乱搞)

    Codeforces 题目传送门 & 洛谷题目传送门 中考终于结束了--简单写道题恢复下状态罢. 首先这一类题目肯定没法用一般的方法解决,因此考虑用一些奇淫的乱搞做法解决这道题,不难发现,如果 ...

  4. codeforces 665C C. Simple Strings(乱搞)

    题目链接: C. Simple Strings time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. [CSP-S模拟测试]:Cicada拿衣服(暴力+乱搞)

    题目传送门(内部题94) 输入格式 第一行两个整数$n,k$,代表衣服的数量和阈值. 接下来一行$n$个数,第$i$个数$a_i$表示每件衣服的愉悦值. 输出格式 输出一行$n$个数,第$i$个数为$ ...

  6. Codeforces #254 div1 B. DZY Loves FFT 暴力乱搞

    B. DZY Loves FFT 题目连接: http://codeforces.com/contest/444/problem/B Description DZY loves Fast Fourie ...

  7. Codeforces Gym 100203G Good elements 暴力乱搞

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以 ...

  8. Codeforces 34C-Page Numbers(set+vector+暴力乱搞)

    C. Page Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  9. BZOJ 1491: [NOI2007]社交网络(Floyd+暴力乱搞)

    题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1491 题解: 先看数据范围,n<=100..欸可以乱搞了 首先因为小学学过的乘法原理 ...

随机推荐

  1. java web项目(spring项目)中集成webservice ,实现对外开放接口

    什么是WebService?webService小示例 点此了解 下面进入正题: Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤: 准备: 采用与spring ...

  2. 这一千个Python库,总有你想要的!

    环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具. Vex – 可以在虚拟环境中执行命令. v ...

  3. eclipse使用技巧的网站收集——转载(三)

    本文来自:https://www.cnblogs.com/jeffen/p/5965227.html,未经更改,尊重作者 工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个“器”.本 ...

  4. spring+struts2+hibernate框架依赖pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  5. POJ1719二分匹配

    第一次发文,就是一些学习的心得而已,自己忘得时候就可以来看看,好了废话不多说,直接说说这题的思路. 题意大概是每列只能射中一个白格子,每行可以射多个,让你输出这样的序列,明显的二分图求匹配,用列去选行 ...

  6. BZOJ 2721: [Violet 5]樱花

    (X-N)(Y-N)=N^2 #include<cstdio> using namespace std; const int mod=1e9+7; int n,cnt,isprime[10 ...

  7. oracle整体结构-内存结构、物理结构、逻辑结构、进程

    Oracle的体系结构大体上分为两部分:Instance(实例)和Database(数据库). Instance(实例) :在Oracle Instance中主要包含了SGA以及一些进程(例如:PMO ...

  8. rocketmq源码分析1-benchmark学习

    benchmark 分析 组成部分 三个java类,都含有main方法,可选的传递一些参数,诸如测试线程数量,消息体积大小.三个类分别用于测试普通生产者,事务生产者,消费者.生产者 默认64个测试线程 ...

  9. Linux 安装 tree命令

    通过yum在线安装tree包 yum install tree -y

  10. Leetcode 476.数字的补数

    数字的补数 给定一个正整数,输出它的补数.补数是对该数的二进制表示取反. 注意: 给定的整数保证在32位带符号整数的范围内. 你可以假定二进制数不包含前导零位. 示例 1: 输入: 5 输出: 2 解 ...