제출 #1366193

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

const int MAXN = 4e6 + 10;
const int INF = 1e9;

int color[MAXN], vis[MAXN], out[MAXN];

int pai[MAXN];

vector<pair<int, pair<int, int>>> adj[MAXN], inv[MAXN];

vector<int> nadj[MAXN];

map<int, int> nodes;

bool cycle = false;

vector<int> ans;

int n, m;

int cur_vtx = 0;

void get_cycle(int u, int v){

  while(v != u){
    if(v < m) ans.push_back(v);
    v = pai[v];
  }

  if(u < m) ans.push_back(u);
  reverse(ans.begin(), ans.end());

}

void dfs(int v){

  vis[v] = 1;

  for(auto u : nadj[v]){
    if(!vis[u]){

      pai[u] = v;
      dfs(u);

    } else if(!out[u]){

      if(!cycle) get_cycle(u, v);
      cycle = true;

    }
  }

  out[v] = 1;

}

void add(int e1, int j, int x, int lx, int rx, int l, int r){

  if(rx < l || lx > r) return;

  if(l <= lx && rx <= r){

    if(nodes.find(x) == nodes.end()){

      nodes[x] = cur_vtx;

      for(int i=lx; i<=rx; i++) nadj[cur_vtx].push_back(adj[j][i - 1].second.second);
      cur_vtx ++;

    }

    int e2 = nodes[x];
    nadj[e1].push_back(e2);

    return;

  }

  int m = (lx + rx) / 2, lc = 2 * x, rc = 2 * x + 1;

  add(e1, j, lc, lx, m, l, r);
  add(e1, j, rc, m + 1, rx, l, r);

}

void solve(){

  cin >> n >> m;

  cycle = false;

  ans.clear();

  for(int i=1; i<=n; i++){
    adj[i].clear();
    inv[i].clear();
  }

  for(int i=0; i<cur_vtx; i++){
    nadj[i].clear();
    vis[i] = out[i] = 0;
  }

  for(int i=0; i<m; i++){

    int a, b; cin >> a >> b >> color[i];

    adj[a].push_back({color[i], {b, i}});
    inv[b].push_back({color[i], {a, i}});

  }

  for(int i=1; i<=n; i++){
    sort(adj[i].begin(), adj[i].end());
    sort(inv[i].rbegin(), inv[i].rend());
  }

  cur_vtx = m;

  for(int j=1; j<=n; j++){

    int sz = (int) adj[j].size();

    int pos_l = sz, pos_r = sz;

    for(auto [c, x] : inv[j]){

      while(pos_l > 0 && adj[j][pos_l - 1].first >= c) pos_l --;
      while(pos_r > 0 && adj[j][pos_r - 1].first > c) pos_r --;

      auto [i, e1] = x;

      // agora sei que quero conectar e1 num intervalo de arestas do j

      // cout << e1 << " has color " << c << endl;
      // cout << "sz of " << j << " = " << sz << endl;
      // for(auto [c, x] : adj[j]) cout << c << " "; cout << endl;

      // cout << i << " -> " << j << endl;

      // cout << "add " << e1 << " in " << 1 << " " << pos_l << " of " << j << endl;
      // cout << "add " << e1 << " in " << pos_r + 1 << " " << sz << " of " << j << endl;

      add(e1, j, 1, 1, sz, 1, pos_l);
      add(e1, j, 1, 1, sz, pos_r + 1, sz);

    }

    nodes.clear();

  }

  for(int i=0; i<m; i++) if(!vis[i]) dfs(i);

  if(!cycle){
    cout << "NO\n";
    return;
  }

  cout << "YES\n";

  cout << (int) ans.size() << " ";

  for(auto i : ans) cout << i + 1 << " ";
  cout << "\n";

}

int main(){
  cin.tie(0)->sync_with_stdio(0);

  int t; cin >> t;

  while(t--){
    solve();
  }

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