zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    VS中生成时“sgen.exe”已退出,代码为 1解决办法
    配置 influxDB 鉴权及 HTTP API 写数据的方法
    InfluxDB 的UTC时间问题与简单的持续查询语句
    C# 中HttpClient的使用中同步异步问题
    Action<T> Delegate
    Task Class
    .net4.0、.net4.5、.net4.6 三者对系统的要求
    vue 组件动态 循环
    js 取得当天0点 / 23:59:59 时间
    vue中element-ui树形控件自定义节点,注意一下
  • 原文地址:https://www.cnblogs.com/AC-Phoenix/p/4657758.html
Copyright © 2011-2022 走看看