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 }
Gym100923H Por Costel and the Match的更多相关文章
- 【并查集】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 ...
- 【带权并查集】Gym - 100923H - Por Costel and the Match
裸题. 看之前的模版讲解吧,这里不再赘述了. #include<cstdio> #include<cstring> using namespace std; int fa[10 ...
- Gym-100923H-Por Costel and the Match(带权并查集)
链接: https://vjudge.net/problem/Gym-100923H 题意: Oberyn Martell and Gregor Clegane are dueling in a tr ...
- 【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 ...
- 【找规律】Gym - 100923L - Por Costel and the Semipalindromes
semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...
- 【分块打表】Gym - 100923K - Por Costel and the Firecracker
semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...
- 【数形结合】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 ...
- 【动态规划】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 ...
- 【Gym - 100923A】Por Costel and Azerah(思维水题)
Por Costel and Azerah Descriptions 给你n个数 问你,有多少个子序列 的和是偶数 Example Input 233 10 124 2 Output 33 题目链接 ...
随机推荐
- 【noi 2.6_1481】Maximum sum(DP)
题意:求不重叠的2段连续和的最大值. 状态定义f[i]为必选a[i]的最大连续和,mxu[i],mxv[i]分别为前缀和后缀的最大连续和. 注意:初始化f[]为0,而max值为-INF.要看好数据范围 ...
- Educational Codeforces Round 89 (Rated for Div. 2) C Palindromic Paths
题目链接:Palindromic Paths 题意: 给你一个n行m列的矩阵,这个矩阵被0或者1所填充,你需要从点(1,1)走到点(n,m).这个时候会有很多路径,每一条路径对应一个01串,你可以改变 ...
- Slim Span POJ 3522 (最小差值生成树)
题意: 最小生成树找出来最小的边权值总和使得n个顶点都连在一起.那么这找出来的边权值中的最大权值和最小权值之差就是本题的结果 但是题目要求让这个输出的结果最小,也就是差值最小.那么这就不是最小生成树了 ...
- Linux-平均负载指数
目录 系统平均负载 什么是平均负载 平均负载多少合理 如何观察平均负载 平均负载和CPU的使用率的区别 平均负载分析 执行CPU密集型任务 执行I/O密集型任务 大量进程调度 关于平均负载的总结 系统 ...
- hdu2333-贪心,如何去后效性,背包太大怎么办,如何最大化最小值,从无序序列中发掘有序性质
补充一下我理解的中文题意.. 你要重新组装电脑..电脑有一些部件..你的预算有b,b(1~1e9),有n个部件..每个部件有类型和名称以及价钱和质量现在你要在不超过预算b的情况下..每个类型都买一个部 ...
- os-hackNOS-2(wp5.3本地文件包含 rbash绕过)
一.信息收集 直接netdiscover,找到IP是 192.168.56.101 然后端口扫描一波 只打开了22和80端口,访问一下80端口,是apache首页,那就继续查目录赛.,发现了一个tsw ...
- 使用 js 实现十大排序算法: 堆排序
使用 js 实现十大排序算法: 堆排序 堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法. 大顶堆:每个节点的值都大于或等于其子节点的值,在堆排序算法中用于升序排列: 小顶堆:每个 ...
- yarn create & npx & npm init
yarn create & npx & npm init https://www.npmtrends.com/npm-vs-npx-vs-yarn demo https://www.n ...
- Nestjs mongodb
nestjs 文档 mongoose 文档 使用"@meanie/mongoose-to-json"转换查询后返回的json数据 将"_id"转为"i ...
- Flutter DraggableScrollableSheet 可滚动对象的容器
文档 Example import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp ex ...