生活随笔
收集整理的這篇文章主要介紹了
hdu 1824 Let's go home(2-sat 基础题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1824
| 小時候,鄉愁是一枚小小的郵票,我在這頭,母親在那頭。 ????????????????????????—— 余光中
集訓是辛苦的,道路是坎坷的,休息還是必須的。經過一段時間的訓練,lcy決定讓大家回家放松一下,但是訓練還是得照常進行,lcy想出了如下回家規定,每一個隊(三人一隊)或者隊長留下或者其余兩名隊員同時留下;每一對隊員,如果隊員A留下,則隊員B必須回家休息下,或者B留下,A回家。由于今年集訓隊人數突破往年同期最高記錄,管理難度相當大,lcy也不知道自己的決定是否可行,所以這個難題就交給你了,呵呵,好處嘛~,免費**漂流一日。 ? ? Input 第一行有兩個整數,T和M,1<=T<=1000表示隊伍數,1<=M<=5000表示對數。 接下來有T行,每行三個整數,表示一個隊的隊員編號,第一個隊員就是該隊隊長。 然后有M行,每行兩個整數,表示一對隊員的編號。 每個隊員只屬于一個隊。隊員編號從0開始。 ? ? Output 可行輸出yes,否則輸出no,以EOF為結束。 ? ? Sample Input 1 2 0 1 2 0 1 1 2 2 4 0 1 2 3 4 5 0 3 0 4 1 3 1 4 ? ? Sample Output yes no |
?
題意:u,v,w隊長,隊員,隊長留下兩個隊員可以回家,兩個隊員留下,隊長回家
2-sat問題,把兩個隊員看成一個整體就變成一個簡單2-sat問題了
#pragma GCC optimize(2)
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 1e4 + 100;
const int inf = 0x3f3f3f3f;
typedef long long ll;
struct node
{int v, next;
}edge[maxn];
int head[maxn], dfn[maxn], low[maxn], instack[maxn], Stack[maxn], belong[maxn];
int n, tot, index, top, cnt, m;
void init()
{memset(head, -1, sizeof(head));memset(dfn, 0, sizeof(dfn));tot = index = 0;
}
void addedge(int u, int v)
{edge[tot].v = v;edge[tot].next = head[u];head[u] = tot++;return;
}
void tarjan(int u)
{dfn[u] = low[u] = ++index;instack[u] = 1;Stack[top++] = u;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 (low[u] == dfn[u]){cnt++;int v;do {v = Stack[--top];instack[v] = 0;belong[v] = cnt;} while (u != v);}
}
int main()
{//freopen("C://input.txt", "r", stdin);while (scanf("%d%d", &n,&m) != EOF){init();top = 0;cnt = 0;int len = -1;int op[maxn];memset(op, 0, sizeof(op));for (int i = 1; i <= n; i++){int u, v, w;scanf("%d%d%d", &u, &v, &w);op[u] = ++len;op[v] = ++len;op[w] = len;}for (int i = 1; i <= m; i++){int u, v;scanf("%d%d", &u, &v);addedge(op[u], op[v] ^ 1);addedge(op[v], op[u] ^ 1);}for (int i = 0; i < n << 1; i++){if (!dfn[i]){tarjan(i);}}int flag = 0;for (int i = 0; i < n; i++){if (belong[i << 1] == belong[(i << 1) ^ 1]){flag = 1;}}if (flag){printf("no\n");}else{printf("yes\n");}}return 0;
}
?
總結
以上是生活随笔為你收集整理的hdu 1824 Let's go home(2-sat 基础题)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。