안녕하세요. 오늘은 걸그룹 마스터가 될 거예요.

문제

https://www.acmicpc.net/problem/16165

아이디어

맵에 저장을 하면 됩니다.
이때 그룹을 저장할 때에는 이름은 string, 멤버들은 vector로 저장을 해주면 됩니다.

소스코드

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#define ll long long
using namespace std;

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	ll N, M, i, j, type;
	map <string, string> group;
	map <string, vector <string> > members;
	string s, s2;

	cin >> N >> M;
	for (i = 1; i <= N; i++)
	{
		cin >> s;
		ll N2;
		cin >> N2;

		for (j = 1; j <= N2; j++)
		{
			cin >> s2;
			group[s2] = s;
			members[s].push_back(s2);
			sort(members[s].begin(), members[s].end());
		}
	}

	for (i = 0; i < M; i++)
	{
		cin >> s >> type;
		if (type == 0)
		{
			for (string temp : members[s])
				cout << temp << "\n";
		}
		else
		{
			cout << group[s] << "\n";
		}
	}
}


감사합니다.

0개의 댓글