제출 #1353721

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

using namespace std;

vector<int>cyc;
vector<int>path;

const int inf = 1e9;

void dfs(int st, array<int,3>edges[], set<array<int,3>>g[], bool vis[]){
    vis[st]=1;
    int x = edges[st][0];
    int y = edges[st][1];
    int c = edges[st][2];
    path.push_back(st);
    auto it = g[y].begin();
    array<int,3>curr = *it;
    while(it!=g[y].end()){
        ///go through curr
        if(cyc.size()){
            return;
        }
        if(curr[0]!=c){
            ///can go further
            if(vis[curr[2]]){
                ///cycle found
                while(path.back()!=curr[2]){
                    cyc.push_back(path.back());
                    path.pop_back();
                }
                cyc.push_back(path.back());
                path.pop_back();
            }
            dfs(curr[2],edges,g,vis);
            g[y].erase(curr);
        }
        else{
            curr={c,inf,inf};
        }
        it=g[y].upper_bound(curr);
        curr=*it;
    }
    path.pop_back();
}

void solve(){
    int n,m;
    cin >> n >> m;
    set<array<int,3>>g[n];
    array<int,3>edges[m];
    for(int i = 0;i<m;i++){
        int x,y,c;
        cin >> x >> y >> c;
        x--;y--;
        g[x].insert({c,y,i});
        edges[i]={x,y,c};
    }
    bool vis[m];
    fill(vis,vis+m,0);
    cyc.clear();
    path.clear();
    for(int i = 0;i<m;i++){
        if(vis[i])
            continue;
        if(cyc.size()){
            break;
        }
        dfs(i,edges,g,vis);
        path.clear();
        int x = edges[i][0];
        g[x].erase({edges[i][2],edges[i][1],i});
    }
    if(cyc.size()==0){
        cout << "NO" << "\n";
    }
    else{
        reverse(cyc.begin(),cyc.end());
        cout << "YES" << "\n";
        cout << cyc.size() << " ";
        for(int i : cyc){
            cout << i+1 << " ";
        }
        cout << "\n";
    }
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…