日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PAT甲级题目翻译+答案 AcWing(并查集)

發布時間:2025/3/19 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT甲级题目翻译+答案 AcWing(并查集) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1013 Battle Over Cities (25 分)

  • 題意 :給圖,問去掉所詢問的一個點后,需要添加多少條邊可以使圖連通,N<1000N<1000N<1000
  • 思路 :并查集,將每條邊存儲,每次刪掉一個點時,相當于將與這個點相關的邊與這個點本身都不加載在圖中,每次重新建圖,查詢每次新圖的連通塊個數,加上連通塊個數-1的邊就可以使圖連通
  • 思路 :連通塊個數的計算,可以用遞減的方式計算,一開始連通塊個數等于圖上點的個數n - 1,每加入一條邊,如果這條邊的兩個端點原先不在同一個連通塊內,則連通塊個數-1,否則,continue
  • 語法 :無向圖的最大邊數M=(N?1)+(N?2)+...+1=N?(N?1)2=5?105M = (N - 1) +(N -2)+...+1=\frac{N*(N-1)}{2}=5*10^5M=(N?1)+(N?2)+...+1=2N?(N?1)?=5?105
#include <iostream> #include <vector> using namespace std;typedef long long ll; typedef pair<int, int> PII;#define endl '\n' #define pb push_back #define fi first #define se secondconst int N = 1010, M = 5e5 + 10;int fa[N]; struct Edge {int u, v; }e[M];int find(int x) {if (fa[x] != x) fa[x] = find(fa[x]);return fa[x]; }int main() {cin.tie(nullptr) -> sync_with_stdio(false);int n, m, k; cin >> n >> m >> k;for (int i = 0; i < m; i ++ ) cin >> e[i].u >> e[i].v;while (k -- ){int x; cin >> x;for (int i = 1; i <= n; i ++ ) fa[i] = i;int cnt = n - 1;for (int i = 0; i < m; i ++ ){int u = e[i].u, v = e[i].v;if (u != x && v != x){u = find(u), v = find(v);if (u != v){cnt -- ;fa[u] = v;}}}cout << cnt - 1 << endl;} }

1114 Family Property (25 分)

題意 :

  • 輸入每個人的id和父親母親(去世則輸入-1)所有孩子和房產數量和房產總面積
  • 輸出每個家庭的id(家庭成員中最小的編號),人數,人均房產數量,人均房產面積
  • 按人均房產面積降序順序輸出所有家庭信息。當存在人均房產面積相同的情況時,按 ID 升序順序排序。

思路 :

  • 先讀入每個人的信息,再根據存下來的每條邊,利用并查集,將連通塊內的人的信息合并在連通塊內編號最小的人身上
  • 然后遍歷人,找到每個連通塊內編號最小的人(也就是家主),這一步需要之前輸入信息時,記錄哪些人是連通塊內存在

語法 :

  • 自定義排序時,盡量避免用除法,將出發轉換為乘法
#include <iostream> #include <vector> #include <algorithm> using namespace std;const int N = 1e4 + 10;typedef pair<int, int> PII;int n; int hc[N], ha[N]; bool st[N]; PII e[N * 2]; int m; int pa[N]; int c[N];struct Family {int id, c, hc, ha;bool operator< (const Family &t) const{// ha / c > t.ha / t.cif (ha * t.c != t.ha * c) return ha * t.c > t.ha * c;return id < t.id;} };int find(int x) {if (pa[x] != x) pa[x] = find(pa[x]);return pa[x]; }int main() {scanf("%d", &n);int id, fa, mo, k, son;for (int i = 1; i <= n; i ++ ){scanf("%d%d%d%d", &id, &fa, &mo, &k);st[id] = true;if (fa != -1) e[m ++ ] = {id, fa}, st[fa] = true;if (mo != -1) e[m ++ ] = {id, mo}, st[mo] = true;for (int j = 1; j <= k; j ++ ){scanf("%d", &son);e[m ++ ] = {id, son};st[son] = true;}scanf("%d%d", &hc[id], &ha[id]);}for (int i = 0; i < N; i ++ ) pa[i] = i, c[i] = 1;for (int i = 0; i < m; i ++ ){int a = e[i].first, b = e[i].second;a = find(a), b = find(b);if (a != b){if (a > b) swap(a, b);hc[a] += hc[b];ha[a] += ha[b];c[a] += c[b];pa[b] = a;}}vector<Family> family;for (int i = 0; i < N; i ++ ){if (st[i] && pa[i] == i)family.push_back({i, c[i], hc[i], ha[i]});}sort(family.begin(), family.end());printf("%d\n", family.size());for (auto &f : family)printf("%04d %d %.3lf %.3lf\n", f.id, f.c, (double)f.hc / f.c, (double)f.ha / f.c);}

1118 Birds in Forest (25 分)

題意 :

  • 假設所有出現在同一張照片中的鳥都屬于同一棵樹。
  • 請你幫助科學家計算森林中樹木的最大數量,對于任何一對鳥類,請判斷它們是否在同一棵樹上。

思路 :

  • 連通塊的數量就是鳥的數量減去被合并的次數
#include <iostream> #include <cstring> #include <algorithm>using namespace std;const int N = 10010;int n; int birds[10]; int p[N]; bool st[N];int find(int x) {if (p[x] != x) p[x] = find(p[x]);return p[x]; }int main() {scanf("%d", &n);for (int i = 0; i < N; i ++ ) p[i] = i;int cnt = 0;for (int i = 0; i < n; i ++ ){int k;scanf("%d", &k);for (int j = 0; j < k; j ++ ){scanf("%d", &birds[j]);st[birds[j]] = true;}for (int j = 1; j < k; j ++ ){int a = birds[j - 1], b = birds[j];a = find(a), b = find(b);if (a != b){p[a] = b;cnt ++ ;}}}int tot = 0;for (int i = 0; i < N; i ++ ) tot += st[i];printf("%d %d\n", tot - cnt, tot);int q;scanf("%d", &q);while (q -- ){int a, b;scanf("%d%d", &a, &b);if (find(a) == find(b)) puts("Yes");else puts("No");}return 0; }

總結

以上是生活随笔為你收集整理的PAT甲级题目翻译+答案 AcWing(并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。