swjtuoj2433 Magic Mirror
Magic Mirror is an artificial intelligence system developed by TAL AI LAB,It can determine human based on human bone information.
Meternal use the magic mirror API,then he found after he input some photo to the system,it will output the two endpoint features of each bone of the human body.
a bone can be described by a integer pair like (1,2) represent this bone consists of endpoint 1 and endpoint 2 .
We can think that everyone’s bones endpoint features are unique.
But when using the magic mirror's API, Meternal broke all the order, and now you need to help him determine the endpoint features that each person has.
The system may contain some unused information which never included in any bone,please skip them.
The data contains n types of endpoints and m bones which described by a pair.
The first line contains two integer n(1≤n≤200000) and m(1≤m≤200000) ,the number of endpoint and bones.
The following m lines,
each line contains two integer a(1≤a≤n) and b(1≤b≤n),the features of two endpoint of this bone.
The output contains some lines of bone endpoint information.
Please output the information of each person in according order by the smallest endpoint number of this person.
For the i-th line information first outputs the number k, the endpoint contained by this person.
then outputs k endpoint number in ascending order.
When the output is complete, output a line "Finish".
please skip unused endpoint.
6 4
1 3
2 5
3 3
3 4
3 1 3 4
2 2 5
Finish
==========================================================================================================
思路:本题考查查并集的基本操作,输出时要求一个集合一个集合输出,集合内的输出顺序为从小到大,集合间的顺序以集合最小元素按升序输出。
可选用vector来存储一个集合,将元素从小到大遍历,每找到一个未输出的集合,便找到相应的vector输出里面的全部元素。最后一行输出Finish。
注意点:
- 直接暴力搜索每一个元素会超时,将每个集合放在一起可避免无效搜索。
- 两个根节点相同时合并时,会出现错误,不要忘记在合并前判定其根节点是否相等。
1 #include<stdio.h>
2 #include<vector>
3 using namespace std;
4
5 const int Maxn = 200005;
6 int set[Maxn];
7 vector<int> ans[Maxn];
8 int sign[Maxn];
9 void init(){
10 for(int i = 0; i < Maxn; i++){
11 set[i] = -1;
12 }
13
14 }
15
16 int Find(int d1){
17 if(set[d1] < 0){
18 return d1;
19 }
20 set[d1] = Find(set[d1]); //路径压缩
21 return set[d1];
22 }
23
24 void Union(int d1,int d2){
25 int root1 = Find(d1);
26 int root2 = Find(d2);
27 if(root1==root2){
28 return;
29 }
30 if(set[root1] > set[root2]){ //按秩归并
31 set[root2] += set[root1];
32 set[root1] = root2;
33 }else{
34 set[root1] += set[root2];
35 set[root2] = root1;
36 }
37 return ;
38 }
39
40
41 int main(){
42 init();
43 int n,m;
44 scanf("%d %d",&n,&m);
45 while(m--){
46 int d1,d2;
47 scanf("%d %d",&d1,&d2);
48 Union(d1,d2);
49 }
50
51 for(int i = 1; i < Maxn; i++){
52 ans[Find(i)].push_back(i);
53
54 }
55 for(int i = 1; i < Maxn ; i++){
56 if(sign[Find(i)] == 1||set[Find(i)] > -2){
57 continue;
58 }
59 sign[Find(i)] = 1;
60 printf("%d", -set[Find(i)]);
61 for(int j = 0; j < ans[Find(i)].size();j++){
62 printf(" %d",ans[Find(i)][j]);
63 }
64 printf("\n");
65 }
66 printf("Finish");
67 }
swjtuoj2433 Magic Mirror的更多相关文章
- 2018ACM/ICPC 焦作网络预选赛-A Magic Mirror
Jessie has a magic mirror. Every morning she will ask the mirror: 'Mirror mirror tell me, who is the ...
- ACM-ICPC 2018 焦作赛区网络预赛 A Magic Mirror(签到)
https://nanti.jisuanke.com/t/31710 题意 若输入的是Jessie或jessie,输出Good guy!,否则输出Dare you say that again? 分析 ...
- 树莓派:使用OpenCV调用自带的摄像头.
总所周知,树莓派上,调用摄像头的指令有raspistill和raspivid.若要使用opencv对摄像头进行调用,不少人会出现 cvCaptureFromCAM(0)函数无法找到Pi Cam的错误情 ...
- Analysis Guidelines
This section describes some best practices for analysis. These practices come from experience of ana ...
- 2018 ACM 网络选拔赛 焦作赛区
A. Magic Mirror #include <cstdio> #include <cstdlib> #include <cmath> #include < ...
- Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题
A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...
- ACM-ICPC 2018 焦作赛区网络预赛 Solution
A. Magic Mirror 水. #include <bits/stdc++.h> using namespace std; int t; ]; inline bool work() ...
- Codeforces Round #359 (Div. 2) A
A. Free Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- ACM-ICPC 2018 焦作赛区网络预赛
这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...
随机推荐
- Linux系统编程 —线程属性
在之前的章节中,我们在调用pthread_create函数创建线程时,第二个参数(即线程属性)都是设为NULL,即使用默认属性.一般情况下,使用默认属性已经可以解决我们开发过程中的大多数问题. 但是, ...
- tensorflow(一):基础
一.张量 1.张量的概念 在TensorFlow中,所有的数据都通过张量的形式来表示.从功能的角度,张量可以简单理解为多维数组,零阶张量表示标量(scalar),也就是一个数:一阶张量为向量(vect ...
- 引用类型之Object
引用类型 引用类的值(对象)是引用类型的一个实例.在ECMAScript中,引用类型是一种数据结构,用于将数据和功能组织在一起. 对象是某个特定引用类型的实例.新对象是使用new操作符后跟一个构造函数 ...
- linux centos7使用docker安装elasticsearch,并且用Django连接使用
一:elasticsearch安装及配置 1:需求分析 当用户在搜索框输入关键字后,我们要为用户提供相关的搜索结果.这种需求依赖数据库的模糊查询like关键字可以实现,但是like关键字的效率极低,而 ...
- A4988两相四线步进电机驱动模块使用经验
1.A4988模块可以驱动两相四线步进电机,模块引脚及接线图如下: 2.步进电机引线如下: 3.引脚: ENABLE:低电平有效,用于打开和关闭场效应管的输出: RESET:低电平有效,芯片复位: S ...
- 玩转控件:GDI+动态绘制流程图
前言 今天,要跟大家一起分享是"GDI+动态生成流程图"的功能.别看名字高大上(也就那样儿--!),其实就是动态生成控件,然后GDI+绘制直线连接控件罢了.实际项目效果图如下 ...
- 多测师讲解常用的测试工具分为10类_高级讲师肖sir
我们将常用的测试工具分为10类. 1. 测试管理工具 2. 接口测试工具 3. 性能测试工具 4. C/S自动化工具 5.白盒测试工具 6.代码扫描工具 7.持续集成工具 8.网络测试工具 9.app ...
- Android开发还不会这些?如何面试拿高薪!
我所接触的Android开发者,百分之九十五以上 都遇到了以下几点致命弱点! 如果这些问题也是阻止你升职加薪,跳槽大厂的阻碍. 那么我确信可以帮你突破瓶颈! 群内有许多来自一线的技术大牛,也有在小厂或 ...
- win32获取进程树,以及命令行参数
1.先上代码 package main import ( "bytes" "errors" "flag" "fmt" & ...
- 【Luogu】P4381 [IOI2008]Island
一.题目 Description 你将要游览一个有N个岛屿的公园.从每一个岛i出发,只建造一座桥.桥的长度以Li表示.公园内总共有N座桥.尽管每座桥由一个岛连到另一个岛,但每座桥均可以双向行走.同时, ...