202人阅读 评论(0)

# Summer Holiday

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2371    Accepted Submission(s): 1111

Problem Description
To see a World in a Grain of Sand
And a Heaven in a Wild Flower,
Hold Infinity in the palm of your hand
And Eternity in an hour.
—— William Blake

Input

Output

Sample Input
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

Sample Output
3 6

Author

Source

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=100010;
int MIN(int a,int b){
return a<b?a:b;
}
int value[maxn];
int scc_cnt,dfs_clock;
int sccno[maxn];
int dfn[maxn];
int low[maxn];
int num[maxn];
int in[maxn];
int out[maxn];
bool instack[maxn];
stack<int>S;
vector<int>scc[maxn];
vector<int>G[maxn];
struct Node{
int from,to,next;
}A[maxn];
void init(){
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(instack,false,sizeof(instack));
memset(sccno,0,sizeof(sccno));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(value,0x3f,sizeof(value));
scc_cnt=dfs_clock=0;
}
void tarjan(int u,int pre){
low[u]=dfn[u]=++dfs_clock;
S.push(u);instack[u]=true;
int v;
v=A[k].to;
if(!dfn[v]){
tarjan(v,u);
low[u]=MIN(low[u],low[v]);
}
else if(instack[v]){
low[u]=MIN(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
scc_cnt++;scc[scc_cnt].clear();
while(1){
v=S.top();S.pop();
instack[v]=false;
sccno[v]=scc_cnt;
G[scc_cnt].clear();
scc[scc_cnt].push_back(v);
value[scc_cnt]=MIN(value[scc_cnt],num[v]);
if(v==u)break;

}
}
}
void suodian(int m){
for(int i=0;i<m;++i){
int u=sccno[A[i].from];
int v=sccno[A[i].to];
if(u!=v){
G[u].push_back(v);
in[v]++;out[u]++;
}
}
}
int main()
{
int n,m,i,j,k;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(i=1;i<=n;++i){
scanf("%d",&num[i]);
}
int a,b;
for(i=0;i<m;++i){
scanf("%d%d",&a,&b);
A[i].from=a;
A[i].to=b;
}
for(i=1;i<=n;++i){
if(!dfn[i])tarjan(i,-1);
}
suodian(m);
int ans1=0,ans2=0;
for(i=1;i<=scc_cnt;++i){
if(in[i]==0){
ans1++;
ans2+=value[i];
}
}
printf("%d %d\n",ans1,ans2);
}
return 0;
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：247470次
• 积分：10322
• 等级：
• 排名：第1623名
• 原创：819篇
• 转载：5篇
• 译文：1篇
• 评论：24条
行到水穷处 ， 坐看云起时。