Hihocoder #1185: Connectivity • Three (strong Unicom components + topological sequencing)

Source: Internet
Author: User

#1185: Connectivity • Three time limit: 10000ms single point limit: 1000ms memory limit: 256MB description

Summer Vacation arrived!! Little Hi and Little ho to experience life, came to live in the Prairie John's house. Early this morning, John because of something to go out, please small hi and small ho busy to help grazing.

John's House has a total of n meadows, each pasture has a capacity of w[i] pasture, n Meadows There is a one-way path of M.

Little Hi and Little ho needed to get cattle and sheep to pasture, and when they had eaten a pasture, they went on to other pastures. Little Hi and Little Ho drove the herd home when there were no meadows to be reached or pastures to reach.

At first little Hi and Little ho in pasture 1th, before returning home, how many cows and sheep can eat the most pasture?

As an example:

Each point in the figure represents a pasture, the upper part number indicates the number, and the lower part represents the pasture quantity W.

After eating grass in 1, small hi and small ho can choose to herd cattle flock to 2 or 3, assuming that small hi and small ho to the cattle flock to 2:

After eating the pasture 2, only to pasture 4, when 4 had not been able to reach the pasture, so little hi and small ho on the cattle flock to drive home.

If you choose from 1 to 3, you can reach 5, 6:

If you choose 5, you can only go straight home after you finish eating. If you choose 6, you can also go through 6 to 3, then to 5.

So there are 3 routes that the graph can choose:

1->2->4 total:111->3->5 total:91->3->6->3->5:total:13  

So the maximum amount of forage that can be eaten is 13.

The subject is adapted from the Usaco month Gold Group

Hint: strongly connected components

Input

Line 1th: 2 positive integers, n,m. Represents the number of points N, the number of edges M. 1≤n≤20,000, 1≤m≤100,000

Line 2nd: n positive integers, and the first integer denotes the amount of grass in the pasture of w[i]. 1≤w[i]≤100,000

3rd.. M+2 line: 2 positive integers, u,v. Indicates that there is a one-way path from u to v. 1≤u,v≤n

Output

Line 1th: An integer, the maximum amount of forage that can be eaten.

Code

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <vector>5#include <stack>6#include <queue>7#include <algorithm>8 using namespacestd;9 Const intn=2e4+5;Ten  Onevector<int>V[n]; Avector<int>Gra[n]; -stack<int>SK; - intN,m,cnt,num,ans; the intVal[n],low[n],dfn[n],fa[n],sum[n],indeg[n],dp[n]; - BOOLVis[n]; -  - //find the strong connected component + voidTarjan (intu) { -low[u]=dfn[u]=++CNT; + sk.push (u); A      for(intI=0; I<v[u].size (); i++){ at         intt=V[u][i]; -         if(!Dfn[t]) { - Tarjan (t); -low[u]=min (low[u],low[t]); -         } -         Else if(!fa[t]) low[u]=min (low[u],dfn[t]); in     } -     if(low[u]==Dfn[u]) { tonum++; +          while(!Sk.empty ()) { -             intt=sk.top (); the Sk.pop (); *fa[t]=num; $sum[num]+=Val[t];Panax Notoginseng             if(T==u) Break; -         } the     } + } A  the //topological sorting for maximum value + inttopo () { -queue<int>Q; $      for(intI=1; i<=num;i++){ $         if(indeg[i]==0) - Q.push (i); -     } the     intans=0; -      while(!Q.empty ()) {Wuyi         intk=Q.front (); the Q.pop (); -dp[k]+=Sum[k]; Wuans=Max (Dp[k],ans); -          for(intI=0; I<gra[k].size (); i++){ About             intt=Gra[k][i]; $indeg[t]--; -dp[t]=Max (dp[t],dp[k]); -             if(indeg[t]==0) - Q.push (t); A         } +     } the     returnans; - } $  the intMain () { thescanf"%d%d",&n,&m); the      for(intI=1; i<=n;i++) scanf ("%d",&val[i]); the      for(intI=1; i<=m;i++){ -         intb; inscanf"%d%d",&a,&b); the V[a].push_back (b); the     } AboutTarjan (1);//starting point is 1 the     //build a new map the      for(intI=1; i<=n;i++){ the         if(fa[i]==0)Continue; +          for(intj=0; J<v[i].size (); j + +){ -             intt=V[i][j]; the             if(fa[i]!=Fa[t])Bayi Gra[fa[i]].push_back (fa[t]); the         } the     } -     //Calculate in degrees -      for(intI=1; i<=num;i++){ the          for(intj=0; J<gra[i].size (); j + +){ the             intt=Gra[i][j]; theindeg[t]++; the         } -     } the     //use topological sorting to figure out the answer theprintf"%d\n", Topo ()); the     return 0;94}

Hihocoder #1185: Connectivity • Three (strong Unicom components + topological sequencing)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.