제출 #1332028

#제출 시각아이디문제언어결과실행 시간메모리
1332028AndreyTour (BOI25_tou)C++20
100 / 100
1778 ms270716 KiB
#include<bits/stdc++.h>
using namespace std;

vector<pair<int,int>> haha[1000001];
vector<int> col(1000001);
set<pair<int,int>> idk[1000001];
multiset<int> banana[1000001];
vector<int> br(1000001);
vector<bool> bruh(1000001);
vector<int> s(1000001);
vector<int> en(1000001);
queue<int> yo;

int n,m;

void refresh() {
    while(!yo.empty()) {
        yo.pop();
    }
    for(int i = 1; i <= n; i++) {
        haha[i].clear();
        idk[i].clear();
        banana[i].clear();
        br[i] = 0;
    }
    for(int i = 0; i < m; i++) {
        bruh[i] = true;
    }
}

void removeedge(pair<int,int> x, int u) {
    idk[u].erase(x);
    bruh[x.second] = false;
    int v = s[x.second];
    banana[v].erase(banana[v].find(x.first));
    if(banana[v].find(x.first) == banana[v].end()) {
        br[v]--;

        if(br[v] <= 1) {
            yo.push(v);
        }
    }
}

void solve() {
    cin >> n >> m;
    refresh();
    int a,b,c;
    for(int i = 0; i < m; i++) {
        cin >> a >> b >> c;
        haha[a].push_back({b,i});
        col[i] = c;
        s[i] = a;
        en[i] = b;
        if(banana[a].find(c) == banana[a].end()) {
            br[a]++;
        }
        banana[a].insert(c);
        idk[b].insert({c,i});
    }
    for(int i = 1; i <= n; i++) {
        if(br[i] <= 1) {
            yo.push(i);
        }
    }
    while(!yo.empty()) {
        int u = yo.front();
        yo.pop();
        if(br[u] == 0) {
            while(!idk[u].empty()) {
                pair<int,int> x = *idk[u].lower_bound({-67,-67});
                removeedge(x,u);
            }
        }
        else {
            int d = *banana[u].lower_bound(-67);
            while(true) {
                auto x = idk[u].lower_bound({d,-67});
                if(x == idk[u].end() || (*x).first != d) {
                    break;
                }
                removeedge(*x,u);
            }
        }
    }
    vector<int> w(n+1,-1);
    vector<int> z(n+1,-1);
    int y = -1,p = -1;
    for(int i = 1; i <= n; i++) {
        for(pair<int,int> v: haha[i]) {
            if(bruh[v.second]) {
                if(w[i] == -1) {
                    y = i;
                    w[i] = v.second;
                }
                else if(z[i] == -1 && col[w[i]] != col[v.second]) {
                    z[i] = v.second;
                }
            }
        }
    }
    vector<int> yeah(m,-1);
    if(y == -1) {
        cout << "NO\n";
        return;
    }
    vector<int> ans(0);
    while(true) {
        int r = w[y];
        if(col[w[y]] == p) {
            r = z[y];
        }
        if(yeah[r] != -1) {
            y = yeah[r];
            break;
        }
        ans.push_back(r);
        yeah[r] = ans.size()-1;
        p = col[r];
        y = en[r];
    }
    cout << "YES " << ans.size()-y << " ";
    for(int i = y; i < ans.size(); i++) {
        cout << ans[i]+1 << " ";
    }
    cout << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin >> t;
    while(t--) {
        solve();
    }
    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...