zoukankan      html  css  js  c++  java
  • hdu2860 并查集模拟

    判断条件有点坑

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstdlib>
      4 #include<cstring>
      5 #include<string>
      6 #include<queue>
      7 #include<algorithm>
      8 #include<map>
      9 #include<iomanip>
     10 #include<climits>
     11 #define INF 1e11
     12 #define MAXN 100010
     13 using namespace std;
     14 
     15 #define _min(a,b) (((a)<(b))?((a):(b)))
     16 //适用于正负整数
     17 template <class T>
     18 inline bool scan_d(T &ret) {
     19     char c; int sgn;
     20     if (c = getchar(), c == EOF) return 0; //EOF
     21     while (c != '-' && (c<'0' || c>'9')) c = getchar();
     22     sgn = (c == '-') ? -1 : 1;
     23     ret = (c == '-') ? 0 : (c - '0');
     24     while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
     25     ret *= sgn;
     26     return 1;
     27 }
     28 typedef long long LL;
     29 
     30 struct soldier{
     31     int rate, com;
     32 }s[MAXN];
     33 struct Com{
     34     LL low;
     35     int num;
     36     Com(){
     37         low = INF;
     38         num = 0; 
     39     }
     40 }c[MAXN];
     41 int fa[MAXN];
     42 int n, m, k;
     43 int x, y;
     44 string o;
     45 
     46 
     47 int find(int x)
     48 {
     49     if (fa[x]  == x) return x;
     50     return fa[x] = find(fa[x]);
     51 }
     52 
     53 void init()
     54 {
     55     for (int i = 0; i < MAXN; ++i)
     56         fa[i] = i,c[i].low = INF,c[i].num = 0;
     57 }
     58 
     59 bool merge(int a,int b)
     60 {
     61     int x = find(a);
     62     int y = find(b);
     63     if (x == y) return true;
     64     else if (x != y) {
     65         if (c[x].low > c[y].low) c[x].low = c[y].low;
     66         c[x].num += c[y].num;
     67         fa[y] = x;
     68     }
     69     return false;
     70 }
     71 int main()
     72 {
     73     //n companies, k soldiers  m orders
     74     while (cin >> n >> k >> m) {
     75         init();
     76         for (int i = 0; i < k; ++i) {
     77             scan_d(s[i].rate);
     78             scan_d(s[i].com);
     79             c[s[i].com].num++;
     80             if (c[s[i].com].low > s[i].rate)
     81             c[s[i].com].low = s[i].rate;
     82         }
     83         for (int i = 0; i < m; ++i) {
     84             cin >> o;
     85             if (o == "GT"){
     86                 scan_d(x);
     87                 if (find(x) == x && c[x].num != 0) printf("Lowest rate: %I64d.
    ",c[x].low);
     88                 else if (find(x) == x && c[x].num == 0) printf("Company %d is empty.
    ", x);
     89                 else printf("Company %d is a part of company %d.
    ",x,fa[x]);
     90             }
     91             else if (o == "MG") {
     92                 scan_d(x);  scan_d(y);
     93                 if (x != find(x) || y != find(y) || merge(x,y)) puts("Reject");
     94                 else puts("Accept");
     95             }
     96             else if (o == "AP") {
     97                 scan_d(x);  scan_d(y);
     98                 if (y != find(y)) {
     99                     puts("Reject");
    100                     continue;
    101                 }
    102                 if (c[y].low > x) c[y].low = x;
    103                 c[y].num++;
    104                 puts("Accept");
    105             }
    106         }
    107         puts("");
    108     }
    109     return 0;
    110 }
  • 相关阅读:
    树莓派学习记录
    ESP8266
    城轨列控系统
    VMware+CentOS7学习记录
    眼镜
    调试蜂鸣器驱动电路
    假期作业进度6
    假期作业进度5
    假期作业进度4
    假期作业进度3
  • 原文地址:https://www.cnblogs.com/usedrosee/p/4250613.html
Copyright © 2011-2022 走看看