Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define pii pair<long long,long long>
- #define pll pair<long long, long long>
- #define rep(i,l,r) for(long long i = l;i<r;i++)
- #define SSTR( x ) static_cast< std::ostringstream & >( \
- ( std::ostringstream() << std::dec << x ) ).str()
- #define fastout ios_base::sync_with_stdio(false);cin.tie(NULL);
- using namespace std;
- using namespace __gnu_pbds;
- typedef tree<pll,null_type,less<pll>,rb_tree_tag,tree_order_statistics_node_update> order_set;
- const long long INF = (long long)1e9 + 7;
- const long long LLINF = (long long)1e18 + 7;
- mt19937 mt('O' + '4' + 'K' + 'O');
- void kekek()
- {
- mt();
- #ifdef EB_UG
- freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout);
- #endif
- #ifndef EB_UG
- ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
- #endif
- }
- struct match {
- string s1;
- int ind1;
- string s2;
- int ind2;
- };
- map<string, int> indx;
- map<int, int> cnt;
- int main() {
- kekek();
- int n;
- cin >> n;
- vector<match> turik(n);
- set<string> heh;
- for (int i = 0; i < n; i++) {
- cin >> turik[i].s1;
- cin >> turik[i].s2;
- heh.insert(turik[i].s1);
- heh.insert(turik[i].s2);
- }
- vector<string> peps;
- for (auto it: heh) {
- peps.push_back(it);
- }
- int pn = peps.size();
- for (int i = 0; i < pn; i++) {
- indx[peps[i]] = i;
- }
- for (auto &it: turik) {
- it.ind1 = indx[it.s1];
- it.ind2 = indx[it.s2];
- }
- for (auto it: turik) {
- cnt[indx[it.s1]]++;
- cnt[indx[it.s2]]++;
- }
- int need = (n + 1) >> 1;
- vector<bool> used(n + 10);
- vector<vector<int>> total(1000);
- int ind = 0;
- while(true) {
- if (need == 1)
- need *= 2;
- int mn = INF;
- for(auto it: cnt) {
- if (it.second == 0)
- continue;
- mn = min(mn, it.second);
- }
- if (mn == INF)
- break;
- vector<int> prek;
- for (auto it: cnt) {
- if (it.second == mn) {
- prek.push_back(it.first);
- }
- }
- if (prek.size() != need) {
- cout << "NO SOLUTION";
- return 0;
- }
- vector<int> del;
- for (int i = 0; i < n; i++) {
- if (used[i])
- continue;
- if (cnt[turik[i].ind1] == 1 || cnt[turik[i].ind2] == 1) {
- used[i] = true;
- del.push_back(i);
- }
- }
- for (auto it: del) {
- cnt[turik[it].ind1] = max(cnt[turik[it].ind1] - 1, 0);
- cnt[turik[it].ind2] = max(cnt[turik[it].ind2] - 1, 0);
- }
- total[ind] = prek;
- ind++;
- need >>= 1;
- }
- for (auto it: total[ind - 2]) {
- cout << peps[it] << ' ';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement