[Tarjan] 1827 summer holiday

Source: Internet
Author: User
Tags cmath

Question link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1827

Summer Holiday Time Limit: 10000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1721 accepted submission (s): 797


Problem descriptionto 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

I heard that lcy helped you book a 7-day tour of the new Mattel. wiskey was so happy that he could not stay at night. He wanted to tell you the news quickly, even though he had contact information for everyone, however, one contact once took too much time and telephone fees. He knows that others also have some contact information, so that he can notify others and ask others for help. Can you help wiskey calculate the minimum number of people to be notified, and the minimum number of phone bills to be paid can be notified to everyone?
 
Input Multiple groups of test arrays, ending with EOF.
The first line has two integers, N and M (1 <= n <= 1000, 1 <= m <= 2000), indicating the number of people and the contact logarithm.
There are n integers in the next line, indicating the phone charge for the person who contacted me by wiskey.
Then there are m rows. Each row has two integers x and y, indicating that X can be associated with y, but not y.
 
Output outputs the minimum number of contacts and the minimum cost.
Each case outputs a line of answers.
 
Sample Input
12 162 2 2 2 2 2 2 2 2 2 2 2 1 33 22 13 42 43 55 44 66 47 47 127 88 78 910 911 10
 
Sample output
3 6
 
Author whisky
Sourcehdoj 2007 Summer exercise (3)-Hold by wiskey
Recommend whisky | we have carefully selected several similar problems for you: 1823 1824 1826 1269 1822
Statistic | submit | discuss | note Question meaning:

I will not explain the Chinese questions.

Solution:

Tarjan requires a strong connected component, and then finds a strong connected component with a 0 degree of access, and obtains the result of the least calls they have spent.

Code:

//#include<CSpreadSheet.h>#include<iostream>#include<cmath>#include<cstdio>#include<sstream>#include<cstdlib>#include<string>#include<string.h>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<set>#include<stack>#include<list>#include<queue>#include<ctime>#include<bitset>#include<cmath>#define eps 1e-6#define INF 0x3f3f3f3f#define PI acos(-1.0)#define ll __int64#define LL long long#define lson l,m,(rt<<1)#define rson m+1,r,(rt<<1)|1#define M 1000000007//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define Maxn 1100int low[Maxn],dfn[Maxn],sta[Maxn],dep,bc,sc,n,m;int sa[Maxn],in[Maxn],dei[Maxn];bool iss[Maxn];vector<vector<int> >myv;vector<vector<int> >bb;void tarjan(int cur){    int ne;    low[cur]=dfn[cur]=++dep;    sta[++sc]=cur;    iss[cur]=true;    for(int i=0;i<myv[cur].size();i++)    {        ne=myv[cur][i];        if(!dfn[ne])        {            tarjan(ne);            if(low[ne]<low[cur])                low[cur]=low[ne];        }        else if(iss[ne]&&dfn[ne]<low[cur])            low[cur]=dfn[ne];    }    if(low[cur]==dfn[cur])    {        ++bc;        do        {            ne=sta[sc--];            iss[ne]=false;            in[ne]=bc;            bb[bc].push_back(sa[ne]);        }while(ne!=cur);    }}void solve(){    sc=bc=dep=0;    bb.clear();    bb.resize(n+1);    memset(iss,false,sizeof(iss));    memset(dfn,0,sizeof(dfn));    for(int i=1;i<=n;i++)        if(!dfn[i])            tarjan(i);}int main(){    //freopen("in.txt","r",stdin);   //freopen("out.txt","w",stdout);   while(~scanf("%d%d",&n,&m))   {       myv.clear();       myv.resize(n+1);       for(int i=1;i<=n;i++)            scanf("%d",&sa[i]);       for(int i=1;i<=m;i++)       {           int a,b;           scanf("%d%d",&a,&b);           myv[a].push_back(b);       }       solve();       memset(dei,0,sizeof(dei));       for(int i=1;i<=n;i++)       {           for(int j=0;j<myv[i].size();j++)           {               int ne=myv[i][j];               if(in[ne]!=in[i])                   dei[in[ne]]++;           }       }       int nu=0,ans=0;       for(int i=1;i<=bc;i++)       {           if(!dei[i])           {               sort(bb[i].begin(),bb[i].end());               ans+=bb[i][0];               nu++;           }       }       printf("%d %d\n",nu,ans);   }    return 0;}


[Tarjan] 1827 summer holiday

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.