-
Notifications
You must be signed in to change notification settings - Fork 5
/
F.cpp
87 lines (75 loc) · 1.79 KB
/
F.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Created by Walingar on 06.03.2017.
//
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <climits>
long long n, m, s, f;
const unsigned long long MAX_N = 260;
using namespace std;
ifstream in("negcycle.in");
ofstream out("negcycle.out");
long long ans = 0;
struct edge {
long long from, to, w;
edge(long long x, long long y, long long z) {
from = x;
to = y;
w = z;
}
};
vector<edge> edges;
void input() {
in >> n;
m = 0;
for (long long i = 0; i < n; ++i)
for (long long j = 0; j < n; ++j) {
long long temp;
in >> temp;
if (temp != 1000 * 1000 * 1000) {
edges.push_back(edge(i, j, temp));
++m;
}
}
}
long long INF = LLONG_MAX / 2;
vector<long long> d(MAX_N, 0);
vector<long long> path(MAX_N, -1);
vector<long long> p;
void solve() {
long long was = -1;
for (long long i = 0; i < n; ++i) {
was = -1;
for (long long j = 0; j < m; ++j)
if (d[edges[j].to] > d[edges[j].from] + edges[j].w) {
d[edges[j].to] = max(-INF, d[edges[j].from] + edges[j].w);
path[edges[j].to] = edges[j].from;
was = edges[j].to;
}
}
if (was == -1) {
out << "NO";
exit(0);
} else {
long long y = was;
for (long long i = 0; i < n; ++i)
y = path[y];
for (long long cur = y;; cur = path[cur]) {
p.push_back(cur);
if (cur == y && p.size() > 1) break;
}
}
}
void output() {
reverse(p.begin(), p.end());
out << "YES" << "\n";
out << p.size() << "\n";
for (auto i : p) out << i + 1 << ' ';
}
int main() {
input();
solve();
output();
return 0;
}