Submission

Status:
[PPPPP][PPPPP]

Score: 100

User: Pera

Problemset: จุดแวะพัก

Language: cpp

Time: 0.013 second

Submitted On: 2025-03-24 22:38:00

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int n, target; 

    // no one visited -> -1
    // first 3 people
    // if same stops, consider name

    cin >> n >> target;
    vector<pair<string, int>> visitedK;
    
    for (int i = 0; i < n; i++) {
        string name;
        cin >> name;

        vector<int> stops;
        int stop;
        while (cin >> stop) {
            stops.push_back(stop);

            if (cin.peek() == '\n' || cin.peek() == EOF) break; 
        }

        auto it = find(stops.begin(), stops.end(), target);
        if (it != stops.end()) {
            visitedK.push_back({name, distance(stops.begin(), it)});
        }

    }

    sort(visitedK.begin(), visitedK.end(), [](pair<string, int>& a, pair<string, int>& b) {
        if (a.second != b.second) return a.second < b.second;
        else {
            return a.first < b.first;
        }
    });

    if (visitedK.empty()) {
        cout << -1 << '\n';
    } else {
        int count{0};
        for (pair<string, int> p : visitedK) {
            if (count >= 3) break;
            cout << p.first << ' ';
            count++;
        }
        cout << '\n';
    }
}