社区讨论
简单题 WA on test 8,极限数据对拍不出来
CF245G Suggested Friends参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lo1oetcw
- 此快照首次捕获于
- 2023/10/23 00:21 2 年前
- 此快照最后确认于
- 2023/11/03 01:02 2 年前
思路就是对每个点查找距离它 2 的点中路径数量最多的点的数量。如果不存在距离为 2 的点则输出 0;
极限数据对拍不出来,不知道哪里错了,感谢帮助!
CPP#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
int main()
{
int m;
cin >> m;
map<string, int> Map;
vector<PII> temp(m + 1);
int tot = 0;
for (int i = 1; i <= m; ++i)
{
string s, t;
cin >> s >> t;
if (!Map.count(s)) Map[s] = ++ tot;
if (!Map.count(t)) Map[t] = ++ tot;
temp[i] = {Map[s], Map[t]};
}
vector<vector<int> > to(tot + 1);
for (int i = 1; i <= m; ++i)
{
auto [x, y] = temp[i];
to[x].push_back(y);
to[y].push_back(x);
}
auto bfs = [&](int s)
{
queue<int> q;
q.push(s);
vector<int> d(tot + 1, 4), cnt(tot + 1, 0);
d[s] = 0;
while (q.size())
{
int x = q.front();
q.pop();
if (d[x] == 2)
{
++ cnt[x];
continue;
}
for (auto y : to[x])
if (d[x] + 1 <= d[y])
d[y] = d[x] + 1, q.push(y);
}
int mx = 0;
for (int i = 1; i <= tot; ++i)
mx = max(mx, cnt[i]);
if (!mx) return 0;
int res = 0;
for (int i = 1; i <= tot; ++i)
if (cnt[i] == mx) ++ res;
return res;
};
cout << Map.size() << '\n';
for (auto [name, id] : Map)
cout << name << ' ' << bfs(id) << '\n';
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...