Untitled
unknown
plain_text
3 years ago
1.2 kB
6
Indexable
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<long long> order;
vector<int> used(100000+1, 0);
void topsort(vector<vector<int>> &g, int v) {
used[v] = 1;
for (auto x : g[v]) {
if (!used[x]) {
topsort(g, x);
} else if (used[x] == 1) {
cout << -1;
exit(0);
}
}
used[v] = 2;
order.push_back(v + 1);
}
int main() {
long long n, m, u;
cin >> n;
long long time[n];
vector<vector<int>> g(n+1);
for (int i=0; i < n; i++) {
cin >> time[i];
}
for (int i=0; i < n; i++) {
cin >> m;
for (int j=0; j < m; j++) {
cin >> u;
g[u-1].push_back(i);
}
}
for (int i=0; i < n; i++) {
if (!used[i]) topsort(g, i);
}
reverse(order.begin(), order.end());
long long s = time[0], i = 0, c = 1;
while (order[i] != 1) {
s += time[order[i] - 1];
c++;
i++;
}
cout << s << " " << c << endl;
i = 0;
while (order[i] != 1) {
cout << order[i] << " ";
i++;
}
cout << 1;
return 0;
}
Editor is loading...