吸烟问题

来源:互联网 发布:python中文手册 chm 编辑:程序博客网 时间:2024/04/30 01:16
 ★实验任务
现在初中生高中生竟然开始抽烟了, 真是太不像话了, 不过作为一个无奸不商的烟草商,
小白菜已经瞄准这块商机无限的市场了。他知道在某个学校里有 n(5 <= n <= 50000) 个学
生, 可人家毕竟是学生, 直接询问他抽什么烟不太好, 学生们也不愿意说, 真烦躁。 不过小
白菜想到了一个办法, 就是每次抓两个人来询问是不是抽同一种烟。 每次他都会记下回答是
yes 的两个学生,一共记了 q(2 <= q <= n*(n- 1)/2) 对学生。现在小白菜捧着一大堆数据
来找你, 他不需要知道每个人抽什么烟, 只需要知道有几种烟能卖到这个学校, 你能解决这
个难题吗?
★数据输入
每组数据的开头包含两个整形 n(5 <= n <= 50000) 和 q(2 <= q <= n*(n- 1)/2), 接下
来跟着 q 行,每行为两个学生的 ID。
★数据输出
输出共有多少种的烟即可。
输入示例 输出示例
5 2                    3

1 2


 #include<stdio.h>       struct Smoke       {           long int id;       };      long int UFfind(Smoke*S,long int root)    {        long int temp,leaf;        leaf=root;        while(root!=S[root].id)        {            root=S[root].id;        }        while(leaf!=root)        {            temp=S[leaf].id;            S[leaf].id=root;            leaf=temp;        }        return root;    }       void hook_up_to(Smoke*S,long int from,long int to)     {            to=UFfind(S,to);          from=UFfind(S,from);           if(from!=to)S[from].id=to;    }    int main()       {           long int n,m,from,to;           Smoke*S;           scanf("%d%d",&n,&m);           S=new Smoke[n+1];         from=n;        while(from>0)        {            S[from].id=from--;        }         while(m--)           {               scanf("%d%d",&from,&to);               hook_up_to(S,from,to);           }            from=0;          while(n>0)        {             if(S[n].id==n)                from++;             n--;        }         printf("%d\n",from);        return 0;       }       


0 0
原创粉丝点击