zoukankan      html  css  js  c++  java
  • Gym100923H Por Costel and the Match

    题目链接:http://codeforces.com/gym/100923/problem/H

    分析:并查集,用enemy储存x的敌人,用weight储存权重决定根节点

    需用scanf和puts输入输出否则会爆

     1 #include<iostream>
     2 #include<sstream>
     3 #include<cstdio>
     4 #include<cstdlib>
     5 #include<string>
     6 #include<cstring>
     7 #include<algorithm>
     8 #include<functional>
     9 #include<iomanip>
    10 #include<numeric>
    11 #include<cmath>
    12 #include<queue>
    13 #include<vector>
    14 #include<set>
    15 #include<cctype>
    16 #define PI acos(-1.0)
    17 const int INF = 0x3f3f3f3f;
    18 const int NINF = -INF - 1;
    19 const int maxn = 1e5 + 5;
    20 typedef long long ll;
    21 using namespace std;
    22 int n, m;
    23 int fa[maxn], enemy[maxn], weight[maxn];
    24 int find(int x)
    25 {
    26     return x == fa[x] ? x : fa[x] = find(fa[x]);
    27 }
    28 void unite(int x, int y)
    29 {
    30     if (weight[x] < weight[y]) fa[x] = y;
    31     else
    32     {
    33         fa[y] = x;
    34         if (weight[x] == weight[y]) weight[x]++;
    35     }
    36 }
    37 int main()
    38 {
    39     //std::ios::sync_with_stdio(false);
    40     freopen("meciul.in", "r", stdin);
    41     freopen("meciul.out", "w", stdout);
    42     int T;
    43     scanf("%d", &T);
    44     while (T--)
    45     {
    46         scanf("%d%d", &n, &m);
    47         memset(enemy, 0, sizeof(enemy));
    48         memset(weight, 0, sizeof(weight));
    49         for (int i = 1; i <= n; ++i) fa[i] = i;
    50         while (m--)
    51         {
    52             int x, y;
    53             scanf("%d%d", &x, &y);
    54             int f1 = find(x), f2 = find(y);
    55             if (f1 == f2) puts("NO");
    56             else
    57             {
    58                 if (!enemy[x]) enemy[x] = y;
    59                 else unite(f2, find(enemy[x]));
    60                 if (!enemy[y]) enemy[y] = x;
    61                 else unite(f1, find(enemy[y]));
    62                 puts("YES");
    63             }
    64         }
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    static作用(修饰函数、局部变量、全局变量)(转)
    地弹
    开漏(open drain)和开集(open colletor)
    过冲、振铃,非单调性
    串扰(crosstalk)
    数字通信基本概念
    电源和地
    分布式系统与集总系统
    传输线及其特性阻抗
    MSP430G2553 Launchpad 硬件I2C驱动
  • 原文地址:https://www.cnblogs.com/veasky/p/11282882.html
Copyright © 2011-2022 走看看