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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

codeforces1012 B. Chemical table(并查集+思维)

發(fā)布時間:2023/12/3 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 codeforces1012 B. Chemical table(并查集+思维) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

B. Chemical table

One of the way to solve this problem is to interprete the cells in 2d matrix as an edge in the bipartite graph, that is a cell (i,?j) is an edge between i of the left part and j of the right part.

將行號放一邊,列號放一邊構(gòu)造一個二分圖,如果表格中有一點(r,c)意味著二分圖左端r向右端c連一條邊。

Note, that each fusion operation (we have edges (r1,?c1), (r1,?c2), (r2,?c1) and get edge (r2,?c2)) doesn’t change the connected components of the graph.

題目中給的這個意思是不改變當(dāng)前圖的連通性。

然后需要求還需要最少的點數(shù)是的n+m個點聯(lián)通(行n個點,列m個點)

#include<bits/stdc++.h> using namespace std; using ll=long long; template <class T=int> T rd() {T res=0;T fg=1;char ch=getchar();while(!isdigit(ch)) {if(ch=='-') fg=-1;ch=getchar();}while( isdigit(ch)) res=(res<<1)+(res<<3)+(ch^48),ch=getchar();return res*fg; } const int N=400010; int fa[N]; int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);} int merge(int x,int y) {if(find(x)==find(y)) return 0;fa[find(x)]=find(y);return 1; } int n,m,q; int main() {n=rd(),m=rd(),q=rd();for(int i=1;i<=n+m;i++) fa[i]=i;int ans=n+m-1;while(q--){int r=rd(),c=rd();ans-=merge(r,c+n);}printf("%d\n",ans);return 0; }

總結(jié)

以上是生活随笔為你收集整理的codeforces1012 B. Chemical table(并查集+思维)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。