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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【PAT甲级 - 1013】Battle Over Cities (25分)(并查集)

發布時間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【PAT甲级 - 1013】Battle Over Cities (25分)(并查集) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting?city?1??-city?2???and?city?1??-city?3??. Then if?city?1???is occupied by the enemy, we must have 1 highway repaired, that is the highway?city?2??-city?3??.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers?N?(<1000),?M?and?K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then?M?lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to?N. Finally there is a line containing?K?numbers, which represent the cities we concern.

Output Specification:

For each of the?K?cities, output in a line the number of highways need to be repaired if that city is lost.

Sample Input:

3 2 3 1 2 1 3 1 2 3

Sample Output:

1 0 0

題目大意:

給定n個點m條邊的無向圖,有k次詢問,每次詢問一個點,問去掉和這個點相連的所有的邊以后,還需要添加幾條邊可以使得圖為一個連通圖。(n<1000)

解題報告:

這題掉坑了啊,,想當然以為m也<1000,因為這樣才符合復雜度,但是這題其實沒說m和k的復雜度,所以m最大可以到一個完全圖n*(n-1)/2,也就是5e5左右,所以需要把邊數開大一點才可以過。但是這樣復雜度就不對了呀,,看了一下數據,大數據下k<20,也就是說查詢很少,,所以預處理的話就非常不劃算,因為PAT題經常容易數據水(??),所以直接暴力就行別預處理了。。。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e6 + 5; int n,m,k,u[MAX],v[MAX],f[MAX]; int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]); } void merge(int u,int v) {int t1 = getf(u),t2 = getf(v);f[t2] = t1; } int main() {cin>>n>>m>>k;for(int i = 1; i<=m; i++) {scanf("%d%d",u+i,v+i);}while(k--) {int tar,cnt = 0;scanf("%d",&tar);for(int i = 1; i<=n; i++) f[i] = i;for(int i = 1; i<=m; i++) {if(u[i] == tar || v[i] == tar) continue;merge(u[i],v[i]); }for(int i = 1; i<=n; i++) {if(f[i] == i) cnt++;}printf("%d\n",max(cnt-2,0));}return 0 ; }

?

總結

以上是生活随笔為你收集整理的【PAT甲级 - 1013】Battle Over Cities (25分)(并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。

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