POJ3683 Falsita
http://poj.org/problem?id=3683
思路:2-SAT,输出任意一组方案,O(m+n)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int first[],next[],tot,go[];
int First[],Next[],Tot,Go[];
int b[],a[],n,ru[],low[],dfn[];
int op[],belong[],instack[],vis[],st[],c[],col[],sz,num,top;
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-')f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
}
void Insert(int x,int y){
Tot++;
Go[Tot]=y;
Next[Tot]=First[x];
First[x]=Tot;
}
bool jud(int x,int y){
if (b[x]<=a[y]||a[x]>=b[y]) return ;
else return ;
}
void build(){
for (int i=;i<=n;i++)
for (int j=i+;j<=n;j++){
if (jud(i*-,j*)){
insert(i*-,j*-);
insert(j*,i*);
}
if (jud(i*-,j*-)){
insert(i*-,j*);
insert(j*-,i*);
}
if (jud(i*,j*)){
insert(i*,j*-);
insert(j*,i*-);
}
if (jud(i*,j*-)){
insert(i*,j*);
insert(j*-,i*-);
}
}
}
void init(){
n=read();
for (int i=;i<=n;i++){
a[i*]=read();
a[i*]=a[i*]*+read();
b[i*-]=read();
b[i*-]=b[i*-]*+read();
int x=read();
b[i*]=a[i*]+x;
a[i*-]=b[i*-]-x;
}
}
void tarjan(int x){
low[x]=dfn[x]=++sz;
instack[x]=vis[x]=;st[++top]=x;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (!vis[pur]){
tarjan(pur);
low[x]=std::min(low[x],low[pur]);
}else if (instack[pur]){
low[x]=std::min(low[x],dfn[pur]);
}
}
if (low[x]==dfn[x]){
num++;
while (st[top]!=x){
instack[st[top]]=;
belong[st[top]]=num;
top--;
}
instack[st[top]]=;
belong[st[top]]=num;
top--;
}
}
void Tarjan(){
for (int i=;i<=*n;i++)
if (!vis[i]) tarjan(i);
}
bool judge(){
for (int i=;i<=n;i++)
if (belong[i*]==belong[i*-]) {
puts("NO");
return ;
}
puts("YES");
return ;
}
void dfs(int x){
if (col[x]) return ;col[x]=-;
for (int i=First[x];i;i=Next[i]){
int pur=Go[i];
dfs(pur);
}
}
void topsort(){
int t=;
for (int i=;i<=num;i++)
if (!ru[i]) c[++t]=i;
while (t){
int now=c[t--];
if (col[now]) continue;col[now]=;
dfs(op[now]);
for (int i=First[now];i;i=Next[i]){
int pur=Go[i];
ru[pur]--;
if (ru[pur]==) c[++t]=pur;
}
}
}
void rebuild(){
for (int i=;i<=*n;i++)
for (int j=first[i];j;j=next[j]){
int pur=go[j];
if (belong[pur]==belong[i]) continue;
ru[belong[i]]++;
Insert(belong[pur],belong[i]);;
}
for (int i=;i<=*n;i++)
op[belong[i*]]=belong[i*-],op[belong[i*-]]=belong[i*];
}
void print(int x){
printf("%.2d:",x/);
printf("%.2d ",x%);
}
void Output(){
for (int i=;i<=n;i++)
if (col[belong[i*]]==){
print(a[i*]);print(b[i*]);puts("");
}else{
print(a[i*-]);print(b[i*-]);puts("");
}
}
int main(){
init();
build();
Tarjan();
if (judge()) return ;
rebuild();
topsort();
Output();
}
为什么我会犯用错数组这种错误。。
POJ3683 Falsita的更多相关文章
- poj3683
poj3683 题意 n对新人举行婚礼,婚礼在不同时间段但可能重叠,婚礼有开始(Si).结束(Ti).仪式举行时间(Di),问能否给出一种举行方案,使得神父能参加所有的婚礼并举行仪式. 分析 xi为真 ...
- [BZOJ3683]Falsita
[BZOJ3683]Falsita 题目大意: 一个\(n(n\le3\times10^5)\)个结点的树,每个结点有一个权值\(w_i\),\(m(m\le3\times10^5)\)次操作,操作包 ...
- 2-sat——输出方案poj3683
一篇讲的详细的博客 https://blog.csdn.net/Hawo11/article/details/74908233 缩点后为什么要建立反图? 如果是按原图处理,选择一个点之后要把所有其后续 ...
- NKOJ-4573 Falsita
问题描述: 到海边了呢...... 如果没有那次选择,现在是不是会好些呢...... 都过去了. 仰望着星空,迎面吹过一阵阵海风,倚靠着护栏,Fine 在海边静静地伫立着,在一个个无际的长夜后,Fin ...
- 2-sat 输出任意一组可行解&拓扑排序+缩点 poj3683
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8170 Accept ...
- poj3683 Priest John's Busiest Day
2-SAT 输出可行解 找可行解的方案就是: 根据第一次建的图建一个反图..然后求逆拓扑排序,建反图的原因是保持冲突的两个事件肯定会被染成不同的颜色 求逆拓扑排序的原因也是为了对图染的色不会发生冲突, ...
- poj3683 Priest John's Busiest Day
2-SAT. 读入用了黄学长的快速读入,在此膜拜感谢. 把每对时间当作俩个点.如果有交叉代表相互矛盾. 然后tarjan缩点,这样就能得出当前的2-SAT问题是否有解. 如果有解,跑拓扑排序就能找出一 ...
- 【POJ3683】Priest John's Busiest Day
题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...
- Poj3683:Priest John's Busiest Day
题意 n对夫妻要结婚,第i对夫妻结婚的婚礼持续时间为[Si, Ti],他们会举行一个仪式,仪式时间为Di,这个仪式只能举行在开头或者结尾举行,要么[Si, Si+Di],要么[Ti-Di, Ti],然 ...
随机推荐
- Best Time to Buy and Sell Stock IV 解答
Question Say you have an array for which the ith element is the price of a given stock on day i. Des ...
- 常用cl命令参数解释
紧接前文,第一行cl命令如下: 1> cl /c /IC:\...\include /ZI /nologo- /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG ...
- 使用python程序监控云服务器的带宽
将一些不重要的服务迁移到云服务上,有些下载什么的,为了防止带宽超了,python做了一个监控程序.用python3写的,由于和python2有些区别,特记录下来备查. 代码如下: #this is f ...
- JDBC编程之事务处理
JDBC中的事务处理指的是事务的一致性等问题,例如插入一条数据(id,name,password)到表中,另一条数据(address,id)到另一张表中,并且两条数据是相关联的,那么假设第一条数据插入 ...
- Linux Resin 安装配置
Resin是一个非常流行的application server,对servlet和JSP提供了良好的支持,性能优良,resin自身采用Java语言开发.Resin Pro版本支持缓存和负载均衡,收费最 ...
- [Angular 2] Property Binding
Property Binding is bind property NOT attribute! import {Component, Input, Output, EventEmitter} fro ...
- Android 基于Netty的消息推送方案之Hello World(一)
消息推送方案(轮询.长连接) 轮询 轮询:比较简单的,最容易理解和实现的就是客户端去服务器上拉信息,信息的及时性要求越高则拉信息的频率越高.客户端拉信息的触发可以是一些事件,也可以是一个定时器,不断地 ...
- 定时改变App主题的方案
1.将接口返回的图片缓存到本地,由于写data到本地是耗时操作,为了不阻塞主线程,可开启子线程来做此操作 dispatch_queue_t queue = dispatch_queue_create( ...
- vs2013+EF6+Mysql
1.首先需要在整个项目中添加一个Model类库,在类库中引用EF 我需要在该项目下添加EF的MYSQL对象实体 首先需要引入几个相关引用,我通过NuGet来添加,如下图 接下来我需要通过ADO.NET ...
- 【warning】clang the linker unused
这个问题是 我在写第一个 mac os 下的helloworld遇到的 就像是 大家写第一个java中的 helloworld 肯定也是要在命令窗口下进行操作 一样 为了让一些和我一样的刚入门的孩子学 ...