PTA 7-8 朋友 简单图论

【问题描述】 同学们应该学会多交一些好朋友。朋友关系是相互的,A是B的好朋友,则B也是A 的好朋友。朋友关系是不传递的,A是B的好朋友,B是C的好朋友,但A和C不一定是 好朋友。现在给出某小学部分同学之间的朋友关系,请编程统计朋友最多的人有多少个好 朋友。

【输入数据】

输入共m+1行。

第1行是两个整数n和m,分别表示同学总人数和朋友关系对数。

第2行到第m+1行,描述了m对朋友关系。每行两个用单个空格隔开的同学姓名。

每个人的姓名仅由小写字母组成,且1≤姓名的长度≤10。

【输出数据】

一个整数,表示朋友最多的人有多少个好朋友。

【输入输出样例1】 4 3 lucy lily jam lily jam peter

2

【样例1解释】

4个人,3对朋友关系。

lucy只有一个朋友lily;

jam 有两个朋友lily和peter;

lily有两个朋友lucy和jam;

peter只有一个朋友jam。

所以lily和jam 朋友最多,都是2个。 【输入输出样例2】

6 5

andy bob

bella andy

bob andy

andy cassie

cassie bob

3

【样例2解释】

6个人,5对朋友关系。其中第1对朋友关系“andy bob”和第3对朋友关系“bob andy” 重复。

andy有三个朋友,分别是bob、bella和cassie;

bob有两个朋友andy和cassie;

bella只有一个朋友andy;

cassie有两个朋友bob和andy;

另外2个人没有朋友(这两个人在输入中没有出现)。

所以andy的朋友最多,有3个朋友。

【数据范围约定】 50%以上的测试点输入数据保证朋友关系没有重复。 100%的测试点输入数据保证2≤n≤100,1≤m≤1000,且没有自己跟自己的朋友关系。




建图后暴力 f o r for for就行了

#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

	char print_f[105];
	void read() {}
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
		inline void read(T &x, T2 &... oth) {
			x = 0;
			char ch = getchar();
			ll f = 1;
			while (!isdigit(ch)) {
				if (ch == '-') f *= -1; 
				ch = getchar();
			}
			while (isdigit(ch)) {
				x = x * 10 + ch - 48;
				ch = getchar();
			}
			x *= f;
			read(oth...);
		}
	template <typename T, typename... T2>
		inline void print(T x, T2... oth) {
			ll p3=-1;
			if(x<0) putchar('-'), x=-x;
			do{
				print_f[++p3] = x%10 + 48;
			} while(x/=10);
			while(p3>=0) putchar(print_f[p3--]);
			putchar(' ');
			print(oth...);
		}
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K, tot;

vector<int> G[MAXN];
map<string, int> mp;
map<pair<string,string>, int> vis;

int ID(string& str) {
	int& rx = mp[str];
	if(!rx) rx = ++ tot;
	return rx;
}

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	string su, sv;
	cin >> n >> m;
	while(m--) {
		cin >> su >> sv;
		if(vis[{su, sv}]) continue ;
		vis[{su, sv}] ++, vis[{sv, su}] ++;
		int u = ID(su), v = ID(sv);
		G[u].push_back(v), G[v].push_back(u);
	}
	int ans = 0;
	for(int i=1; i<=tot; i++) 
		ans = max(ans, (int)G[i].size());
	printf("%d\n", ans);



#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}




全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务