제출 #1361201

#제출 시각아이디문제언어결과실행 시간메모리
1361201faricaTour (BOI25_tou)C++20
0 / 100
0 ms344 KiB
#include <bits/stdc++.h>

using namespace std;
using vi = vector<int>;
using pi = pair<int,int>;

const int MAX = 1e9;

vector<vector<array<int,3>>>adjL;
vi path;
vector<pi>cnt;
vi visited;
int st, C;

bool mark(int pos, int clr) {
    if(cnt[pos].first == clr or cnt[pos].second == clr or cnt[pos].second != -1) return 0;
    if(cnt[pos].first == -1) cnt[pos].first = clr;
    else cnt[pos].second = clr;
    return 1;
}

int dfs(int pos, int c=-1) {
    for(auto &[adj, clr, id]: adjL[pos]) {
        if(c == clr or visited[id] == 2) continue;
        if(visited[id] == 1) {
            path.push_back(id);
            return id;
        }
        if(!mark(adj, clr)) continue;
        visited[id] = 1;
        int x = dfs(adj, clr);
        if(x != -1) {
            if(x == id) return -1;
            path.push_back(id);
            return x;
        }
        visited[id] = 2;
    }
    return -1;
}

void solve() {
    int n, m;
    cin >> n >> m;
    adjL.assign(n+1, vector<array<int,3>>());
    vector<array<int,3>>vec(m);
    cnt.assign(n+1, {-1,-1});
    visited.assign(m+1, 0);
    for(int i=0; i<m; ++i) {
        int x,y,c;
        cin >> x >> y >> c;
        vec[i] = {x,y,c};
        adjL[x].push_back({y,c,i});
    }
    path.clear();
    for(int i=0; i<n; ++i) {
        dfs(i);
        if(!path.empty()) {
            reverse(path.begin(), path.end());
            cout << "YES" << endl << path.size() << " ";
            for(int x: path) cout << x+1 << " ";
            cout << endl;
            return;
        }
    }
    cout << "NO" << endl;
}

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

    int tc;
    cin >> tc;
    while(tc--) solve();

    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…