오류로 글이 2개 떠있었는데 오류인지 모르고 하나 삭제했더니 둘 다 삭제돼서 다시 쓰는 벨로그
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
int N, M;
vector<vector<int>> computer;
vector<int> visit;
pair<float, vector<int>> bfs(int start) {
vector<int> component;
set<pair<int, int>> circuit;
float cnt = 1;
visit[start] = 1;
queue<int> q;
q.push(start);
component.emplace_back(start);
while(!q.empty()) {
int now = q.front();
q.pop();
if(computer[now].empty()) continue;
for(int i : computer[now]) {
if(now < i) circuit.insert({now, i});
else circuit.insert({i, now});
if(!visit[i]) {
visit[i] = 1;
q.push(i);
component.emplace_back(i);
++cnt;
}
}
}
return {float(circuit.size()) / cnt, component};
}
vector<int> bfsAll() {
float max = 0.0;
vector<int> component;
for(int i = 1; i <= N; i++) {
if(!visit[i]) {
pair<float, vector<int>> now = bfs(i);
float density = now.first;
if(density > max) {
max = density;
component = now.second;
}
else if(density == max) {
if(component.size() < now.second.size()){
component = now.second;
}
else if(component.size() == now.second.size()) {
if(component[0] > now.second[0])
component = now.second;
}
}
}
}
return component;
}
int main() {
cin >> N >> M;
computer = vector<vector<int>>(N + 1, vector<int>(0, 0));
visit = vector<int>(N + 1, 0);
for(int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
computer[a].emplace_back(b);
computer[b].emplace_back(a);
}
vector<int> v = bfsAll();
sort(v.begin(), v.end());
for(int i : v){
cout << i << " ";
}
return 0;
}