#ifdef LOCAL #include"algo/debug.h" #else #define debug(...) 42 #endif
#define rep(i, a, n) for (int i = a; i <= n; i++) #define per(i, a, n) for (int i = n; i >= a; i--) #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) #define mp make_pair #define pb push_back #define fi first #define se second #define pi acos(-1) #define il inline #define rg register #define SZ(x) ((int)(x).size()) #define all(x) x.begin(), x.end() #define INF 0x7fffffff; #define inf 0x3f3f3f3f; #define MOD 998244353; #define mod 1000000007; typedef vector<int> VI; typedef pair<int, int> PII; typedeflonglong ll; typedefdouble db; ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
intmain(){ int n, m; cin >> n >> m; VI val(n + 1, 0); for (int i = 1; i <= n; ++i) cin >> val[i]; vector<VI> adj(n + 1); for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); }
int sc = 0; VI dfn(n + 1, 0), low(n + 1, 0); vector<VI> SCC; SCC.push_back(VI{}); VI scc(n + 1, 0); bitset<10000> in; in.reset(); stack<int> sta;
for (int i = 1; i <= n; ++i) if (dfn[i] == 0) tarjan(i);
int N = (int)SCC.size() - 1; vector<VI> edge(N + 1); vector<int> Val(N + 1); vector<int> In(N + 1, 0);
for (int i = 1; i <= N; ++i) for (int u : SCC[i]) Val[i] += val[u];
for (int u = 1; u <= n; ++u) { for (int &v : adj[u]) { if (scc[u] != scc[v]) { edge[scc[u]].push_back(scc[v]); In[scc[v]]++; } } }
set<int> s; for (int i = 1; i <= N; ++i) { if (In[i] == 0) s.insert(i); } vector<int> top; while (!s.empty()) { int u = *s.begin(); for (int v : edge[u]) { In[v]--; if (In[v] == 0) s.insert(v); } top.push_back(u); s.erase(u); }
vector<int> dp(N + 1); for (int u : top) for (int v : edge[u]) dp[v] = max(dp[v], dp[u] + Val[u]);
int ans = 0; for (auto u : top) ans = max(ans, dp[u] + Val[u]); cout << ans << '\n';
vector<int> a(n + 1); for (int i = 1; i <= n; ++i) cin >> a[i];
vector<vector<int>> adj(n + 1), inv(n + 1); for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); inv[v].push_back(u); }
bitset<10000> vis; vis.reset(); function<void(int)> dfs1 = [&](int u) { vis[u] = 1; for (int &v : adj[u]) if (!vis[v]) dfs1(v); s.push_back(u); }; for (int i = 1; i <= n; ++i) if (vis[i] == 0) dfs1(i);
int sc = 0; vector<int> scc(n + 1, 0);
vector<int> val; function<void(int)> dfs2 = [&](int u) { scc[u] = sc; val[sc] += a[u]; for (int &v : inv[u]) if (!scc[v]) dfs2(v); }; val.push_back(0); for (int i = n - 1; i >= 0; --i) { if (!scc[s[i]]) { ++sc; val.push_back(0); dfs2(s[i]); } }
vector<int> dp(sc + 1, 0), in(sc + 1, 0); vector<vector<int>> to(sc + 1); for (int i = 1; i <= n; ++i) for (int &v : adj[i]) if (scc[i] != scc[v]) in[scc[v]]++, to[scc[i]].push_back(scc[v]); set<int> S; for (int i = 1; i <= sc; ++i) if (!in[i]) S.insert(i); while (!S.empty()) { int u = *S.begin(); for (int &v : to[u]) { if (--in[v] == 0) S.insert(v); dp[v] = max(dp[v], dp[u] + val[u]); } S.erase(u); } int ans = 0; for (int i = 1; i <= sc; ++i) ans = max(ans, dp[i] + val[i]); cout << ans; return0; }
#ifdef LOCAL #include"algo/debug.h" #else #define debug(...) 42 #endif
#define rep(i, a, n) for (int i = a; i <= n; i++) #define per(i, a, n) for (int i = n; i >= a; i--) #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) #define mp make_pair #define pb push_back #define fi first #define se second #define pi acos(-1) #define il inline #define rg register #define SZ(x) ((int)(x).size()) #define all(x) x.begin(), x.end() #define INF 0x7fffffff; #define inf 0x3f3f3f3f; #define MOD 998244353; #define mod 1000000007; typedef vector<int> VI; typedef pair<int, int> PII; typedeflonglong ll; typedefdouble db; ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
intmain(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<VI> adj(n + 1); for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } VI low(n + 1), dfn(n + 1, 0); vector<VI> v_bcc; stack<int> sta; int root; VI cut(n + 1, 0); function<void(int)> tarjan = [&](int u) { staticint dfc = 0; low[u] = dfn[u] = ++dfc; sta.push(u); int cnt = 0; for (int v : adj[u]) { if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); if (low[v] >= dfn[u]) { cnt++; VI tem; if (cnt > 1 || root != u) { cut[u] = 1; while (!sta.empty() && sta.top() != v) { tem.pb(sta.top()); sta.pop(); } tem.pb(sta.top()); sta.pop(); tem.pb(u); v_bcc.pb(tem); } } } else low[u] = min(low[u], dfn[v]); } }; for (int i = 1; i <= n; ++i) { if (!dfn[i]) { root = i; tarjan(i); } } vector<int> tem; while (!sta.empty()) { tem.pb(sta.top()); sta.pop(); } v_bcc.pb(tem); for (auto it : v_bcc) { for (auto iter : it) cout << iter << ' '; cout << '\n'; } return0; }