[2016-03-15] [HDU] [1213] [How many Tables]

Source: Internet
Author: User

    • Time: 2016-03-15-16:19:15 Tuesday
    • Title Number: [2016-03-15][hdu][1213][how many Tables]
    • Ask a friend to eat, each friend does not like and do not know the person in a table, give the understanding of the relationship, asked at least how many tables
    • Input:
      • Number of T groups
      • Each set of data
      • Nm
      • M line U V for u and v recognition
    • Output:
      • At least
    • Analysis: And look up sets, find the number of sets
  
 
  1. #ifdef _WORK_
  2. #include <vector>
  3. #include <list>
  4. #include <map>
  5. #include <set>
  6. #include <deque>
  7. #include <queue>
  8. #include <stack>
  9. #include <bitset>
  10. #include <algorithm>
  11. #include <functional>
  12. #include <numeric>
  13. #include <utility>
  14. #include <sstream>
  15. #include <iostream>
  16. #include <iomanip>
  17. #include <cstdio>
  18. #include <cmath>
  19. #include <cstdlib>
  20. #include <cctype>
  21. #include <string>
  22. #include <cstring>
  23. #include <cstdio>
  24. #include <cmath>
  25. #include <cstdlib>
  26. #include <ctime>
  27. using namespace std;
  28. typedef long long LL;
  29. #define CLR ( Span class= "PLN" >x y memset x y Span class= "pun"), sizeof x Span class= "pun")))
  30. #define for ( Span class= "PLN" >x y z for ( int ( x y x Span class= "pun") < ( z x
  31. #define FORD ( Span class= "PLN" >x y z for ( int ( x y x Span class= "pun") >= ( z x
  32. const int maxn = 1000 + 100;
  33. int fa[maxn],mrank[maxn];
  34. void ini(int n){
  35. memset(mrank,0,sizeof(mrank));
  36. for(int i = 0; i <= n;++i){
  37. fa[i] = i;
  38. }
  39. }
  40. int fnd(int x){
  41. return x ==fa[x]?x:fa[x] = fnd(fa[x]);
  42. }
  43. void uni(int x,int y){
  44. x = fnd(x);y = fnd(y);
  45. if(x == y) return ;
  46. if(mrank[x] < mrank[y]){
  47. fa[x] = y;
  48. }else {
  49. fa[y] = x;
  50. if(mrank[x] == mrank[y]) ++mrank[x];
  51. }
  52. }
  53. int main(){
  54. //freopen("in.txt","r",stdin);
  55. //freopen("out.txt","w",stdout);
  56. int t;
  57. scanf("%d",&t);
  58. while(t--){
  59. int n,m;
  60. scanf("%d%d",&n,&m);
  61. ini(n);
  62. FOR(i,0,m){
  63. int u,v;
  64. scanf("%d%d",&u,&v);
  65. uni(u,v);
  66. }
  67. int ans = 0;
  68. FOR(i,1,n + 1){ if(fa[i] == i)
  69. ++ans;
  70. }
  71. printf("%d\n",ans);
  72. }
  73. return 0;
  74. }
  75. #endif


From for notes (Wiz)

[2016-03-15] [HDU] [1213] [How many Tables]

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.