Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- typedef long long ll;
- #define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
- using namespace std;
- const ll INF = 1e5;
- int mx[101][101];
- vector<vector<int>> g;
- vector<int> cycle, cnt, ans;
- void dfs(int v) {
- cnt[v]++;
- cycle.push_back(v + 1);
- if (cnt[v] >= 2) {
- int j = 0;
- while (cycle[j] != v + 1) ++j;
- for (int i = j; i < cycle.size(); ++i) {
- ans.push_back(cycle[i]);
- }
- return;
- }
- for (auto u : g[v]) {
- if (mx[u][u] < 0) {
- dfs(u);
- return;
- }
- }
- }
- int main() {
- fast
- int n;
- cin >> n;
- g.resize(n); cnt.resize(n, 0);
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- cin >> mx[i][j];
- if (mx[i][j] != INF) {
- g[i].push_back(j);
- }
- }
- }
- for (int k = 0; k < n; ++k) {
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- if (mx[i][k] < INF && mx[k][j] < INF) {
- mx[i][j] = min(mx[i][j], mx[i][k] + mx[k][j]);
- }
- }
- }
- }
- for (int i = 0; i < n; ++i) {
- if (mx[i][i] < 0) {
- dfs(i);
- break;
- }
- }
- if (cycle.empty()) {
- cout << "NO" << '\n';
- } else {
- cout << "YES" << '\n';
- cout << ans.size() << '\n';
- for (auto el : ans) cout << el << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement