BZOJ 1529: [POI2005]ska Piggy banks( 并查集 )
生活随笔
收集整理的這篇文章主要介紹了
BZOJ 1529: [POI2005]ska Piggy banks( 并查集 )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
每一連通塊砸開一個就可以拿到所有的錢, 所以用并查集求連通塊數?
-------------------------------------------------------------------
#include<bits/stdc++.h>#define rep(i, n) for(int i = 0; i < n; i++)#define clr(x, c) memset(x, c, sizeof(x))using namespace std;const int maxn = 1000009;int fa[maxn], n, ans;int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}void unite(int x, int y) {int a = find(x), b = find(y);fa[a] = b;ans -= a != b;}int main() {freopen("test.in", "r", stdin);cin >> n;ans = n;rep(i, n) fa[i] = i;rep(i, n) {int v;scanf("%d", &v);unite(--v, i);}cout << ans << "\n";return 0;}-------------------------------------------------------------------?
?
1529: [POI2005]ska Piggy banks
Time Limit:?5 Sec??Memory Limit:?64 MBSubmit:?959??Solved:?437
[Submit][Status][Discuss]
Description
Byteazar 有 N 個小豬存錢罐. 每個存錢罐只能用鑰匙打開或者砸開. Byteazar 已經把每個存錢罐的鑰匙放到了某些存錢罐里. Byteazar 現在想買一臺汽車于是要把所有的錢都取出來. 他想盡量少的打破存錢罐取出所有的錢,問最少要打破多少個存錢罐.Input
第一行一個整數 N (1 <= N <= 1.000.000) – 表示存錢罐的總數. 接下來每行一個整數,第 i+1行的整數代表第i個存錢罐的鑰匙放置的存錢罐編號.Output
一個整數表示最少打破多少個存錢罐.Sample Input
42
1
2
4
Sample Output
2
In the foregoing example piggy banks 1 and 4 have to be smashed.
HINT
Source
?
轉載于:https://www.cnblogs.com/JSZX11556/p/4670197.html
總結
以上是生活随笔為你收集整理的BZOJ 1529: [POI2005]ska Piggy banks( 并查集 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win7安装MarkdownPad2破解
- 下一篇: 使用3DMM进行人脸重建中的配准方法