Codeforces Goodbye 2014 problem B ACM解题报告(经典的并查集)
来源:互联网 发布:潜25淘宝哪个店是真的 编辑:程序博客网 时间:2024/05/22 05:22
这题对于大神来说简直水题,毕竟是B题,但是对于本渣渣来说确实是好题,开头暴力贪心,用了好多循环就是WA,死活过不了第15个test,后来经学长提醒用并查集秒杀之啊。
本题是某几个位置的数可以互相交换位置,交换后输出字典序最小的一组。。。
把可以互相交换的全部用并查集连通,然后把存在于同一个集合的数排序,再放回,这样就是轻松秒杀啊。
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int pre[305];int Find(int x){ int r=x; while(pre[r]!=r) r=pre[r]; int i=x,j; while(pre[i]!=r) { j=pre[i]; pre[i]=r; i=j; } return r;}void mix(int x, int y){ int fx=Find(x); int fy=Find(y); if(fy!=fx) pre[fy]=fx;}int main(){ int n; int a[305],d[305]; char b[305][305]; scanf("%d",&n); for(int i=1;i<=n;i++) pre[i]=i; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) { getchar(); for(int j=1;j<=n;j++) { scanf("%c",&b[i][j]); if(b[i][j]=='1') mix(i,j); } } for(int i=1;i<=n;i++) { int count=0; for(int j=1;j<=n;j++) { if(Find(j)==i) { d[count++]=a[j]; } } sort(d,d+count); count=0; for(int j=1;j<=n;j++) { if(Find(j)==i) a[j]=d[count++]; } } for(int i=1;i<=n;i++) { if(i!=n) printf("%d ",a[i]); else printf("%d\n",a[i]); } return 0;}
0 0
- Codeforces Goodbye 2014 problem B ACM解题报告(经典的并查集)
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph ACM解题报告(并查集)
- ***HDU-1272 小希的迷宫 ACM解题报告(并查集难题)
- HDU-1325 Is It A Tree? ACM解题报告(恶心的并查集)
- 北大acm 1611,2524解题报告--关于并查集
- Codeforces 870E 并查集 解题报告
- Pku acm 1611 The Suspects数据结构题目解题报告(九)---- 并查集的应用
- POJ1182 食物链 ACM解题报告(并查集+路径压缩难题)
- 并查集解题报告
- 【解题报告】 POJ 1182 食物链 并查集的经典应用+相对位置
- Pku acm 2492 A Bug's Life数据结构题目解题报告(十)---- 并查集的应用
- (one day one problem) codeforces 468/problem/B Two Sets (并查集)
- POJ1611 The Suspects ACM解题报告(并查集水题)
- 解题报告-HDOJ-1232(并查集)
- poj p1182解题报告(带权并查集)
- 训练题 极品飞车(并查集应用) 解题报告
- NOI 2001 食物链 解题报告 (并查集)
- POJ 2524 Ubiquitous Religions 解题报告(并查集)
- 用户体验要素
- C# Using 用法
- ViewPage and Fragment(仿微信)
- uva 10913(dp)
- 探寻Beacon技术
- Codeforces Goodbye 2014 problem B ACM解题报告(经典的并查集)
- Flex 4中 Date 与String 互相转换
- KRW韩币、IDR印度尼西亚盾 金额小100倍
- JS获取 asp.net GridView数据
- IM、视频会议、即时通讯、音视频通讯的文件传输功能
- [LeetCode] Factorial Trailing Zeroes
- 基于XMPP协议的手机多方多端即时通讯方案
- android 通过webView,简单的通过网页唤起本地界面,如果比较多的话最好不要用原生的webView和js去搭建,用框架去做
- Android学习 AsyncTask