题目链接: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 }

Gym100923H Por Costel and the Match的更多相关文章

  1. 【并查集】Gym - 100923H - Por Costel and the Match

    meciul.in / meciul.out Oberyn Martell and Gregor Clegane are dueling in a trial by combat. The fight ...

  2. 【带权并查集】Gym - 100923H - Por Costel and the Match

    裸题. 看之前的模版讲解吧,这里不再赘述了. #include<cstdio> #include<cstring> using namespace std; int fa[10 ...

  3. Gym-100923H-Por Costel and the Match(带权并查集)

    链接: https://vjudge.net/problem/Gym-100923H 题意: Oberyn Martell and Gregor Clegane are dueling in a tr ...

  4. 【Heap-dijkstra】Gym - 100923B - Por Costel and the Algorithm

    algoritm.in / algoritm.out Even though he isn't a student of computer science, Por Costel the pig ha ...

  5. 【找规律】Gym - 100923L - Por Costel and the Semipalindromes

    semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...

  6. 【分块打表】Gym - 100923K - Por Costel and the Firecracker

    semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...

  7. 【数形结合】Gym - 100923I - Por Costel and the Pairs

    perechi3.in / perechi3.out We don't know how Por Costel the pig arrived at FMI's dance party. All we ...

  8. 【动态规划】Gym - 100923A - Por Costel and Azerah

    azerah.in / azerah.out Por Costel the Pig has received a royal invitation to the palace of the Egg-E ...

  9. 【Gym - 100923A】Por Costel and Azerah(思维水题)

    Por Costel and Azerah Descriptions 给你n个数 问你,有多少个子序列 的和是偶数 Example Input 233 10 124 2 Output 33 题目链接 ...

随机推荐

  1. A. Vitaly and Strings

    Vitaly is a diligent student who never missed a lesson in his five years of studying in the universi ...

  2. Codeforces Round #550 (Div. 3) D. Equalize Them All (贪心,模拟)

    题意:有一组数,可以选择某个数\(a_i\)相邻的一个数\(a_j\),然后可以让\(a_i\)加上或者减去\(|a_i-a_j|\),问最少操作多少次使得数组中所有数相同. 题解:不难发现,每次操作 ...

  3. Milk Patterns POJ - 3261 后缀数组

    Farmer John has noticed that the quality of milk given by his cows varies from day to day. On furthe ...

  4. 国产网络损伤仪 SandStorm -- 只需要拖拽就能删除链路规则

    国产网络损伤仪SandStorm可以模拟出带宽限制.时延.时延抖动.丢包.乱序.重复报文.误码.拥塞等网络状况,在实验室条件下准确可靠地测试出网络应用在真实网络环境中的性能,以帮助应用程序在上线部署前 ...

  5. 洛谷p2216 多次单调队列,扫描矩阵中的最大值减去最小值最的固定大小子矩阵

    #include <iostream> #include <cstdio> #include <cstring> using namespace std; int ...

  6. Linux Centos7发送QQ邮件

    一.关闭本机的sendmail服务或者postfix服务 #sendmial service sendmail stop chkconfig sendmail off #postfix service ...

  7. js中当for循环中有事件要使用循环变量时,变量用var声明和let声明的区别

    var 声明一个全局变量,声明的变量会变量提升: let 声明一个局部变量: 当页面加载完后,for循环也结束了,如果用var声明的变量此时也随着for循环的结束而自增到满足结束循环的条件, 此时调用 ...

  8. vue component :is

    vue component :is Vue <component> element https://vuejs.org/v2/guide/components.html#Dynamic-C ...

  9. HTML a Tag All In One

    HTML a Tag All In One HTML <a> target https://developer.mozilla.org/en-US/docs/Web/HTML/Elemen ...

  10. The Weekly Web Dev Challenge: String Calculator

    The Weekly Web Dev Challenge: String Calculator https://twitter.com/intent/tweet?text=I just complet ...