Tarjan(强连通分量) - Checkposts - CodeForces 427C
题意:
输入:
输出:
Examples
Input
3
1 2 3
3
1 2
2 3
3 2
Output
3 1
Input
5
2 8 0 6 0
6
1 4
1 3
2 4
3 4
4 5
5 1
Output
8 2
Input
10
1 3 2 2 1 3 1 4 10 10
12
1 2
2 3
3 1
3 4
4 5
5 6
5 7
6 4
7 3
8 9
9 10
10 9
Output
15 6
Input
2
7 91
2
1 2
2 1
Output
7 1
数据范围:
分析:
代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=100010, M=300010, mod=1e9+7;
int n,m;
int e[M],ne[M],h[N],w[N],idx;
int stk[N],top;
bool in_stk[N];
int id[N],ssc_cnt;
int dfn[N],low[N],timestamp;
int minp[N],cnt_minp[N];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void tarjan(int u)
{
dfn[u]=low[u]=++timestamp;
stk[++top]=u,in_stk[u]=true;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(!dfn[j])
{
tarjan(j);
low[u]=min(low[u],low[j]);
}
else if(in_stk[j]) low[u]=min(low[u],dfn[j]);
}
if(dfn[u]==low[u])
{
++ssc_cnt;
int y;
do
{
y=stk[top--];
in_stk[y]=false;
id[y]=ssc_cnt;
if(minp[ssc_cnt]>w[y]) minp[ssc_cnt]=w[y], cnt_minp[ssc_cnt]=1;
else if(minp[ssc_cnt]==w[y]) cnt_minp[ssc_cnt]++;
}while(y!=u);
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&w[i]);
cin>>m;
memset(h,-1,sizeof h);
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
memset(minp,0x3f,sizeof minp);
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
ll res1=0, res2=1;
for(int i=1;i<=ssc_cnt;i++)
{
res1+=minp[i];
res2=(ll)res2*cnt_minp[i]%mod;
}
cout<<res1<<' '<<res2<<endl;
return 0;
}