poj并查集问题-2524

来源:互联网 发布:阿里云ace下线原因 编辑:程序博客网 时间:2024/06/05 16:21
                **Ubiquitous Religions**Time Limit: 5000MS      Memory Limit: 65536KTotal Submissions: 27348        Accepted: 13438DescriptionThere are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in. You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.InputThe input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.OutputFor each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.Sample Input10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0Sample OutputCase 1: 1Case 2: 7Hint

(⊙o⊙)…!并查集问题,这类题目只要知道模版,然后稍微看下意思就可以了,

#include<iostream>#include<cstring>using namespace std;#define MAX 500#define INF 1<<30int n;typedef struct{    int arcs[MAX][MAX];} graph;graph g;int lowcost[MAX];int vis[MAX];void Init(){    for(int i=1;i<=n;i++)        for(int j=1;j<=n;j++)        g.arcs[i][j]=INF;}void prim(){    int result=0;    memset(vis,0,sizeof(vis));    int pos=1;    vis[pos]=1;    for(int i=1; i<=n; i++)    {        if(pos!=i)        {            lowcost[i]=g.arcs[pos][i];        }    }    for(int i=1; i<n; i++)    {        int min=9999999;        for(int j=1; j<=n; j++)            if(vis[j]==0&&min>lowcost[j])            {                min=lowcost[j];                pos=j;            }            //cout<<min<<endl;        vis[pos]=1;        result+=min;        for(int k=1; k<=n; k++)            if(vis[k]==0&&lowcost[k]>g.arcs[pos][k])                lowcost[k]=g.arcs[pos][k];    }    cout<<result<<endl;}int main(){    int w,num;    char va,vb;    while(cin>>n&&n)    {        Init();        for(int i=1; i<n; i++)        {            cin>>va>>num;            while(num--)            {                cin>>vb>>w;                g.arcs[(va-'A'+1)][(vb-'A'+1)]=w;                g.arcs[(vb-'A'+1)][(va-'A'+1)]=w;               // cout<<(va-'A'+1)<<" "<<g.arcs[(va-'A'+1)][(vb-'A'+1)]<<" "<<(vb-'A'+1)<<endl;                //cout<<w<<" ";            }            //cout<<endl;        }         /*for(int j=1;j<=n;j++)            {                for(int k=1;k<=n;k++)                cout<<g.arcs[j][k]<<" ";            cout<<endl;            }*/        prim();    }    return 0;}
0 0
原创粉丝点击