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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

BZOJ2199: [Usaco2011 Jan]奶牛议会

發(fā)布時(shí)間:2024/4/13 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BZOJ2199: [Usaco2011 Jan]奶牛议会 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

趁此機(jī)會(huì)學(xué)了一下2-SAT。

以前的2-SAT都是用并查集寫的,只能應(yīng)用于極小的一部分情況,這次學(xué)了一正式的2-SAT,是用一張有向圖來表示其依賴關(guān)系。

2-SAT的介紹參見劉汝佳《訓(xùn)練指南》。

1 /************************************************************** 2 Problem: 2199 3 User: zhuohan123 4 Language: C++ 5 Result: Accepted 6 Time:140 ms 7 Memory:1388 kb 8 ****************************************************************/ 9 10 #include <iostream> 11 #include <cstdio> 12 #include <cstring> 13 #include <algorithm> 14 using namespace std; 15 int n,m; 16 struct point{int y,n;}p[1100];int pnum; 17 int head[2100]; 18 struct edge{int next,to;}g[11000];int gnum; 19 void addedge(int from,int to) 20 { 21 g[++gnum].to=to;g[gnum].next=head[from];head[from]=gnum; 22 } 23 int q[2100],l,r; 24 bool cango[2100]; 25 bool check(int po) 26 { 27 memset(cango,0,sizeof cango); 28 cango[po]=true;l=1;r=0;q[++r]=po; 29 while(l<=r) 30 for(int i=head[q[l++]];i;i=g[i].next) 31 if(!cango[g[i].to])cango[q[++r]=g[i].to]=true; 32 for(int i=1;i<=n;i++) 33 if(cango[p[i].y]&&cango[p[i].n])return false; 34 return true; 35 } 36 char ans[1100]; 37 int main(int argc, char *argv[]) 38 { 39 #ifndef ONLINE_JUDGE 40 freopen("1.in","r",stdin); 41 freopen("1.out","w",stdout); 42 #endif 43 scanf("%d%d",&n,&m); 44 for(int i=1;i<=n;i++)p[i].n=++pnum,p[i].y=++pnum; 45 while(m--) 46 { 47 int b,c;char vb[4],vc[4]; 48 scanf("%d%s%d%s",&b,vb,&c,vc); 49 if(vb[0]=='Y'&&vc[0]=='Y')addedge(p[b].n,p[c].y),addedge(p[c].n,p[b].y); 50 if(vb[0]=='Y'&&vc[0]=='N')addedge(p[b].n,p[c].n),addedge(p[c].y,p[b].y); 51 if(vb[0]=='N'&&vc[0]=='Y')addedge(p[b].y,p[c].y),addedge(p[c].n,p[b].n); 52 if(vb[0]=='N'&&vc[0]=='N')addedge(p[b].y,p[c].n),addedge(p[c].y,p[b].n); 53 } 54 for(int i=1;i<=n;i++) 55 { 56 bool by=check(p[i].y),bn=check(p[i].n); 57 if(by&&bn)ans[i]='?'; 58 else if(by)ans[i]='Y'; 59 else if(bn)ans[i]='N'; 60 else {puts("IMPOSSIBLE");return 0;} 61 } 62 puts(ans+1); 63 return 0; 64 }

P.S.這個(gè)代碼寫的相當(dāng)非主流啊!

轉(zhuǎn)載于:https://www.cnblogs.com/zhuohan123/p/3285261.html

總結(jié)

以上是生活随笔為你收集整理的BZOJ2199: [Usaco2011 Jan]奶牛议会的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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