没难度上代码。
#include <bits/stdc++.h>
using namespace std;
vector<vector<string>> v;
bool cmp (string a, string b) {
return a < b;
}
int main() {
int n, k, m, course;
string name;
scanf ("%d %d", &n, &k);
v.resize(k + 1);
for (int i = 0; i < n; i++) {
cin >> name >> m;
for (int j = 0; j < m; j++) {
scanf ("%d", &course);
v[course].push_back(name);
}
}
for (int i = 1; i <= k; i++) {
printf ("%d %d\n", i, v[i].size());
sort (v[i].begin(), v[i].end(), cmp);
for (auto it : v[i]) {
printf ("%s\n", it.c_str());
}
}
}