zoukankan      html  css  js  c++  java
  • HDU-1856 More is better

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1856

    题意:给n个朋友关系,所有间接或直接的朋友算一组,求人数最多的组的人数

    思路:裸的并查集找最大连通块,但是WA了6发才A了,最后改成1遍历到1e7找最大值

    代码:

     1 //#include<bits/stdc++.h>
     2 #include<iostream>
     3 #include<vector>
     4 #include<stack>
     5 #include<string>
     6 #include<cstdio>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<map>
    10 #include<set>
    11 #include<cmath>
    12 #include<iomanip>
    13 #define inf 0x7f7f7f7f
    14 //#define scanf scanf_s
    15 using namespace std;
    16 typedef long long ll;
    17 const ll M = ll(1e7) + 5;
    18 struct node {
    19     int fa, cnt;
    20 };
    21 node root[M];
    22 int maxn;
    23 void init() {
    24     for (int i = 0; i <= M; i++) {
    25         root[i].fa = i;
    26         root[i].cnt = 1;
    27     }
    28 }
    29 int find(int x) {
    30     if(x!=root[x].fa)
    31         root[x].fa=find(root[x].fa);
    32     return root[x].fa;
    33 }
    34 void merge(int x, int y) {
    35     x = find(x);
    36     y = find(y);
    37     if(x!=y){
    38         root[x].fa=y;
    39         root[y].cnt+=root[x].cnt;
    40     }
    41 }
    42 int maxnum;
    43 signed main()
    44 {
    45     int t; 
    46     while (~scanf("%d",&t)) {
    47         init();
    48         maxn = -1;
    49         for(int i = 0; i < t; i++) {
    50             int x, y;
    51             scanf("%d%d", &x, &y);
    52             merge(x, y);
    53             maxnum=max(maxnum,max(x,y));
    54             //output(t);
    55         }
    56         for(int i=1;i<=M;i++)
    57             if(root[i].fa==i){
    58                 maxn=max(maxn,root[i].cnt);
    59             }
    60         printf("%d
    ", maxn);
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    JS 随机整数
    微信小程序 功能函数 支付接口
    JS 正则表达式
    JS 日期 自动补齐 “2017-11-22 14:43”
    schema get_ddl
    StringBuffer 清空
    java中split任意数量的空白字符
    美国法官工资
    纪检委,检察院的工资
    国家司法机构
  • 原文地址:https://www.cnblogs.com/harutomimori/p/10884108.html
Copyright © 2011-2022 走看看