Tarjan 求查错
查看原帖
Tarjan 求查错
246099
dalao_see_me楼主2021/10/19 14:08

Rt,WA on test 10

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5 + 5, mod = 1e9 + 7;
int scc, tot, cnt, ans1, n, m, ans2;
struct edge {
	int to, nxt;
} e[N];
int a[N], vis[N], low[N], lst[N], dfn[N], Min[N], num[N];
stack <int> s;
inline int read() {
	int x = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') f = -f; c = getchar();}
	while (c >= '0' && c <= '9') {x = x * 10 + (c ^ 48); c = getchar();}
	return x * f;
}
inline void add(int u, int v) {
	e[++cnt].to = v;
	e[cnt].nxt = lst[u];
	lst[u] = cnt;
}
inline void Tarjan(int x) {
	low[x] = dfn[x] = ++tot;
	s.push(x); vis[x] = 1;
	for (int i = lst[x]; i; i = e[i].nxt) {
		int to = e[i].to;
		if (!dfn[to]) {
			Tarjan(to);
			low[x] = min(low[x], low[to]);
		}
		else if (vis[to]) low[x] = min(low[x], dfn[to]);
	}
	if (low[x] == dfn[x]) {
		++scc; int y; Min[scc] = 1e18;
		do {
			y = s.top(); s.pop(); vis[y] = 0;
			if (Min[scc] > a[y]) {
				Min[scc] = a[y];
				num[scc] = 1;
			}
			else if (Min[scc] == a[y])
				num[scc]++;
		} while (x != y);
	}
}
signed main() {
	n = read();
	for (int i = 1; i <= n; i++) a[i] = read();
	m = read();
	for (int i = 1; i <= m; i++) {
		int u = read(), v = read();
		add(u, v);
	}
	for (int i = 1; i <= n; i++)
		if (!dfn[i]) Tarjan(i);
	ans2 = 1;
	for (int i = 1; i <= scc; i++) {
		ans1 = (ans1 + Min[i]) % mod;
		ans2 = ans2 * num[i] % mod;
	}
	printf("%lld %lld", ans1, ans2);
	return 0;
}
2021/10/19 14:08
加载中...