HDU ACM 1827 Summer holiday-> Strong connected component + Pinch point (Tarjan algorithm)

Source: Internet
Author: User

Analysis: First, the number of strong connected components, then the contraction point, and finally find the final answer.

1. Use Tarjan algorithm to find the number of strong connected components.

2, the contraction point for another graph, through the Tarjan algorithm to find the results. The degree of each point is calculated in the graph after the indent.

3, to find out the minimum cost of each strong connected component.

4. The final result is obtained according to the dimension of the graph after the contraction point.


#include <iostream> #include <vector> #include <stack>using namespace std;vector<int> map[1002 ];stack<int> tarjan_stack;int low[1002];int dfn[1002];bool vis[1002];int belong[1002];int in[1002];int cost[ 1002];int mincost[1002];int cnt,pos;void Init (int n) {int i;cnt=pos=0;memset (low,0,sizeof (Low)); Memset (Dfn,0,sizeof ( DFN) memset (vis,0,sizeof (Vis)); Memset (belong,0,sizeof (belong)); Memset (In,0,sizeof (in)); for (i=1;i<=n;i++) Map[i].clear (); for (i=1;i<=n;i++) Mincost[i]=0x7fffffff;while (!tarjan_stack.empty ()) Tarjan_stack.pop ();} void Input (int n,int m) {int i,a,b;for (i=1;i<=n;i++) scanf ("%d", &cost[i]), for (i=1;i<=m;i++) {scanf ("%d%d", &AMP;A,&AMP;B); Map[a].push_back (b);}} void Tarjan (int u) {int I,t;dfn[u]=low[u]=++pos;vis[u]=true;tarjan_stack.push (u); for (I=0;i<map[u].size (); i++) {T =map[u][i];if (!dfn[t]) {Tarjan (t); if (Low[t]<low[u]) low[u]=low[t];} else if (Vis[t] && low[u]>dfn[t]) low[u]=dfn[t];} if (Low[u]==dfn[u]) {cnt++;while (!tarjan_stack.empty ()) {   T=tarjan_stack.top (); Tarjan_stack.pop (); vis[t]=false;belong[t]=cnt;if (t==u) Break;}}} void Solve (int n,int m) {int i,u,j,t;int number,total;init (n); Input (n,m); for (i=1;i<=n;i++)//Cycle prevention is a forest condition can also handle if (!DFN [i]) Tarjan (i); for (u=1;u<=n;u++) for (J=0;j<map[u].size (); j + +)//indent + the degree to which each point of the graph after the indent is counted. {t=map[u][j];if (belong[u]!=belong[t]) in[belong[t]]++;} for (i=1;i<=n;i++) mincost[belong[i]]=mincost[belong[i]]<cost[i]?mincost[belong[i]]:cost[i]; Count the minimum cost number=total=0;for (i=1;i<=cnt;i++)//statistic minimum number of people to be notified in each strong connected component and the cost (the point at which the graph after the indent point is 0) if (in[i]==0) {number++; Total+=mincost[i];} printf ("%d%d\n", number,total);} int main () {int N,m;while (cin>>n>>m) {Solve (n,m);} return 0;}


HDU ACM 1827 Summer holiday-> Strong connected component + Pinch point (Tarjan algorithm)

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.