HDU5923-Prediction-有继承味道的并查集
目錄
- 目錄
- 思路:
(有任何問題歡迎留言或私聊 && 歡迎交流討論哦
目錄
題意:傳送門
?原題目描述在最下面。
?有一個n個節(jié)點m條邊的無向圖和一個m個節(jié)點的有根樹(根為1)。樹上每個節(jié)點和圖中的某些邊一一對應(yīng)。
?每次詢問給一個樹的點的集合S,真實完整的點集合不僅包含集合里面的點,還包含這些點在樹上的祖先。這個完整的點集合對應(yīng)了圖中一些邊集合。輸出這個圖的邊集合的聯(lián)通塊的個數(shù)。
思路:
判斷聯(lián)通用并查集很方便咯。
因為詢問帶有一點繼承的味道。因為每個點的父節(jié)點也算在其中了。
所以針對此題要用一個可繼承的并查集:對樹上每個節(jié)點維護一個圖連通性的并查集。然后每個子節(jié)點\(v\)繼承其父節(jié)點\(u\)的并查集,并在此基礎(chǔ)上將新的一條邊\(u->v\)添加進\(v\)節(jié)點的并查集。加邊就是并查集的合并操作。
對于每個詢問,就把點集中每個點的并查集合并。意思就是把每個點的聯(lián)通塊合并在一起。最后數(shù)聯(lián)通塊的個數(shù)。
代碼中還有一點注釋。
AC代碼:
#include <bits/stdc++.h> #define mme(a,b) memset((a),(b),sizeof((a))) #define fuck(x) cout<<"* "<<x<<"\n" #define iis std::ios::sync_with_stdio(false) using namespace std; int n, m, q; vector<int> son[10005]; int mp[10005][2], fa[10005][505], vis[505]; int Fi(int id,int x){return fa[id][x]==x?x:fa[id][x]=Fi(id,fa[id][x]); } void un(int id,int a,int b){int pa=Fi(id,a),pb=Fi(id,b);if(pa==pb)return;fa[id][pb]=pa; } void dfs(int u,int Fa){//每個節(jié)點繼承其父節(jié)點的并查集for(int i=1;i<=n;++i)fa[u][i]=fa[Fa][i];un(u,mp[u][0],mp[u][1]);int len=son[u].size();for(int i=0;i<len;++i){dfs(son[u][i],u);} } int main(){ #ifndef ONLINE_JUDGEfreopen("E://ADpan//in.in", "r", stdin);//freopen("E://ADpan//out.out", "w", stdout); #endifint tim,tc = 0;scanf("%d", &tim);while(tim--){scanf("%d%d",&n,&m);for(int i=0;i<=m;++i)son[i].clear();for(int i=2,x;i<=m;++i){scanf("%d",&x);son[x].push_back(i);}for(int i=1,u,v;i<=m;++i){scanf("%d%d",&u,&v);mp[i][0]=u;mp[i][1]=v;}for(int i=0;i<=n;++i)fa[0][i]=i;dfs(1,0);printf("Case #%d:\n", ++tc);scanf("%d",&q);while(q--){int cnt;scanf("%d",&cnt);for(int i=1;i<=n;++i)fa[0][i]=i,vis[i]=0;//將cnt個節(jié)點及其父節(jié)點的并查集合并for(int i=0,x;i<cnt;++i){scanf("%d",&x);for(int j=1;j<=n;++j){int tfa = Fi(x,j);//這個處理是精髓if(tfa!=j){//表示在此子圖中這兩個點聯(lián)通,故合并un(0,tfa,j);}}}int ans=0;for(int i=1;i<=n;++i){int tmp = Fi(0,i);if(vis[tmp]==0){vis[tmp]=1;ans++;}}printf("%d\n", ans);}}return 0; }原題目描述:
Problem Description
There is a graph G=?VG,EG? with |VG|=n and |EG|=m, and a magic tree T=?VT,ET?) rooted at 1, which contains m vertices.
Each vertex of the magic tree corresponds to an edge in the original graph G and each edge occurs in the magic tree exactly once.
Each query includes a set S(S?VT), and you should tell Mr. Frog the number of components in the modified graph G‘=(VG,E‘G), where E‘G is a set of edges in which every edge corresponds to a vertex v in magic tree T satisfying at least one of the following two conditions:
?v∈S.
?v is an ancestor of some vertices in S.
Note that the queries are independent, and namely one query will not influence another.
Input
The input contains several test cases and the first line of the input data is an integer T, denoting the number of test cases.
For each test case, the first line contains two integers n and m(1≤n≤500,1≤m≤10000), where n is the number of vertices and m is the number of edges.
The second line contains m - 1 integers describing the magic tree, i-th integer represents the parent of the (i + 1)-th vertex.
Then the following m lines describe the edges of the graph G. Each line contains two integers u and v indicating the two ends of the edge.
The next line contains only one integer q(1≤q≤50000), indicating the number of queries.
Then the following q lines represent queries, i-th line represents the i-th query, which contains an integer ki followed by ki integers representing the set Si.
It is guarenteed that ∑qi=1ki≤300000.
Output
For each case, print a line "Case #x:", where x is the case number (starting from 1).
For each query, output a single line containing only one integer representing the answer, namely the number of components.
Sample Input
1
5 4
1 1 3
1 2
2 3
3 4
4 5
3
1 2
2 2 3
2 2 4
Sample Output
Case #1:
3
2
1
Hint
magic tree and the original graph in the sample are:
In the first query, S = {2} and the modified graph G' = {{1, 2, 3, 4}, {(1, 2), (2, 3)}}, thus the number of the components in the modified graph is 3.
In the second query, S = {1, 2, 3}, where 1 is the ancestor of 2 (and 3) in the magic tree, and the modified graph G'' = {{1, 2, 3,4}, {(1, 2), (2, 3), (3, 4)}},
therefore the number of the components in the modified graph is 2.
In the third query, S = {1, 2, 3, 4}, where 1 is the ancestor of 2 (and 4), 3 is the ancestor of 4, and the modified graph G' = {{1, 2, 3,4}, {(1, 2), (2, 3), (3,4), (4, 5)}},
therefore the answer equals to 1.
Source
2016CCPC東北地區(qū)大學生程序設(shè)計競賽 - 重現(xiàn)賽
轉(zhuǎn)載于:https://www.cnblogs.com/Cwolf9/p/9433902.html
總結(jié)
以上是生活随笔為你收集整理的HDU5923-Prediction-有继承味道的并查集的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle常见sql笔试题,一路SQL
- 下一篇: 汽车诊断协议UDS概述