标签:des style color io os ar for sp div
12 16 2 2 2 2 2 2 2 2 2 2 2 2 1 3 3 2 2 1 3 4 2 4 3 5 5 4 4 6 6 4 7 4 7 12 7 8 8 7 8 9 10 9 11 10
3 6题意:中文#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<limits.h> typedef long long LL; using namespace std; const int INF=0x3f3f3f; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int maxn=1100; const int maxm=10000; struct node{ int u,v; int next; }e[maxm]; int head[maxn],cntE; int DFN[maxn],low[maxn]; int s[maxm],top,index,cnt; int belong[maxn],instack[maxn]; int in[maxn],val[maxn]; int tt[maxn];//tt保存缩成的点中的最小值 int n,m; void init() { top=cntE=0; index=cnt=0; CLEAR(DFN,0); CLEAR(head,-1); CLEAR(instack,0); } void addedge(int u,int v) { e[cntE].u=u;e[cntE].v=v; e[cntE].next=head[u]; head[u]=cntE++; } void Tarjan(int u) { DFN[u]=low[u]=++index; instack[u]=1; s[top++]=u; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].v; if(!DFN[v]) { Tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]) low[u]=min(low[u],DFN[v]); } int v; if(DFN[u]==low[u]) { cnt++; do{ v=s[--top]; belong[v]=cnt; instack[v]=0; }while(u!=v); } } void work() { REPF(i,1,n) if(!DFN[i]) Tarjan(i); CLEAR(in,0); CLEAR(tt,INF); REPF(i,1,n) { if(tt[belong[i]]>val[i]) tt[belong[i]]=val[i]; } REPF(k,1,n) { for(int i=head[k];i!=-1;i=e[i].next) { int v=e[i].v; if(belong[k]!=belong[v]) in[belong[v]]++; } } int ans=0; int num=0; REPF(i,1,cnt) { if(!in[i]) { num++; ans+=tt[i]; } } printf("%d %d\n",num,ans); } int main() { int u,v; while(~scanf("%d%d",&n,&m)) { init(); for(int i=1;i<=n;i++) scanf("%d",&val[i]); for(int i=0;i<m;i++) { scanf("%d%d",&u,&v); addedge(u,v); } work(); } return 0; }
HDU 1827 Summer Holiday(Tarjan缩点)
标签:des style color io os ar for sp div
原文地址:http://blog.csdn.net/u013582254/article/details/40248195