某个比赛现场有来自不同学校的N名学生,给出M对“两人同属一所学校”的关系, 请推断学校数量,并找出人数最多的学校。
输入格式:
第一行是一个在[2, 1000]范围的整数N,
接下来N行,每行是一个在现场的学生的姓名,每个姓名仅由字母组成,长度不超过30。
接下来一行是非负整数M,表示有M对关系;
然后是M行,每行是用空格间隔的两个人名,表示同属一所学校。
输出格式:
在一行内分别输出学校的数量以及人数最多学校的人数,用一个空格分隔。
输入样例:
8
Bill
Ellen
Ann
Chris
Daisy
Flin
Henry
Grace
5
Ann Chris
Ellen Chris
Daisy Flin
Henry Ellen
Grace Flin
输出样例:
3 4
- 使用
sum[i]
表示根为i
的联通块的个数 - 初始化
sum[i]=1
- 合并
x
和y
时,sum[fa(x)]+=sum[fa(y)]
#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)1e5+7)
#define ll long long
#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;
map<string, int> mp;
int ID(string& str) {
int& rx = mp[str];
if(rx == 0) rx = ++ tot;
return rx;
}
int pre[MAXN], sum[MAXN];
int fa(int x) {
return (x==pre[x]) ? x : (pre[x]=fa(pre[x]));
}
void union_xy(int x, int y) {
int rx = fa(x), ry = fa(y);
if(x != y) {
sum[rx] += sum[ry];
pre[ry] = rx;
}
}
int main() {
#ifdef debug
freopen("test", "r", stdin);
// freopen("out_main", "w", stdout);
clock_t stime = clock();
#endif
cin >> n;
for(int i=1; i<=n; i++) {
string str;
cin >> str;
ID(str);
}
n = tot;
cin >> m;
for(int i=1; i<=n; i++) pre[i] = i, sum[i] = 1;
while(m--) {
string sa, sb;
cin >> sa >> sb;
int x = ID(sa), y = ID(sb);
union_xy(x, y);
}
int ans1 = 0, ans2 = 0;
// show(n);
// forarr(pre, 1, n);
for(int i=1; i<=n; i++)
if(pre[i] == i) ans1 ++, ans2 = max(ans2, sum[i]);
printf("%d %d\n", ans1, ans2);
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}