标签
Q4. 友達の友達
输出
输出结果
6 7 0
0 1
0 5
1 3
1 5
2 3
3 4
4 5
2
六种朋友的关系如下面的图。
阿尔勒的朋友是学生 1 和学生 5,阿尔勒的朋友的朋友是学生 3 和学生 4。由于 阿尔勒 有两个“朋友的朋友”,我们将输出 2。
5 7 2
0 2
0 3
1 2
1 4
2 3
2 4
3 4
0
阿尔勒和学校所有学生都是朋友,所以朋友的朋友为:0
#include <bits/stdc++.h>
using namespace std;
#define endl '\n';
void coder_solution();
int main() {
// 小码匠
coder_solution();
// 返回
return 0;
}
void coder_solution() {
// 提升cin、cout效率
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// 提升cin、cout效率
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, x, a, b;
cin >> n >> m >> x;
vector<vector<int>> c(n);
for(int i = 0; i < m; ++i) {
cin >> a >> b;
c[a].push_back(b);
c[b].push_back(a);
}
unordered_set<int> ans;
set<int> f;
for(auto i : c[x]) {
f.insert(i);
}
for(auto i : c[x]) {
for (auto j : c[i]) {
if (!f.count(j) && j != x) {
ans.insert(j);
}
}
}
cout << ans.size();
}