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. jvm架构以及Tomcat优化

      JVM栈 JVM栈是线程私有的,每个线程创建的同时都会创建JVM栈,JVM栈中存放的为当前线程中局部基本类型的变量(java中定义的八种基本类型:boolean.char.byte.short.i ...

  2. 使用IAR在开发nordic问题记录

    使用IAR在开发nordic的sdk的时候,官方有一段话*****Note for IAR 8 users:(Libraries for IAR 8 require wchar_t to be of ...

  3. 关于stm32优先级大小的理解

    转载自:https://www.cnblogs.com/ZKeJun/p/6112591.html 一. 组别:0>1>2>3>4   组别优先顺序(第0组优先级最强,第4组优 ...

  4. 二叉排序树:HDU3791-二叉搜索树(用指针建立二叉排序树)

    二叉搜索树 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...

  5. poj 2236 网络连接问题 并查集

    题意:n台电脑,当两台之间的距离小于d的时候可以连接. 题目会进行操作“修复”还有“查询是否已经连接”.只要在查询的时候输出YES或者ON 思路: 把可以相互连接的 即两者之间的距离小于 d  q[i ...

  6. AngularJS自定义指令directive:scope属性 (转载)

    原文地址:http://blog.csdn.net/VitaLemon__/article/details/52213103 一.介绍: 在AngularJS中,除了内置指令如ng-click等,我们 ...

  7. 静态方法中不能使用 $this

    忽略了一个问题,$this 代表当前对象!! 静态方法中应该使用 类名 . self 或者 static 关键字来代替! static public function get_info($id) { ...

  8. js常见的方法

    Ajax请求 jquery ajax函数 我自己封装了一个ajax的函数,代码如下: var Ajax = function(url, type success, error) { $.ajax({ ...

  9. CodeForces839D[莫比乌斯反演] Codeforces Round #428 (Div. 2)

    /*CodeForces839D[莫比乌斯反演]*/ #include <bits/stdc++.h> typedef long long LL; const LL MOD = 10000 ...

  10. 【bzoj3782】上学路线 dp+容斥原理+Lucas定理+中国剩余定理

    题目描述 小C所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M).小C家住在西南角,学校在东北角.现在有T个路口进行施工,小C不能通过这些路口.小C喜欢走最短的路径到达目的 ...