Codeforces--Books Exchange (hard version)

题目链接http://codeforces.com/contest/1249/problem/B2 。并查集思想,将数分成多个集合,每个集合的大小就是一轮的所需天数。

Map[i]存储数据。

flag[i]来表示第i个数是否被访问过。

mm[i]记录第i个集合所对应的集合大小,索引i为第i个集合的根对应的值。

getParent方法利用递归的思想更新每个节点的上继,直接指向当前集合的根。

由于访问过的集合不再进行访问,因此,分析时间复杂度,准确是O(CN),C为一个常数。在这里需要注意的是,mm在每次使用后要清,不然会有问题。

 1 #include<bits/stdc++.h>
 2
 3 /*
 4 * 并查集
 5 */
 6
 7 using namespace std;
 8
 9 static const int MAX = 200005;
10
11 int Map[MAX];
12 bool flag[MAX];
13 map<int, int> mm;
14
15 int getParent(int x){
16     if(!flag[x]){
17         flag[x] = true;
18         Map[x] = getParent(Map[x]);
19     }
20     return Map[x];
21 }
22
23 int main(){
24     int q;
25     scanf("%d", &q);
26     while(q--){
27         int n;
28         scanf("%d", &n);
29         for(int i=1;i<=n;i++){
30             flag[i] = false;
31         }
32
33         // construct set
34         int tmp;
35         for(int i=1;i<=n;i++){
36             scanf("%d", &Map[i]);
37         }
38
39         // solve
40         for(int i=1;i<=n;i++){
41             int parent = getParent(i);
42             mm[parent]++;
43         }
44
45         for(int i=1;i<=n;i++){
46             if(i==1){
47                 printf("%d", mm[Map[i]]);
48             }
49             else{
50                 printf(" %d", mm[Map[i]]);
51             }
52         }
53         printf("\n");
54         mm.clear();
55     }
56     return 0;
57 }

原文地址:https://www.cnblogs.com/sgatbl/p/11746984.html

时间: 2024-07-30 14:09:52

Codeforces--Books Exchange (hard version)的相关文章

codeforces - 1249B2 Books Exchange (hard version)

https://vjudge.net/problem/CodeForces-1249B2 题目描述: The only difference between easy and hard versions is constraints. There are nn kids, each of them is reading a unique book. At the end of any day, the ii-th kid will give his book to the pipi-th kid

codeforces#1251E2. Voting (Hard Version)(贪心)

题目链接: http://codeforces.com/contest/1251/problem/E2 题意: 主角需要获得n个人的投票 有两种方式让某个人投票 1,已经投票的人数大于m 2,花p枚硬币收买 数据范围: $1\leq n \leq 200 000$ 分析: 对$m$进行排序 保留前缀的$m$数组,$pre[x]$为$m$小于等于$x$的人数 对x逆序处理,当枚举到$x$的时候,假设$m$小于等于$x-1$的那些人已经投票,也就是有$pre[x-1]$人已经投票 如果$pre[x-

Codeforces Round #595 (Div. 3)

比赛链接:传送门 Codeforces1249A. Yet Another Dividing into Teams(水题) 代码: #include <bits/stdc++.h> #define N 105 using namespace std; int a[N]; int main() { int q; cin >> q; while (q--) { int n; cin >> n; for (int i = 1; i <= n; i++) { cin &g

cf 595 补题

1.B2   Books Exchange (hard version) 题意:有n(1~n)个孩子看书,定义一个数组,记录了每个孩子看完??,把书给的下一个人 T个样例,输出孩子重新拿到自己的书需要的传递次数 思路:easy  : i=a[ i ] ;直到 i ==本身,记录次数     hard :   递归找环 ,运用递归,减少遍历次数,就是说一次递归把环上的所有点的次数全部找到. #include<bits/stdc++.h> using namespace std; const in

codeforces #595 div3 题解

A. Yet Another Dividing into Teams Description Solution 1 #include <algorithm> 2 #include <cctype> 3 #include <cmath> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cstring> 7 #include <iostream> 8 #include

本体概述

原文地址:http://blog.csdn.net/sfbegingmail/article/details/6093010 本体的定义 Ontology的概念最初起源于哲学领域,可以追溯到公元前古希腊哲学家亚里士多德(384-322 b.c.)尝试对世界上的事物分类,在哲学中定义为"对世界上客观存在物的系统地描述,即存在论"[1].牛津英语词典定义为"存在的科学或研究".当不同的理论家提出本体的不同建议,或者不同的知识领域谈论本体建议时,应该使用本体的复数即本体论

Java - 使用 XSD 校验 XML

package com.huey.dream.utils; import java.io.File; import java.io.IOException; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import ja

[Hive - LanguageManual] Alter Table/Partition/Column

Alter Table/Partition/Column Alter Table Rename Table Alter Table Properties Alter Table Comment Add SerDe Properties Alter Table Storage Properties Additional Alter Table Statements Alter Partition Add Partitions Dynamic Partitions Rename Partition

java攻城狮之路--复习xml&amp;dom_pull编程

xml&dom_pull编程: 1.去掉欢迎弹窗界面:在window项的preferences选项中输入“configuration center” 找到这一项然后     把复选框勾去即可. 2.去掉打开Myeclipse时弹出的:Please allow Subclipse team to receive......办法: Windows-->Preferences-->General-->Startup and Shutdown-->取消Subclipse Usage