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. HTTP认证之基本认证——Basic(一)

    导航 HTTP认证之基本认证--Basic(一) HTTP认证之基本认证--Basic(二) HTTP认证之摘要认证--Digest(一) HTTP认证之摘要认证--Digest(二) 一.概述 Ba ...

  2. Page-Object思想

    为什么要使用page-object 集中管理元素对象 集中管理一个page内的公共方法 后期维护方便 集中管理元素对象 实现方法: 调用方法: WebElement element = dri ...

  3. HDU 5473 There was a kingdom 凸包 DP

    题意: 给出平面上n个点的坐标,选k个点,使得这k个点围起来的面积最大. 分析: 参考了 叉姐的分析 和 不慌不忙菊苣的代码 思路我都懂,但是DP的部分还是不太会写. 我体会了一下其中含义,也许这样可 ...

  4. 文件上传下载,命令之wget / curl / which / sort / uniq / cut / wc /tr /sed

    目录 命令 1.文件的上传下载 2.从外网下载文件wget 3.curl文件下载 4.查找命令which 5.字符处理命令-排序sort 6.字符处理-去重uniq 7.字符处理-截取cut 8.字符 ...

  5. luogu2394 yyy loves Chemistry I

    练习 #include <iostream> #include <cstdio> using namespace std; long double a; int main(){ ...

  6. webdriver高级应用-js操作滚动条

    1.滑动页面的滚动条到页面最下面 2.滑动页面的滚动条到页面的某个元素 3.滑动页面的滚动条向下移动某个数量的像素 #encoding=utf-8 from selenium import webdr ...

  7. python-高级编程-05-异步IO

    [异步非阻塞IO] ------------------------------------------------------------------------------------------ ...

  8. Deep Android Malware Detection小结

    题目:Deep Android Malware Detection 作者:Niall McLaughlin, Jesus Martinez del Rincon, BooJoong Kang 年份:2 ...

  9. aiomysql inserting operation failed !

    emotions: those days,i am using aiomysql(python3.5) to acess my database .But a 'strange' problem ma ...

  10. pat 1036

    1036. 跟奥巴马一起编程(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 美国总统奥巴马不仅呼吁所有人 ...