標籤:acm c語言 演算法 編程 2-sat
題目地址:HDU 1824
這題可以把每隊的兩個隊員看成一個,這樣就是2-sat水題了。。。
代碼如下:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL __int64const int INF=0x3f3f3f3f;int head[2100], cnt, index, top, ans;int dfn[2100], low[2100], instack[2100], stak[2100], belong[2100];int id[2100];struct node{ int u, v, next;}edge[1000000];void add(int u, int v){ edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++;}void tarjan(int u){ dfn[u]=low[u]=++index; stak[++top]=u; instack[u]=1; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]) { low[u]=min(low[u],dfn[v]); } } if(dfn[u]==low[u]) { ans++; while(1) { int v=stak[top--]; instack[v]=0; belong[v]=ans; if(u==v) break; } }}void init(){ memset(head,-1,sizeof(head)); cnt=index=top=ans=0; memset(instack,0,sizeof(instack)); memset(dfn,0,sizeof(dfn));}int main(){ int n, m, i, j, a, b, c, flag; while(scanf("%d%d",&n,&m)!=EOF) { init(); for(i=0;i<n;i++) { scanf("%d%d%d",&a,&b,&c); id[a]=i<<1; id[b]=id[c]=i<<1|1; } for(i=0;i<m;i++) { scanf("%d%d",&a,&b); add(id[a],id[b]^1); add(id[b],id[a]^1); } for(i=0;i<2*n;i++) { if(!dfn[i]) tarjan(i); } flag=0; for(i=0;i<n;i++) { if(belong[i<<1]==belong[i<<1|1]) { flag=1; break; } } if(flag) puts("no"); else puts("yes"); } return 0;}
HDU 1824 Let's go home (2-SAT)