Let's go home

Time Limit: 10000/1000 MS (Java/Others)    Memory
Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1616    Accepted Submission(s): 661

Problem Description
小时候,乡愁是一枚小小的邮票,我在这头,母亲在那头。

—— 余光中



集训是辛苦的。道路是坎坷的,歇息还是必须的。经过一段时间的训练,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
 
Author
威士忌
 
Source
解题思路:
2-SAT就是两者有冲突就连边。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#define LL long long
using namespace std;
const int MAXN = 20010;
vector<int>G[MAXN];
int pre[MAXN], lowlink[MAXN], sccno[MAXN], dfs_clock, scc_cnt;
stack<int> s;
void dfs(int u)
{
pre[u] = lowlink[u] = ++dfs_clock;
s.push(u); int sz = G[u].size();
for(int i=0;i<sz;i++)
{
int v = G[u][i];
if(!pre[v])
{
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}
else if(!sccno[v])
{
lowlink[u] = min(lowlink[u], pre[v]);
}
}
if(lowlink[u] == pre[u])
{
scc_cnt++;
for(;;)
{
int x = s.top(); s.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
}
void find_scc(int n)
{
dfs_clock = scc_cnt = 0;
memset(sccno, 0, sizeof(sccno));
memset(pre, 0, sizeof(pre));
for(int i=1;i<=n;i++) if(!pre[i])
dfs(i);
}
int N, M, T;
int main()
{
while(scanf("%d%d", &T, &M)!=EOF)
{
int x, y, z;
N = 3 * T;
for(int i=0;i<=2*N;i++) G[i].clear();
for(int i=1;i<=T;i++)
{
scanf("%d%d%d", &x, &y, &z);
x++; y++; z++;
G[x+N].push_back(y);
G[x+N].push_back(z);
G[y+N].push_back(x);
G[z+N].push_back(x);
}
for(int i=1;i<=M;i++)
{
scanf("%d%d", &x, &y);
x++; y++;
G[x].push_back(y + N);
G[y].push_back(x + N);
}
find_scc(2 * N);
int flag = 1;
for(int i=1;i<=N;i++)
{
if(sccno[i] == sccno[i + N])
{
flag = 0;
break;
}
}
if(flag) puts("yes");
else puts("no");
}
return 0;
}

HDU 1824 Let&#39;s go home (2-SAT判定)的更多相关文章

  1. HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT

    一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...

  2. HDU 1824 Let's go home(2-SAT+Tarjan)

    Let's go home Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组)

    pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...

  4. HDU 3966 Aragorn&#39;s Story(树链剖分)

    HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...

  5. hdu 5282 Senior&#39;s String 两次dp

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282 Senior's String Time Limit: 2000/1000 MS (Java/Oth ...

  6. HDU 3177 Crixalis&#39;s Equipment(贪婪)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=3177 Problem Description Crixalis - Sand King used t ...

  7. HDU - 5186 - zhx&#39;s submissions (精密塔尔苏斯)

    zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  8. HDU 2120 Ice_cream&#39;s world I(并检查集合)

    职务地址:HDU 2120 这题尽管字数不多,但就是看不懂. . 意思是求最多有多少个被墙围起来的区域.显然就是求环的个数.然后用并查集求环个数就能够了. 代码例如以下: #include <i ...

  9. HDU 3729 I&#39;m Telling the Truth(二部图最大匹配+结果输出)

    职务地址:HDU 3729 二分图最大匹配+按字典序输出结果. 仅仅要从数字大的開始匹配就能够保证字典序最大了.群里有人问. . 就顺手写了这题. . 代码例如以下: #include <ios ...

随机推荐

  1. 自定义tableView通用MVC设计

  2. WCF未找到终结点

    配置都配了,仍然找不到,config文件没有重新加载,原因不详,只能重新编译一下,就好了....后续找找原因看看

  3. 解决sqlplus无法退格问题

      # wget http://download.openpkg.org/components/cache/rlwrap/rlwrap-0.42.tar.gz # tar -zxf rlwrap-0. ...

  4. tomcat7使用dbcp连接池遇到的坑

    项目部署在tomcat后每隔一段时间便会报错 Cause: java.sql.SQLException: Could not retrieve transation read-only status ...

  5. python_OS 模块

    os模块 用于提供系统级别的操作 os.getcwd() # 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") # 改变当前脚本工作目 ...

  6. Centos 7安装Mysql5.7

    1.下载(国内镜像,比搜狐的快一点):http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc ...

  7. JavaScript中离线应用和客户端存储(cookies、sessionStorage、localStorage)

    一.离线应用 所谓离线web应用,就是在设备不能上网的情况下仍然可以运行的应用. 开发离线web应用需要几个步骤:首先,确保应用知道设备是否能上网,以便下一步执行正确的操作:然后,应用还必须能访问一定 ...

  8. Java学习--异常处理及其应用类

    异常是运行时在代码序列中引起的非正常状况,换句话说,异常是运行时错误.在不支持异常处理的计算机语言中,必须手动检查和处理错误----通常是通过使用错误代码,等等.这种方式既笨拙又麻烦.Java的异常处 ...

  9. luoguT21777

    #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> ...

  10. 跟初学者学习IbatisNet第三篇

    这一章我们主要介绍一下IbatisNet里面的动态sql语句的运用,比如有时候我们想进行模糊查询,参数是动态加入的.或者要实现top n ,order by ,分页等功能的时候,我们就不得不用动态拼接 ...