제출 #1182218

#제출 시각아이디문제언어결과실행 시간메모리
1182218santi3223Marshmallow Molecules (CCO19_day2problem2)C++17
25 / 25
181 ms22160 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vl vector<ll>
#define vb vector<bool>
#define ed "\n"
#define fi first
#define se second
#define pll pair<ll, ll>
#define ff(aa, bb, cc) for(ll aa = bb; aa < cc; aa++)
#define pb push_back
#define all(aaa) aaa.begin(), aaa.end()

int main(){
	ll n, m;
	cin >> n >> m;
	map<ll, set<ll>> conexiones;
	ll c = 0;
	ff(i, 0, m){
		ll a, b;
		cin >> a >> b;
		a--;
		b--;
		conexiones[a].insert(b);
	}
	ff(i, 0, n){
		if(conexiones[i].empty()){
			continue;
		}
		auto val = *conexiones[i].begin();
		/*cout << i+1 << " " << conexiones[i].size() << ed;
		for(auto p : conexiones[i]){
			cout << " " << p+1;
		}
		cout << ed*/
		c += conexiones[i].size();
		conexiones[i].erase(val);
		//cout << "V " << val+1 << ed;
		if(conexiones[i].size()>conexiones[val].size()){
			//cout << "SWAP " << i+1 << " " << val+1 << ed;
			swap(conexiones[i], conexiones[val]);
		}
	//	cout << "ACT";
		for(auto p : conexiones[i]){
			//cout << " " << p+1;
			conexiones[val].insert(p);
		}
		//cout << ed << ed;
	}
	cout << c << ed;
 
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...