题目链接: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. 【noi 2.6_666】放苹果 & 【noi 2.6_8467】鸣人的影分身(DP)

    这题其实在2.6前面的专题也有出现过,我还以为我有写,结果发现,并没有.于是就现在写了.这2题其实重复了......我就按放苹果的来说. 题意:把N个苹果放在M个盘子里,允许有的盘子空着不放,问共有多 ...

  2. 吉哥系列故事——完美队形II(马拉车算法)

    吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要求 ...

  3. hdu1011 Starship Troopers

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...

  4. LEETCODE - 1181【前后拼接】

    class Solution { public: string gethead(string str){//获取头单词 string ret = ""; int strlen = ...

  5. haut-1282 ykc想吃好吃的

    1282: ykc想吃好吃的 时间限制: 1 秒  内存限制: 128 MB提交: 706  解决: 89提交 状态 题目描述 一天,ykc在学校闲的无聊,于是决定上街买点吃的,ykc很懒,本来就不是 ...

  6. 牛客多校第九场 && ZOJ3774 The power of Fibonacci(二次剩余定理+斐波那契数列通项/循环节)题解

    题意1.1: 求\(\sum_{i=1}^n Fib^m\mod 1e9+9\),\(n\in[1, 1e9], m\in[1, 1e4]\) 思路1.1 我们首先需要知道斐波那契数列的通项是:\(F ...

  7. Gym 101128A Promotions(思维 + dfs)题解

    题意:给一有向图,如果A指向B,则A是B的上级.一直i要升职那么他的上级必须都升职.现在给你一个升职人数的区间[a, b],问你升职a人时几个人必被升职,b时几个人必升职,b时几个人没有可能被升职. ...

  8. cs实时系统之网关设计

    今天给大家讲一下client-server系统(cs)设计,基本结构 1.client 客户端,插件式开发,负责对应ui的展示 2.gateway 网关层,管理客户端通信连接,负载后端集群服务 3.s ...

  9. URL parser All In One

    URL parser All In One const url = new URL(`https://admin:1234567890@cdn.xgqfrms.xyz:8080/logo/icon.p ...

  10. Navigator.registerProtocolHandler All In One

    Navigator.registerProtocolHandler All In One Web API custom protocol URL Schemes URL Protocols https ...