#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int n, m, a[N], cnt, tot;
vector<int>e[N], g[N];
int dfn[N], low[N]; //dfn[i]i节点的dfs序,low[i]i节点的回溯值
int book[N], id[N], sum[N], in[N], out[N]; //判断x是否在栈里
stack<int>stk;
int f[N];
void tarjan(int x) {
low[x] = dfn[x] = ++cnt;
stk.push(x);
book[x] = 1;
for (auto v : e[x]) {
if (!dfn[v]) {
tarjan(v);
low[x] = min(low[x], low[v]);
} else if (book[v]) {
low[x] = min(low[x], dfn[v]);
}
}
if (dfn[x] == low[x]) {
++tot;
int v;
do {
v = stk.top();
stk.pop();
book[v] = 0;
id[v] = tot;
sum[tot] += a[v];
} while (v != x);
}
}
void rebuild() {
//重新建图
for (int i = 1; i <= n; i++) {
for (auto v : e[i]) {
if (id[i] != id[v]) {
g[id[i]].push_back(id[v]);
in[id[v]]++;
}
}
}
}
void topsort() {
queue<int>q;
int res = 0;
for (int i = 1; i <= tot; i++) {
if (in[i] == 0)
q.push(i), f[i] = sum[i], res = max(res, f[i]);
}
while (!q.empty()) {
int tmp = q.front();
q.pop();
for (auto v : g[tmp]) {
in[v]--;
if (in[v] == 0)
q.push(v);
f[v] = max(f[v], f[tmp] + sum[v]);
res = max(res, f[v]);
}
}
cout << res << endl;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) {
tarjan(i);
}
}
rebuild();
topsort();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int cnt, tot, id[N], n, m, book[N], low[N], dfn[N];
int sum[N], out[N];
vector<int>e[N];
stack<int>stk;
void tarjan(int x) {
dfn[x] = low[x] = ++cnt;
stk.push(x);
book[x] = 1;
for (auto v : e[x]) {
if (!dfn[v]) {
tarjan(v);
low[x] = min(low[x], low[v]);
} else if (book[v]) {
low[x] = min(low[x], dfn[v]);
}
}
if (dfn[x] == low[x]) {
++tot;
int v;
do {
v = stk.top();
stk.pop();
sum[tot]++;
id[v] = tot;
} while (v != x);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i])
tarjan(i);
}
for (int i = 1; i <= n; i++) {
for (auto v : e[i]) {
//id[i]->id[v]
if (id[i] != id[v]) {
out[id[i]]++;
}
}
}
int res = 0, ans = 0;
for (int i = 1; i <= tot; i++) {
if (out[i] == 0) {
res++;
ans = sum[i];
}
}
if (res > 1)
cout << 0 << endl;
else
cout << ans << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
#define int long long
int n, m, a[N], cnt, tot, s, k, pos[N];
vector<int>e[N], g[N];
int dfn[N], low[N]; //dfn[i]i节点的dfs序,low[i]i节点的回溯值
int book[N], id[N], sum[N], in[N], out[N]; //判断x是否在栈里
stack<int>stk;
int f[N];
void tarjan(int x) {
low[x] = dfn[x] = ++cnt;
stk.push(x);
book[x] = 1;
for (auto v : e[x]) {
if (!dfn[v]) {
tarjan(v);
low[x] = min(low[x], low[v]);
} else if (book[v]) {
low[x] = min(low[x], dfn[v]);
}
}
if (dfn[x] == low[x]) {
++tot;
int v;
do {
v = stk.top();
stk.pop();
book[v] = 0;
id[v] = tot;
sum[tot] += a[v];
} while (v != x);
}
}
void rebuild() {
//重新建图
for (int i = 1; i <= n; i++) {
for (auto v : e[i]) {
if (id[i] != id[v]) {
g[id[i]].push_back(id[v]);
in[id[v]]++;
}
}
}
}
void topsort() {
queue<int>q;
int res = 0;
q.push(id[s]);
f[id[s]] = sum[id[s]];
for (int i = 1; i <= tot; i++) {
if (in[i] == 0 && i != id[s])
q.push(i), f[i] = -1e9;
}
while (!q.empty()) {
int tmp = q.front();
q.pop();
for (auto v : g[tmp]) {
in[v]--;
if (in[v] == 0)
q.push(v);
f[v] = max(f[v], f[tmp] + sum[v]);
}
}
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
}
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++) {
if (!dfn[i]) {
tarjan(i);
}
}
cin >> s >> k;
for (int i = 1; i <= k; i++) {
cin >> pos[i];
}
rebuild();
topsort();
int res = 0;
for (int i = 1; i <= k; i++) {
res = max(res, f[id[pos[i]]]);
}
cout << res << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m;
vector<int>e[N], g[N];
int low[N], dfn[N], book[N], cnt, tot, siz[N], id[N];
stack<int>stk;
int dis[N << 1], vis[N << 1];
void tarjan(int x) {
stk.push(x);
dfn[x] = low[x] = ++cnt;
book[x] = 1;
for (auto v : e[x]) {
if (!dfn[v]) {
tarjan(v);
low[x] = min(low[x], low[v]);
} else if (book[v])
low[x] = min(low[x], dfn[v]);
}
if (low[x] == dfn[x]) {
int v;
tot++;
do {
v = stk.top();
stk.pop();
book[v] = 0;
id[v] = tot;
siz[tot]++;
} while (x != v);
}
}
void rebuild() {
for (int i = 1; i <= n; i++) {
for (auto v : e[i]) {
if (id[i] != id[v]) {
g[id[i]].push_back(id[v]);
g[id[v]].push_back(id[i] + tot);
g[id[i] + tot].push_back(id[v] + tot);
}
}
}
}
void spfa() {
int res = 0;
//dis[id[1]] = siz[id[1]];
queue<int>q;
q.push(id[1]);
vis[id[1]] = 1;
while (!q.empty()) {
int tmp = q.front();
q.pop();
res = max(res, dis[tmp]);
vis[tmp] = 0;
for (auto v : g[tmp]) {
int t = v % tot;
if (t == 0)
t = tot;
if (dis[v] < dis[tmp] + siz[t]) {
dis[v] = dis[tmp] + siz[t];
if (!vis[v]) {
vis[v] = 1;
q.push(v);
}
}
}
}
cout << max(siz[id[1]],dis[id[1] + tot]) << endl;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i])
tarjan(i);
}
rebuild();
spfa();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e4 + 5,Mod=1e9;
int cnt, tot, n, m, dfn[N], low[N], book[N], id[N], siz[N], in[N];
stack<int>stk;
vector<int>e[N], g[N];
int vis1[N], vis2[N], ccnt[N];
void bfs() {
queue<int>q;
for (int i = 1; i <= n; i++) {
if (in[i] == 0)
q.push(i);
}
ccnt[1] = 1;
book[1] = 1;
//q.push(1);
while (!q.empty()) {
int tmp = q.front();
q.pop();
for (auto v : e[tmp]) {
if (vis2[v] == 1) {
ccnt[v] += ccnt[tmp];
ccnt[v]%=Mod;
--in[v];
if (in[v] == 0)
q.push(v);
}
}
}
// for (int i = 1; i <= n; i++)
// cout << ccnt[i] << " ";
cout << ccnt[2]%Mod << endl;
}
void tarjan(int x) {
dfn[x] = low[x] = ++cnt;
book[x] = 1;
stk.push(x);
for (auto v : e[x]) {
if (!dfn[v]) {
tarjan(v);
low[x] = min(low[x], low[v]);
} else if (book[v])
low[x] = min(low[x], dfn[v]);
}
if (low[x] == dfn[x]) {
tot++;
int v;
do {
v = stk.top();
stk.pop();
book[v] = 0;
id[v] = tot;
siz[tot]++;
} while (v != x);
}
}
void dfs1(int x) {
vis1[x] = 1;
for (auto v : e[x]) {
if (!vis1[v]) {
dfs1(v);
}
}
}
void dfs2(int x) {
vis2[x] = 1;
for (auto v : g[x]) {
if (!vis2[v])
dfs2(v);
}
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
in[y]++;
g[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i])
tarjan(i);
}
//判断从1出发可以到达的dian
dfs1(1);
dfs2(2);
if (vis1[2] == 0) {
cout << 0 << endl;
return 0;
}
for (int i = 1; i <= n; i++) {
if (vis1[i] == 1 && vis2[i] == 1 && siz[id[i]] > 1) {
cout << "inf" << endl;
return 0;
}
}
bfs();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5 + 5, Mod = 1e9;
int cnt, tot, n, m, dfn[N], low[N], book[N], id[N], siz[N], in[N];
stack<int>stk;
vector<int>e[N], g[N];
int vis1[N], vis2[N], ccnt[N];
map<pair<int, int>, int>mp;
void tarjan(int x) {
dfn[x] = low[x] = ++cnt;
book[x] = 1;
stk.push(x);
for (auto v : e[x]) {
if (!dfn[v]) {
tarjan(v);
low[x] = min(low[x], low[v]);
} else if (book[v])
low[x] = min(low[x], dfn[v]);
}
if (low[x] == dfn[x]) {
tot++;
int v;
do {
v = stk.top();
stk.pop();
book[v] = 0;
id[v] = tot;
siz[tot]++;
} while (v != x);
}
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i])
tarjan(i);
}
for (int i = 1; i <= n; i++) {
for (auto v : e[i]) {
if (id[i] != id[v] && mp.count({id[i], id[v]}) == 0) {
mp[ {id[i], id[v]}] = 1;
g[id[i]].push_back(id[v]);
in[id[v]]++;
}
}
}
int sum = 0, flag = 0;
for (int i = 1; i <= tot; i++) {
if (in[i] == 0)
sum++;
}
for (int i = 1; i <= tot; i++) {
if (siz[i] == 1 && flag == 0 && in[i] == 0) {
int T = 0;
for (auto v : g[i]) {
if (in[v] == 1) {
T = 1;
}
}
if (T == 0) {
flag = 1;
}
}
}
sum -= flag;
cout << fixed << setprecision(6) << double(n - sum) / double(n) << endl;
return 0;
}