您的位置:首页 > 编程语言 > Go语言

hdu1824 Let's go home--2-sat

2016-09-17 19:57 351 查看
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1824

题意:n个队伍,每一个队(三人一队),或者队长留下或者其余两名队员同时留下;接下来m对编号,每一对队员,如果队员A留下,则队员B必须回家休息下,或者B留下,A回家。

分析:用hash来保存编号,对于一个队三个人,给队长单独的hash值,再给剩下的两人相同的hash值,这样问题就转化了,很简单。

#define _CRT_SECURE_NO_DEPRECATE

#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
#define INF 99999999
#define eps 0.0001
using namespace std;

int n, m;
int index, cnt;
int dfn[2005];
int low[2005];
int belong[2005];
int hash1[30005];
bool inStack[2005];
vector<int> vec[2005];
stack<int> s;

void tarjan(int u)
{
low[u] = dfn[u] = ++index;
inStack[u] = 1;
s.push(u);
for (int i = 0; i < vec[u].size(); i++)
{
int v = vec[u][i];
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 p;
do
{
p = s.top();
s.pop();
inStack[p] = 0;
belong[p] = cnt;
} while (p != u);
}
}

void solve()
{
for (int i = 0; i < 2 * n; i++)
if (!dfn[i])
tarjan(i);

for (int i = 0; i < 2 * n; i = i + 2)
{
if (belong[i] == belong[i + 1])
{
printf("no\n");
return;
}
}
printf("yes\n");
}

int main()
{
while (~scanf("%d%d", &n, &m))
{
//init
index = cnt = 0;
for (int i = 0; i < 2 * n; i++)
{
vec[i].clear();
dfn[i] = 0;
inStack[i] = 0;
}

int x, y, z;
int t = 0;
for (int i = 0; i < n; i++)
{
scanf("%d%d%d", &x, &y, &z);
hash1[x] = t++;
hash1[y] = hash1[z] = t++;
}

for (int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
vec[hash1[x]].push_back(hash1[y] ^ 1);
vec[hash1[y]].push_back(hash1[x] ^ 1);
}

solve();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: