洛谷 P2194 HXY烧情侣

P2194 HXY烧情侣

裸tarjan

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 #define LL long long
 9 #define maxn 1000000
10 #define inf 0x3f3f3f3f
11 const LL hzy=1e9+7;
12 
13 LL ans1,ans2=1;
14 
15 LL n,m,x[maxn],y[maxn],z,num,head[maxn],tim,ans;
16 LL top,sum1[maxn],v[maxn],minn[maxn],a;
17 LL sumcol,dfn[maxn],low[maxn],sumedge,Stack[maxn],point[maxn],color[maxn];
18 bool vis[maxn];
19 
20 struct Edge{
21     int u,v,d,next;
22 }edge[maxn],edge2[maxn],edge3[maxn];
23 
24 void read(LL &now)
25 {
26     char ch=getchar(); now=0; int f=1;
27     while(ch>'9'||ch<'0') { if(ch=='-') f*=-1; ch=getchar();}
28     while(ch>='0'&&ch<='9') now=now*10+ch-'0',ch=getchar();
29     now*=f;
30 }
31 
32 void add_edge(LL u,LL v)
33 {
34     edge[++num].v=v;
35     edge[num].next=head[u]; head[u]=num;
36 }
37 
38 LL tarjan(int now)
39 {
40     dfn[now]=low[now]=++tim;
41     vis[now]=true,Stack[++top]=now;
42     for(int i=head[now];i;i=edge[i].next)
43     {
44         int t=edge[i].v;
45         if(vis[t]) low[now]=min(low[now],dfn[t]);
46         else if(!dfn[t]) tarjan(t),low[now]=min(low[now],low[t]);
47     }
48     if(low[now]==dfn[now])
49     {
50         sumcol++,color[now]=sumcol,minn[sumcol]=inf;
51         for(;;)
52         {
53             a=Stack[top--];
54             vis[a]=0;
55             color[a]=sumcol;
56             if(v[a]<minn[sumcol])
57             {
58                 minn[sumcol]=v[a];
59                 sum1[sumcol]=0;
60             }
61             if(v[a]==minn[sumcol]) sum1[sumcol]++;
62             if(a==now) break;
63         }
64         
65     }
66 }
67 
68 int main()
69 {
70     read(n);
71     for(int i=1;i<=n;i++) read(v[i]);
72     read(m);
73     for(int i=1;i<=m;i++)
74     {
75         read(x[i]);
76         read(y[i]);
77         add_edge(x[i],y[i]);
78     }
79         
80     for(int i=1;i<=n;i++)
81         if(!dfn[i]) tarjan(i);
82     for(int i=1;i<=sumcol;i++)
83     {
84         ans2*=sum1[i];
85         ans1+=minn[i];
86         ans2%=hzy;
87     }
88     printf("%lld %lld
",ans1,ans2);
89 //    cout<<ans1<<" "<<ans2;
90     return 0;
91 }
View Code
原文地址:https://www.cnblogs.com/chen74123/p/7498547.html