POJ 2723 HDU 1816 Get Luffy Out
二分答案 + 2-SAT验证
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=+; int N,M;
stack<int>S;
vector<int>G[maxn];
vector<int>FG[maxn];
int Belong[maxn];
int flag[maxn];
int Block; int doorX[maxn],doorY[maxn];
int keyX[maxn],keyY[maxn];
int left,right,mid,ans; void init()
{
for(int i=; i<maxn; i++) G[i].clear();
for(int i=; i<maxn; i++) FG[i].clear();
memset(Belong,,sizeof Belong);
memset(flag,,sizeof flag);
while(!S.empty()) S.pop();
Block=;
} void addEdge(int x,int y)
{
G[x].push_back(y);
FG[y].push_back(x);
} void dfs1(int now)
{
flag[now]=;
for(int i=; i<G[now].size(); i++)
if(!flag[G[now][i]])
dfs1(G[now][i]);
S.push(now);
} void dfs2(int now)
{
Belong[now]=Block;
for(int i=; i<FG[now].size(); i++)
if(!Belong[FG[now][i]])
dfs2(FG[now][i]);
} bool judge()
{
for(int i=; i<**N; i++) if(!flag[i]) dfs1(i);
while(!S.empty())
{
int Top=S.top();
S.pop();
if(!Belong[Top])
{
Block++;
dfs2(Top);
}
}
for(int i=; i<*N; i++)
if(Belong[*i]==Belong[*i+])
return ;
return ;
} void read()
{
for(int i=;i<N;i++)
scanf("%d%d",&keyX[i],&keyY[i]); for(int i=;i<M;i++)
scanf("%d%d",&doorX[i],&doorY[i]);
} int main()
{
while(~scanf("%d%d",&N,&M))
{
if(!N&&!M) break;
read();
left=,right=M;
while(left<=right)
{
mid=(left+right)/;
init();
for(int i=;i<N;i++)
{
addEdge(*keyX[i]+,*keyY[i]);
addEdge(*keyY[i]+,*keyX[i]);
}
for(int i=;i<mid;i++)
{
addEdge(*doorX[i],*doorY[i]+);
addEdge(*doorY[i],*doorX[i]+);
} if(judge()){ans=mid;left=mid+;}
else right=mid-;
}
printf("%d\n",ans);
}
return ;
}
POJ 2723 HDU 1816 Get Luffy Out的更多相关文章
- HDU - 1816 Get Luffy Out *(二分 + 2-SAT)
题目大意:有N串钥匙,M对锁.每串钥匙仅仅能选择当中一把.怎样选择,才干使开的锁达到最大(锁仅仅能按顺序一对一对开.仅仅要开了当中一个锁就可以) 解题思路:这题跟HDU - 3715 Go Deepe ...
- HDU 1816 Get Luffy Out *
Get Luffy Out * Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 1816, POJ 2723 Get Luffy Out(2-sat)
HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...
- POJ 2723 Get Luffy Out(2-SAT+二分答案)
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8851 Accepted: 3441 Des ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
- poj 2723 Get Luffy Out(2-sat)
Description Ratish is a young man who always dreams of being a hero. One day his friend Luffy was ca ...
随机推荐
- hdu_5787_K-wolf Number(数位DP)
题目链接:hdu_5787_K-wolf Number 题意: 给你一个区间,让你找满足任意k个数位内都没有相同的数字的个数 题解: 因为k不大,就直接将当前pos的前k-1个数传进去就行了 #inc ...
- HDU4310:Hero
Problem Description When playing DotA with god-like rivals and pig-like team members, you have to fa ...
- 微信支付WxpayAPI_php_v3(一)sdk简介与错误修改
经过断断续续将近一周的时间终于把微信支付调通了. 这里总结一下,算是给后来者有个指引.少踩坑!!!! 开发语言:php5.5 语言框架:laravel5.2 微信sdk:WxpayAPI_php_v3 ...
- linux下编译安装apache
在linux(CentOS6.5)上安装Apache,要首先确保以下程序事先安装 apr:The mission of the Apache Portable Runtime (APR) projec ...
- NoRouteToHostException
http://stackoverflow.com/questions/1572215/how-to-avoid-a-noroutetohostexception 端口不够用,注意一定要关闭所有不用的连 ...
- CSS样式 初学
CSS样式 参考网站: CSS用法:3种 一:直接样式表 如<p style="color:red;">这是一个段落</p> 二:内部样式表 如:<s ...
- iOS 枚举写法
1.第一种 typedef enum { kPRStateNormal = 0, kPRStatePulling = 1, kPRStateLoading = 2, kPRStateHitTheEnd ...
- 火狐解决OCSP回应包含过期信息的问题
连接 addons.mozilla.org 时发生错误. OCSP 回应包含过期信息. (错误码: sec_error_ocsp_old_response)hosts文件添加 vi /etc/host ...
- cURL: Learning..
CURL usage.. -v, -m, -H, -I, -s, --connect-timeout, -x, -X GET|POST, -d, -T, -o. --retry, -u curl [o ...
- Sticks<DFS>
题意: 给n个木棍,这些木棍是由m个长度均为L的木棍切割而来,求L的最小值. 思路: DFS+剪枝. 剪枝: 1:L的取值范围在n(max)和n(sum)之间,逐个枚举.sum%L!=0则L不能用. ...