• hdu 5285 BestCoder Round #48 ($) 1002 种类并查集


    // M == 0 有trick... N < 2 也有trick......

      1 #include"iostream"
      2 #include"cstdio"
      3 #include"cstring"
      4 #include"map"
      5 using namespace std;
      6 int N, M;
      7 int rt[100010], w[100010];
      8 int Size[100010], Friend[100010];
      9 
     10 int n,m,l,r;
     11 char str[10];
     12 
     13 void paco(int a)
     14 {
     15     if(rt[a]==a) {
     16         return ;
     17     }
     18     paco(rt[a]);
     19     w[a] ^= w[rt[a]];
     20     rt[a] = rt[rt[a]];
     21 }
     22 
     23 bool unio(int a, int b)
     24 {
     25     paco(a);
     26     paco(b);
     27     int rt_a = rt[a],rt_b = rt[b];
     28     if(rt_a==rt_b) {
     29         return w[a] != w[b];
     30     }
     31     else {
     32         rt[rt_a] = rt_b;
     33         w[rt_a] = (w[a] == w[b]);
     34         if(w[rt_a] == 0) {
     35             Friend[rt_b] += Friend[rt_a];
     36         }
     37         else {
     38             Friend[rt_b] += (Size[rt_a] - Friend[rt_a]);
     39         }
     40         Size[rt_b] += Size[rt_a];
     41     }
     42     return true;
     43 }
     44 
     45 int main()
     46 {
     47     int i, T;
     48     scanf("%d", &T);
     49     while(T--) {
     50         memset(w, 0, sizeof(w));
     51         scanf("%d%d", &N, &M);
     52         if(N < 2) {
     53             printf("Poor wyh
    ");
     54             continue;
     55         }
     56         for(i = 0; i <= N; ++i) {
     57             rt[i] = i;
     58             Size[i] = 1;
     59             Friend[i] = 1;
     60         }
     61         int i;
     62         int u, v;
     63         bool ok = 1;
     64         for(i = 1; i <= M; ++i) {
     65             scanf("%d%d", &u, &v);
     66 //            if(w[u] == -1) {
     67 //                w[u] = 0;
     68 //            }
     69 //            if(w[v] == -1) {
     70 //                w[v] = 0;
     71 //            }
     72             if(!unio(u, v)) {
     73                 ok = 0;
     74             }
     75 //            int j;
     76 //            for(j = 1; j <= N; ++j) {
     77 //                printf("w[%d] == %d   rt[%d] == %d
    ", j, w[j], j, rt[j]);
     78 //            }
     79 //            printf("
    ");
     80         }
     81         if(ok) {
     82             int res = 0;
     83             for(i = 1; i <= N; ++i) {
     84                 if(rt[i] == i) {
     85                     res += min(Friend[i], Size[i] - Friend[i]);
     86                 }
     87             }
     88             int Max = N - res;
     89             int Min = res;
     90             if(Max < Min) {
     91                 swap(Max, Min);
     92             }
     93             if(Max == N) {
     94                 --Max;
     95                 ++Min;
     96             }
     97             printf("%d %d
    ", Max, Min);
     98         }
     99         else {
    100             printf("Poor wyh
    ");
    101         }
    102     }
    103 }
  • 相关阅读:
    并查集
    CCF201604-02
    作业八
    作业七
    数据结构--链队列基本操作
    数据结构--栈的基本操作
    eclipse配置maven
    软件架构
    scala lambda 表达式 & spark RDD函数操作
    spark(3)
  • 原文地址:https://www.cnblogs.com/AC-Phoenix/p/4657758.html
Copyright © 2020-2023  润新知