Codeforces 245G Suggested Friends 暴力乱搞
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:
- x ≠ y;
- x and y aren't friends;
- 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 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 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 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[50]; int main() { scanf("%d", &m); for (int i = 0; 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] = 1; G[t].push_back(s); G[s].push_back(t); } printf("%d\n", n); for (int i = 0; i < n; i++) { printf("%s ", name[i].c_str()); int c = 0; int mf = 0; for (int j = 0; j < n; j++) { if (bi[i][j] || i == j)continue; int tmp = 0; for(int k=0;k<G[j].size();k++) if(bi[i][G[j][k]])tmp++; if (tmp > mf) { mf = tmp; c = 1; } else if (tmp == mf)c++; } printf("%d\n", c); } return 0; }