개발세리의 성장기🌿
STL) 5. set 본문
0. set 헤더
#include <set>
1. set 특징
-중복되는 값을 넣어도 안들어감, 수학적 집합의 개념임
-자동으로 작은 수 부터 정렬됨
2. set 사용법
#incldue <set> //헤더
set<int> s; //set 선언
s.insert(10); //삽입 현재 set: 10
s.insert(2); //삽입 현재 set: 2, 10
set<int>::iterator it; //set<int>형 반복자 (포인터 같은거임)
for (it = s.begin(); it != s.end(); it++) { //set 출력하려면 반복자 사용해야함
cout << *it << endl; //2 출력
cout << *it << endl; //10 출력
}
#include <iostream>
#include <vector>
#include <utility>
#include <set>
#include <algorithm>
using namespace std;
vector< pair<int, int>> total;
set<int> f;
set<int> ff;
set<int> invite;
int n, m;
bool cmp(pair<int, int> a, pair<int, int> b) {
return a.first < b.first;
}
void input() {
cin >> n >> m;
int a, b;
for (int i = 0; i< m; i++) {
cin >> a >> b;
if (a == 1) { //상근이랑 친구인 관계
f.insert(b);
invite.insert(b);
}
else if (b == 1) {
f.insert(a);
invite.insert(a);
}
else { //상근이랑 친친구 이거나 아무것도 아닌 관계
total.push_back(make_pair(a, b));
}
}
sort(total.begin(), total.end(), cmp);
}
void sol() {
int fr;
int restart = 0;
set<int>::iterator it;
for (it = f.begin(); it != f.end(); it++) {
fr = *it;
//cout << "fr: "<< fr <<endl;
for (int j = 0; j< total.size(); j++) {
if (total[j].first == fr) {
//cout << "invite: "<< total[j].second <<endl;
invite.insert(total[j].second);
}
if (total[j].second == fr) {
//cout << "invite: "<< total[j].second <<endl;
invite.insert(total[j].first);
}
}
}
}
int main() {
input();
sol();
cout << invite.size();
return 0;
}
'algorithm > STL' 카테고리의 다른 글
STL) 4. sort() 함수 사용하기 (0) | 2020.03.10 |
---|---|
STL) 3. vector와 pair 같이 쓰기 (0) | 2020.03.10 |
STL) 2. Vector (0) | 2020.02.26 |
STL) 1. STL이란 (0) | 2020.02.15 |
Comments