题目链接: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. hdu4770 Lights Against Dudely

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  2. hdu5534 Partial Tree

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submissi ...

  3. 2017, X Samara Regional Intercollegiate Programming Contest B.Pursuing the Happiness (string函数)

    题意:给你一个字符串,可以交换两个字符的位置,问操作后能否在字符串中找到子串\("happiness"\),如果不能,输出交换的两个位置. 题解:这题其实用string中的find ...

  4. Chapter Zero 0.2.2 内存

    目录 内存 内存的多通道设计 DRAM 和 SRAM 只读存储器(ROM) RAM.ROM以及硬盘的区别(转自百度) 内存 CPU的数据都是来自主存储器(main memory),个人计算机的主寄存器 ...

  5. Chapter Zero 0.1.1 计算机硬件五大单元

    计算机硬件的五大单元 先从CPU说起,CPU的全称如下: 中央处理器(Central Processing Unit,CPU) CPU作为一个特定功能的芯片,内含微指令集, 主机的功能差异,主要参考C ...

  6. Linux POSIX共享内存方法&ipcs &struct shmid_ds

    内容是主进程创建子进程计算斐波那契数列. 其中计算到第几项是有主进程命令行输入. 共享内存段,并且查看了一些信息. 参考操作系统概念第七版 3.10,3.11 关于LINUX C库函数 中的 fpri ...

  7. Leetcode(19)-删除链表的倒数第N个节点

    给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...

  8. HTML教程(看完这篇就够了)

    HTML教程 超文本标记语言(英语:HyperText Markup Language,简称:HTML)是一种用于创建网页的标准标记语言.您可以使用 HTML 来建立自己的 WEB 站点,HTML 运 ...

  9. Web 页面生命周期 All In One

    Web 页面生命周期 All In One Web Page LifeCycle All In One refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允 ...

  10. JavaScript interview Question - Create a Array with two papameters without using loop!

    JavaScript interview Question - Create a Array with two papameters without using loop! JavaScript - ...