- toc {:toc}
๐ฆ1043: ๊ฑฐ์ง๋ง
[์๊ณ ๋ฆฌ์ฆ ํ์ด] ๊ฐ๋๊ท
Reviewed by Kade Kang (devkade12@gmail.com)
Reviewed:: 2024-02-08
๋ฌธ์ ํ์ธํ๊ธฐ
ํ์ด
-
vector์ ์ด๋ฆ ์ค์ ์ด ํท๊ฐ๋ ค์ ํ์ด๊ฐ ๋๋ ธ๋ค.
-
์ฌ์ ์ค์
- party[i]๋ i ๋ฒ์งธ ์ฌ๋์ด ์ฐธ๊ฐํ๋ ํํฐ ๋ฒํธ๋ฅผ ์๋ฏธํ๋ค.
- participant[i]๋ i ๋ฒ์งธ ํํฐ์ ์ฐธ์ฌํ๋ ์ฐธ๊ฐ์์ ๋ฒํธ ๋ชจ์์ ์๋ฏธํ๋ค. ์ฆ, 1๋ฒ์งธ ํํฐ์์ 1, 2, 3, 4๊ฐ ์ฐธ์ฌํ๋ ๊ฒฝ์ฐ participant[1]์๋ {1, 2, 3, 4}๊ฐ ๋ค์ด์๋ค.
- ํ์ด
- ์ง์ค์ ์๊ณ ์๋ ์ฌ๋๋ค์ ํ์ ๋ฃ๋๋ค.
- BFS๋ฅผ ์งํํ๋ฉด์ ์ง์ค์ ์๊ณ ์๋ ์ฌ๋๊ณผ ํจ๊ป ํํฐ์ ์ฐธ์ฌํ๋ ์ฌ๋๋ค์ ๋ฒํธ๋ฅผ ๋ชจ๋ ์ฒดํฌํ๋ค.
- ํ ํํฐ ์์์ ์ง์ค์ ์๊ณ ์๋ ์ฌ๋์ด ์๋ค๋ฉด res๋ฅผ ์ฆ๊ฐ์ํจ๋ค.
- res๋ฅผ ์ต์ข ์ถ๋ ฅํ๋ค.
#include <iostream>
#include <vector>
#include <queue>
#define endl '\n';
using namespace std;
vector<int> party[51] = {}; // N, i๋ฒ์งธ ์ฌ๋์ ์ฐธ๊ฐํ๋ ํํฐ ๋ฒํธ
vector<int> participant[51] = {}; // M, ํํฐ ์ฐธ๊ฐ์
int truth[51];
int N, M, res=0;
queue<int> q;
void Input(){
cin >> N >> M;
int val;
cin >> val;
for(int j=1; j<=val; j++){
int number;
cin >> number;
truth[number] = 1;
q.push(number);
}
for(int i=1; i<=M; i++){
int val;
cin >> val;
for(int j=1; j<=val; j++){
int number;
cin >> number;
party[number].push_back(i);
participant[i].push_back(number);
}
}
}
void bfs(){
while(!q.empty()){
int number = q.front(); q.pop();
for(auto partyNum : party[number]){
for(auto temp : participant[partyNum]){
if(truth[temp] == 1) continue;
truth[temp] = 1;
q.push(temp);
}
}
}
}
void Solution(){
bfs();
for(int i=1; i<=M; i++){
int flag = 1;
for(auto number : participant[i]){
if(truth[number]==1){
flag = 0;
break;
}
}
if(flag){
res++;
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
Input();
Solution();
cout << res << endl;
return 0;
}