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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

2021牛客多校3 - Black and white(思维+最小生成树)

發布時間:2024/4/11 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2021牛客多校3 - Black and white(思维+最小生成树) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個 n?mn*mn?m 的矩陣,初始時都是白色,可以花費掉 cost[i][j]cost[i][j]cost[i][j] 將格子 (i,j)(i,j)(i,j) 染黑,也可以通過魔法選擇兩行 x1,x2x1,x2x1,x2 和兩列 y1,y2y1,y2y1,y2,如果標記中的四個點有三個點是黑色的,那么第四個點可以直接被染黑

問矩陣被染黑的最小貢獻

題目分析:比賽的時候分析出來只需要選擇 n+m?1n+m-1n+m?1 個點就可以滿足條件了,但是沒有思考出來這些點之間的關系,壓根沒往圖論上去思考,只能說是見識太少了

如果將每個點視為邊,將行和列視為 n+mn+mn+m 個點,利用魔法操作后,不難發現 x1,x2,y1,y2x1,x2,y1,y2x1,x2,y1,y2 四個點的連通性不發生改變,所以我們需要在進行魔法操作之前,就令 n+mn+mn+m 個點都變成連通的才行

當點 (i,j)(i,j)(i,j) 被染黑后,第 iii 行和第 jjj 列就是連通的了

所以對 n+mn+mn+m 個點求最小生成樹就是答案了

注意,sort 的克魯斯卡爾會被卡掉,可以選擇桶排的克魯斯卡爾或者prim

代碼:

// Problem: Black and white // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/11254/B // Memory Limit: 1048576 MB // Time Limit: 4000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) (x&-x) using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; vector<pair<int,int>>node[N]; int f[N]; int find(int x) {return f[x]==x?x:f[x]=find(f[x]); } bool merge(int x,int y) {int xx=find(x),yy=find(y);if(xx!=yy) {f[xx]=yy;return true;}return false; } void init() {for(int i=0;i<N;i++) {f[i]=i;} } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);init();LL n,m,a,b,c,d,p;cin>>n>>m>>a>>b>>c>>d>>p;for(int i=1;i<=n;i++) {for(int j=1;j<=m;j++) {a=(a*a*b+a*c+d)%p;node[a].push_back({i,n+j});}}LL ans=0;for(int i=0;i<p;i++) {for(auto it:node[i]) {if(merge(it.first,it.second)) {ans+=i;}}}cout<<ans<<endl;return 0; } 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的2021牛客多校3 - Black and white(思维+最小生成树)的全部內容,希望文章能夠幫你解決所遇到的問題。

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