62342023-11-08 12:09:33CWMZsonglőrködéscpp17Accepted 100/100907ms80692 KiB
// KKGyak.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
using namespace std;

#include <iostream>
#include <map>
#include <set>
#include <unordered_set>
#include <vector>
#include <algorithm>

int main()
{
    int a;
    cin >> a;
	map<int, multiset<int>> dataStr;
	set<int> exists;
	vector<int> exsistsVec;
	for (size_t i = 0; i < a; i++)
	{
		int a, b;
		cin >> a >> b;
		if (exists.count(a) == 0) {
			dataStr.insert({ a,multiset<int>() });
			exists.insert(a);
			exsistsVec.push_back(a);
		}
		dataStr[a].insert(b);
	}
	int res = 0;
	int testedIdx = 0;
	sort(exsistsVec.begin(), exsistsVec.end());
	while (true)
	{
		int chain = -1;
		int convToMap = exsistsVec[testedIdx];
		/*for (auto m : dataStr) {
			if (m.second.size() > 0) {
				chain = *m.second.begin();
				dataStr[m.first].erase(dataStr[m.first].begin());
				break;
			}
		}*/
		while (true) {
			if (dataStr[exsistsVec[testedIdx]].size() == 0) {
				testedIdx++;
				if (exsistsVec.size() <= testedIdx) break;
			}
			else {
				chain = *dataStr[exsistsVec[testedIdx]].begin();
				dataStr[exsistsVec[testedIdx]].erase(dataStr[exsistsVec[testedIdx]].begin());
				break;
			}
		}
		if (chain == -1) break;
		while (true)
		{
			if (exists.count(chain) == 1 && dataStr[chain].size()>0) {
				int old = chain;
				chain = *dataStr[chain].begin();
				dataStr[old].erase(dataStr[old].begin());
			}
			else break;
		}
		res++;
	}
	cout << res;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1816 KiB
2Accepted3ms2000 KiB
subtask220/20
3Accepted7ms3020 KiB
4Accepted199ms25136 KiB
5Accepted550ms59884 KiB
6Accepted782ms71352 KiB
7Accepted883ms79152 KiB
8Accepted906ms79340 KiB
9Accepted772ms79472 KiB
10Accepted879ms79884 KiB
11Accepted769ms79720 KiB
subtask335/35
12Accepted2ms3468 KiB
13Accepted3ms3596 KiB
14Accepted3ms3624 KiB
15Accepted3ms3620 KiB
16Accepted3ms4004 KiB
17Accepted4ms4096 KiB
18Accepted3ms3984 KiB
19Accepted4ms4092 KiB
20Accepted4ms4088 KiB
subtask445/45
21Accepted7ms4716 KiB
22Accepted83ms9840 KiB
23Accepted617ms61424 KiB
24Accepted259ms21092 KiB
25Accepted907ms80340 KiB
26Accepted305ms22816 KiB
27Accepted878ms80344 KiB
28Accepted282ms22916 KiB
29Accepted763ms80692 KiB