官方题解|ACGO社区
2024-03-22 11:37:46
发布于:浙江
22阅读
0回复
0点赞
题目解析
考虑使用一种数据结构来高效维护用户 和用户 之间的关系。
使用 map<int, set<int>> user
来维护每位用户关注的所有其他用户:
user[A]
表示用户 关注的所有用户的集合。
如果满足条件 user[A]
中存在 且 user[B]
中存在 则说明 和 目前属于互相关注的状态。
AC代码
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; cin >> _;
while (_--) {
int n, q;
cin >> n >> q;
map<int, set<int>> fans;
for (int i = 0; i < q; ++i) {
int t, a, b;
cin >> t >> a >> b;
if (t == 1)
user[a].insert(b);
else if (t == 2)
user[a].erase(b);
else
cout << (user[a].count(b) and user[b].count(a) ? "Yes" : "No") << '\n';
}
}
return 0;
}
复杂度分析
对于每个操作时间复杂度为 ,总时间复杂度为 。
这里空空如也
有帮助,赞一个