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
| #include<bits/stdc++.h> #define INIT(a,b) memset(a,b,sizeof(a)) #define LL long long using namespace std; const int inf=0x3f3f3f3f; const int maxn=1e3+7; const int maxm=2e3+7; const int mod=1e9+7; struct Edge{int from,to,next;}edge[maxm]; int Begin[maxn],dfn[maxn],low[maxn],vis[maxn],sta[maxn],color[maxn]; int val[maxn],minV[maxn],inde[maxn]; int n,m,x,y,dfsNum,colNum,tot,top,ansV,ansN; void init(){ INIT(Begin,-1);INIT(dfn,0); INIT(low,0);INIT(vis,0); INIT(color,0);INIT(minV,inf); INIT(inde,0); colNum=0,dfsNum=0,tot=0,top=-1,ansV=0,ansN=0; } void add(int x,int y){ edge[tot]=(Edge){x,y,Begin[x]}; Begin[x]=tot++; } void Tarjan(int x){ dfn[x]=low[x]=++dfsNum; vis[x]=1; sta[++top]=x; for(int i=Begin[x];~i;i=edge[i].next){ int ne=edge[i].to; if(!dfn[ne]){ Tarjan(ne); low[x]=min(low[x],low[ne]); } else if(vis[ne]) low[x]=min(low[x],dfn[ne]); } if(low[x]==dfn[x]){ vis[x]=0; color[x]=++colNum; minV[colNum]=min(minV[colNum],val[x]); while(sta[top]!=x){ vis[sta[top]]=0; minV[colNum]=min(minV[colNum],val[sta[top]]); color[sta[top--]]=colNum; } top--; } } int main(){ while(~scanf("%d%d",&n,&m)){ init(); for(int i=1;i<=n;i++) scanf("%d",&val[i]); while(m--){ scanf("%d%d",&x,&y); add(x,y); } for(int i=1;i<=n;i++){ if(!dfn[i]) Tarjan(i); } for(int i=0;i<tot;i++){ if(color[edge[i].from]!=color[edge[i].to]){ inde[color[edge[i].to]]++; } } for(int i=1;i<=colNum;i++){ if(inde[i]==0){ ansN++; ansV+=minV[i]; } } printf("%d %d\n",ansN,ansV); } return 0; }
|