Title Description
Students should learn to make more good friends. Friend relationship is mutual. If a is a good friend of B, then B is also a good friend of A. A is a good friend of B, B is a good friend of C, but a and C are not necessarily good friends. Now give the relationship between some students in a primary school. Please program and count how many good friends the person with the most friends has.
input
Enter a total of m+1 lines.
The first line is two integers, n and m, representing the total number of students and the logarithm of friends.
Lines 2 to m+1 describe the m-to-friend relationship. Two student names separated by a single space in each line.
Each person's name is only composed of lowercase letters, and the length of 1 ≤ name ≤ 10.
sample input
4 3
lucy lily
jam lily
jam peter
sample output
2
Tips
Four people, three friends. lucy has only one friend lily; jam has two friends Lily and peter; Lily has two friends lucy and jam;
peter has only one friend, jam.
So lily and jam have the most friends, two of them.
More than 50% of the test points input data to ensure that the relationship between friends is not repeated.
100% of the test point input data guarantees that 2 ≤ n ≤ 100, 1 ≤ m ≤ 1000, and there is no self friend relationship.
Just found out, the practice of the seniors, link: https://blog.csdn.net/qq_/article/details/84929875? OPS ﹐ request ﹐ misc =% 7b% 22request% 5fid% 22% 3A% 221582115942197248358843038% 22% 2C% 22scm% 22% 3A% 2220140713.130056874 %22%7D&request_id=158211594219724835843038&biz_id=0&utm_source=distribute.pc_search_result.none-task
It deals with strings as array subscripts.
Then, I tried to do it with map and debugged it.
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma comment(linker, "/stack:200000000") #pragma GCC optimize (2) #pragma G++ optimize (2) #include <bits/stdc++.h> using namespace std; ///#define wuyt main typedef long long ll; #define HEAP(...) priority_queue<__VA_ARGS__ > #define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > > template<class T> inline T min(T &x,const T &y){return x>y?y:x;} template<class T> inline T max(T &x,const T &y){return x<y?y:x;} ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar(); if(c == '-')Nig = -1,c = getchar(); while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar(); return Nig*x;} #define read read() const ll inf = 1e15; const int maxn = 2e5 + 7; const int mod = 1e9 + 7; #define start int main() #define end return 0 int n,m; start{ n=read,m=read; string a,b; int ans=-1; map<string,int> frien; map<string,map<string,int>> judge; while(m--){ cin>>a; cin>>b; if(judge[a][b]==0){///There's no relationship between them. Let's build a relationship frien[a]++;//The number of friends corresponding to two people increases frien[b]++; judge[a][b]=judge[b][a] = 1;///A is B's good friend, then B is a's good friend ans = max(ans,max(frien[a],frien[b]));///Keep updating maximum } } printf("%d",ans); end; }
In fact, it's like using a two-dimensional array to do this problem. To change the idea to mp, use
map<string,int> frien; map<string,map<string,int>> judge;
These two lines are defined as two-dimensional arrays, and the final output ans is the answer.