Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P2194 HXY烧情侣

P2194 HXY烧情侣

题意即每个强连通分量选择其中代价最低的点,求方案数。

求出所有的强连通分量,求其中最小费用的个数,乘起来即可。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <cstdio>
#include <stack>
#include <queue>
#include <climits>
#define inf INT_MAX
using namespace std;
typedef long long LL;
const int N = 1e5 + 10, M = 3e5 + 10, mod = 1e9 + 7;
bool vis[N];
int n, m, w[N];
int idx, hd[N], nxt[M], edg[M];
int cnt, stmp, id[N], dfn[N], low[N];
stack<int> stk;
vector<int> v[N];
void tarjan(int x)
{
dfn[x] = low[x] = ++stmp;
stk.push(x);
vis[x] = true;
for (int i = hd[x]; ~i; i = nxt[i])
if (!dfn[edg[i]])
{
tarjan(edg[i]);
low[x] = min(low[x], low[edg[i]]);
}
else if (vis[edg[i]])
low[x] = min(low[x], dfn[edg[i]]);
if (dfn[x] == low[x])
{
int y;
cnt++;
do
{
y = stk.top();
stk.pop();
vis[y] = false;
id[y] = cnt;
v[cnt].push_back(y);
} while (y != x);
}
}
void add(int a, int b)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
hd[i] = -1;
for (int i = 1; i <= n; i++)
scanf("%d", &w[i]);
scanf("%d", &m);
for (int i = 1, a, b; i <= m; i++)
{
scanf("%d%d", &a, &b);
add(a, b);
}
for (int i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i);
int f = 0;
LL g = 1;
for (int i = 1; i <= cnt; i++)
{
int mn = inf, tot = 0;
for (auto j : v[i])
if (w[j] == mn)
tot++;
else if (w[j] < mn)
{
mn = w[j];
tot = 1;
}
f += mn;
g = g * (LL)tot % mod;
}
printf("%d %lld", f, g);
return 0;
}