#include #include #include using namespace std; using i64 = long long; struct edge { int to; i64 cost; }; bool visit(const vector> &G, int v, vector &order, vector &color) { color[v] = 1; for(edge e : G[v]) { if(color[e.to] == 2) { continue; } if(color[e.to] == 1) { return false; } if(!visit(G, e.to, order, color)) { return false; } } order.push_back(v); color[v] = 2; return true; } bool topological_sort(const vector> &G, vector &order) { int n = G.size(); vector color(n); for(int u=0; u> fG(n, vector()), rG(n, vector()); for(int i=0; i order; bool ok = topological_sort(fG, order); assert(ok); vector dp0(n), dp1(n); for(int i=0; i