제출 #614090

#제출 시각아이디문제언어결과실행 시간메모리
614090OttoTheDinoTeam Contest (JOI22_team)C++17
100 / 100
784 ms79428 KiB
#include <bits/stdc++.h>
using namespace std;

#define rep(i,s,e)                  for (int i = s; i <= e; ++i)
#define rrep(i,s,e)                 for (int i = s; i >= e; --i)
#define pb                          push_back
#define pf                          push_front
#define fi                          first
#define se                          second
#define all(a)                      a.begin(), a.end()
#define len(a)                      (int)a.size()
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    map<int, vi> with[3];
    map<int, int> cnt[3];
    queue<int> rem;

    int n; cin >> n;
    int have[n+1] = {}, d[n+1][3], dun[n+1] = {};
    vi b[3];

    rep (i,1,n) {
        rep (j,0,2) {
            cin >> d[i][j];
            with[j][d[i][j]].pb(i);
            if (!cnt[j][d[i][j]]++) b[j].pb(d[i][j]);
        }
    }

    rep (i,0,2) {
        sort(all(b[i]));
        for (int el : with[i][b[i].back()])
            if (++have[el]>1 && !dun[el]) {
                rem.push(el);
                dun[el] = 1;
            }
    }

    while (len(rem)) {
        int u = rem.front();
        rem.pop();
        rep (i,0,2) {
            --cnt[i][d[u][i]];
            bool c = 0;
            while (len(b[i]) && !cnt[i][b[i].back()]) {
                b[i].pop_back();
                c = 1;
            }
            if (!len(b[i])) {
                cout << "-1\n";
                return 0;
            }
            if (c) {
                for (int el : with[i][b[i].back()])
                    if (++have[el]>1 && !dun[el]) {
                        rem.push(el);
                        dun[el] = 1;
                    }
            }
        }
    }

    cout << b[0].back() + b[1].back() + b[2].back() << "\n";

    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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...