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
| #include <cstring> #include <cstdio> #include <cctype> #include <cmath> #include <stack> using std::stack; #define long long ll #define register int ri
char buf[1<<21],*p1=buf,*p2=buf; inline int gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;} inline int read() { char v = gc();int x = 0,f = 1; while (!isdigit(v)) {if (v == '-')f = -1;v = gc();} while (isdigit(v)) {x = x * 10 + v - 48;v = gc();} return x * f; } const int N = 100001; const int M = 500001; const int INF = 0x3f3f3f3f;
int to[M],hd[N],nxt[M],tot;
inline void add(int u,int v) {to[++tot] = v;nxt[tot] = hd[u];hd[u] = tot;} inline void addedge(int u,int v) {add(u,v),add(v,u);}
template <typename T> inline T min(T x,T y) {return x<y?x:y;} template <typename T> inline T max(T x,T y) {return x>y?x:y;}
int dfn[N],low[N],c[N],cnt,num,siz[N],p[N],pre[N],ins[N],n,m,ans1,ans2 = 1; stack <int> s;
void tarjan(int x) { dfn[x] = low[x] = ++cnt; s.push(x);ins[x] = 1; for (int i = hd[x];i;i = nxt[i]) { if (!dfn[to[i]]) { tarjan(to[i]); low[x] = min(low[x],low[to[i]]); } else if (ins[to[i]]) { low[x] = min(low[x],dfn[to[i]]); } } if (dfn[x] == low[x]) { int y;c[x] = ++num; do { y = s.top();c[y] = num;ins[y] = 0;s.pop(); if (pre[y] < p[num]) { p[num] = pre[y]; siz[num] = 1; } else if (pre[y] == p[num]) { ++siz[num]; } }while (x != y); } }
signed main() { memset(p,INF,sizeof(p)); n = read(); for (int i = 1;i <= n;++i) pre[i] = read(); m = read(); for (int i = 1;i <= m;++i) { int x = read(),y = read();add(x,y); } for (int i = 1;i <= n;++i) { if (!dfn[i]) tarjan(i); } for (int i = 1;i <= num;++i) { ans1 += p[i];ans2 *= siz[i]; } printf("%d %d",ans1,ans2); return 0; }
|