SPOJ IAPCR2F 【并查集】
思路:
利用并查集/DFS都可以处理连通问题。
PS:注意Find()查找值和pre[]值的区别。
#include<bits/stdc++.h>
using namespace std; const int N=1e3+10;
int val[N];
int pre[N],n,m;
vector<int>xs,ans; 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;
} int main()
{
int T,cas=1,x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&val[i]);
pre[i]=i;
}
while(m--)
{
scanf("%d%d",&x,&y);
x=Find(x);
y=Find(y);
if(x!=y)
pre[x]=y;
}
xs.clear();
ans.clear();
for(int i=1;i<=n;i++)
if(pre[i]==i) xs.push_back(i);
int sz=xs.size();
for(int i=0;i<sz;i++)
{
int sum=0;
for(int j=1;j<=n;j++)
if(Find(j)==xs[i])
sum+=val[j];
ans.push_back(sum);
}
sort(ans.begin(),ans.end());
sz=ans.size();
printf("Case %d: %d\n",cas++,sz);
for(int i=0;i<sz;i++)
{
if(i) printf(" ");
printf("%d",ans[i]);
}
puts("");
}
return 0;
}
SPOJ IAPCR2F 【并查集】的更多相关文章
- SPOJ LEXSTR 并查集
题目描述: Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using ...
- SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集
[题目分析] 区间开方+区间求和. 由于区间开方次数较少,直接并查集维护下一个不是1的数的位置,然后暴力修改,树状数组求和即可. 这不是BZOJ上上帝造题7分钟嘛 [代码] #include < ...
- SPOJ:Lexicographically Smallest(并查集&排序)
Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...
- SPOJ:Lost and survived(multiset+并查集)
On September 22, 2004, Oceanic Flight 815 crashed on a mysterious island somewhere in the pacific. T ...
- Mutual Training for Wannafly Union #6 E - Summer Trip(并查集)
题目链接:http://www.spoj.com/problems/IAPCR2F/en/ 题目大意: 给m个数字代表的大小,之后n组数据,两两关联,关联后的所有数字为一组,从小到大输出组数以及对应的 ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
随机推荐
- MLGBZ
April cloudy, boss rainy, told me he want to kick But coming so,Formosa Heart sad , Down,down,down W ...
- matlab 在机器视觉中常用的函数
~ triangulate() 三角化(获得距离)匹配点 ~ undistortImage() 去除相机畸变并生成图像
- HTML5/CSS3淡入淡出滑块焦点图
在线演示 本地下载
- HDU4825 Xor Sum —— Trie树
题目链接:https://vjudge.net/problem/HDU-4825 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- LightOJ - 1395 A Dangerous Maze (II) —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1395 1395 - A Dangerous Maze (II) PDF (English) Statistic ...
- c语言学习的第12天
#include <stdio.h> int main(void) { int *p; int i=5; char ch='A'; p=&i; *p=99; printf(&quo ...
- Codeforces 872C Maximum splitting:数学【分解成合数之和】
题目链接:http://codeforces.com/contest/872/problem/C 题意: 给你一个数n,问你最多能将n分解成多少个合数之和.(若不能分解,输出-1) 题解: 若要让合数 ...
- HihoCoder1651 : 小球染色([Offer收割]编程练习赛38)(DP的优化)
描述 小Ho面前有N个小球排成了一排.每个小球可以被染成M种颜色之一. 为了增强视觉效果,小Ho希望不存在连续K个或者K个以上的小球颜色一样. 你能帮小Ho计算出一共有多少种不同的染色方法么? 例如N ...
- The current .NET SDK does not support targeting .NET Core 2.2
The current .NET SDK does not support targeting .NET Core 2.2 1. 奇怪的错误 最近遇到了一件奇怪的事, The current .NET ...
- POJ1511(最短路大数据处理)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 23357 Accepted: 767 ...