auto optimize_cpp_stdio = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return0; }(); classSolution { public: conststaticint maxn = 2e4 + 1; int deg[maxn] = {}; int id[maxn];
unordered_map<int, int> mp; vector<int> countPairs(int n, vector<vector<int>> &edges, vector<int> &queries) { int m = edges.size(); for (int i = 0, u, v; i < m; ++i) { u = edges[i][0] - 1; v = edges[i][1] - 1; deg[u]++; deg[v]++; if (u > v) swap(u, v); mp[u * n + v]++; } for(int i = 0; i < n; ++i) id[i] = i; sort(id, id + n, [&](constint &x, constint &y) { return deg[x] < deg[y]; });
for (auto &q : queries) { int ans = 0; int l = 0, r = n - 1, u, v; while (l < r) { u = id[l], v = id[r]; if (deg[u] + deg[v] <= q) ++l; else { ans += r - l; r--; } } for (auto &kv : mp) { int u = kv.first / n; int v = kv.first % n; // u = id[u], v = id[v]; int tot = deg[u] + deg[v]; if (tot > q && tot - kv.second <= q) { ans--; } } q = ans; } return queries; } };
inlineboolgetline() { if (!getline(cin, buf_line)) returnfalse; _i = 0, _n = buf_line.length(); returntrue; } template <classT> inlinevoidshow(T *a, intn) { cout << "["; for (int i = 0; i < n; ++i) { if (i != 0) cout << ","; cout << a[i]; } cout << "]"; }
boolendofl() { if (_i >= _n) returntrue; if (_i == 0) returnfalse; if (buf_line[_i - 1] == ']') { _i++; returntrue; } returnfalse; }
template <classT, std::size_t Num> inlinevoidshow(T a[][Num], int n, int m) { cout << "["; for (int i = 0; i < n; ++i) { if (i != 0) cout << ","; show(a[i], m); } cout << "]"; }
} // namespace FAST_IO usingnamespace FAST_IO;
int init = [] { /*********** fast_read ***************/ freopen("user.out", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); /*************************************/ conststaticint maxn = 2e4 + 1; conststaticint maxm = 1e5 + 1; staticint deg[maxn] = {}; staticint cnts[maxm * 2] = {}; unordered_map<int, int> mp; int n = 0, m, k, u, v, max_deg, cnts_size = 0; int queries[21]; while (true) { if (!getline()) break; mp.clear(); for(int i = 0; i < n; ++i) deg[i] = 0; for(int i = 0; i < cnts_size; ++i) cnts[i] = 0; read(n); getline(); for (m = 0; !endofl(); ++m) { read(u, v); u--; v--; deg[u]++; deg[v]++; if (u > v) swap(u, v); mp[u * n + v]++; endofl(); } getline(); for (k = 0; !endofl(); ++k) { read(queries[k]); } max_deg = 0; unordered_map<int, int> freq_deg; for (int i = 0; i < n; ++i) { freq_deg[deg[i]]++; max_deg = max(max_deg, deg[i]); } // 计算cnts[deg[u] + deg[v]]次数 cnts_size = max_deg * 2 + 1; for (auto &kv1 : freq_deg) { for (auto &kv2 : freq_deg) { if (kv1.first > kv2.first) continue; if (kv1.first == kv2.first) { cnts[kv1.first * 2] += kv1.second * (kv1.second - 1) / 2; } else { cnts[kv1.first + kv2.first] += kv1.second * kv2.second; } } } // 去掉重边 for (auto &kv : mp) { int u = kv.first / n; int v = kv.first % n; int tot = deg[u] + deg[v]; cnts[tot]--; cnts[tot - kv.second]++; } // cnts 后缀和 for (int i = cnts_size - 1; i >= 0; --i) { cnts[i] += cnts[i + 1]; } cout << "["; for (int i = 0; i < k; ++i) { int &q = queries[i]; if (q + 1 < cnts_size) { q = cnts[q + 1]; } else q = 0; if (i == 0) cout << q; else cout << "," << q; } cout << "]\n"; } exit(0); return0; }();