CodeForces 27D - Ring Road 2 构图2-sat..并输出选择方案
Program:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#define ll long long
#define oo 1000000007
#define pi acos(-1.0)
#define MAXN 205
using namespace std;
struct node
{
int x,y;
}line[MAXN];
vector<int> T[MAXN];
int dfn[MAXN],low[MAXN],DfsIndex,tpnum,tp[MAXN],color[MAXN];
bool instack[MAXN],arc[MAXN][MAXN],d[MAXN];
stack<int> mystack;
bool ok(int a,int b)
{
if (line[a].y>line[b].x && line[a].y<line[b].y)
if (!(line[a].x>=line[b].x && line[a].x<=line[b].y)) return false;
if (line[a].x>line[b].x && line[a].x<line[b].y)
if (!(line[a].y>=line[b].x && line[a].y<=line[b].y)) return false;
return true;
}
void tarjan(int x)
{
int i,y,m=T[x].size();
dfn[x]=low[x]=++DfsIndex;
instack[x]=true;
mystack.push(x);
for (i=0;i<m;i++)
{
y=T[x][i];
if (!dfn[y])
{
tarjan(y);
low[x]=min(low[x],low[y]);
}else
if (instack[y]) low[x]=min(low[x],dfn[y]);
}
if (dfn[x]==low[x])
{
tpnum++;
do
{
x=mystack.top();
mystack.pop();
instack[x]=false;
tp[x]=tpnum;
}while (dfn[x]!=low[x]);
}
return;
}
bool judge(int m)
{
int i;
for (i=0;i<m;i++)
if (tp[i<<1]==tp[(i<<1)|1]) return false;
return true;
}
void dfs(int x,int m)
{
int i;
color[x]=1;
for (i=0;i<(m<<1);i++) if (tp[i]==x) color[tp[i^1]]=-1;
for (i=0;i<tpnum;i++)
if (arc[x][i] && !color[i]) dfs(i,m);
return;
}
int main()
{
int i,j,n,m;
while (~scanf("%d%d",&n,&m))
{
for (i=0;i<m;i++)
{
int x,y,t;
scanf("%d%d",&x,&y);
if (x>y) t=x,x=y,y=t;
line[i].x=x,line[i].y=y;
}
for (i=0;i<(m<<1);i++) T[i].clear();
for (i=0;i<m;i++)
for (j=i+1;j<m;j++)
if (!ok(i,j))
{
T[i<<1].push_back((j<<1)|1);
T[j<<1].push_back((i<<1)|1);
T[(i<<1)|1].push_back(j<<1);
T[(j<<1)|1].push_back(i<<1);
}
memset(instack,false,sizeof(instack));
memset(dfn,0,sizeof(dfn));
while (!mystack.empty()) mystack.pop();
DfsIndex=tpnum=0;
for (i=0;i<(m<<1);i++)
if (!dfn[i]) tarjan(i);
if (!judge(m))
{
printf("Impossible\n");
continue;
}
memset(arc,false,sizeof(arc));
memset(d,0,sizeof(d));
for (i=0;i<(m<<1);i++)
{
int x,num=T[i].size();
for (x=0;x<num;x++)
{
arc[tp[i]][tp[T[i][x]]]=true;
d[tp[T[i][x]]]++;
}
}
memset(color,0,sizeof(color));
for (i=0;i<tpnum;i++)
if (!color[i]) dfs(i,m);
for (i=0;i<m;i++)
if (color[tp[i<<1]]==1) printf("i");
else printf("o");
printf("\n");
}
return 0;
}
CodeForces 27D - Ring Road 2 构图2-sat..并输出选择方案的更多相关文章
- CodeForces 24A Ring road(dfs)
A. Ring road time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- Educational Codeforces Round 15 Road to Post Office
Road to Post Office 题意: 一个人要从0走到d,可以坐车走k米,之后车就会坏,你可以修或不修,修要花t时间,坐车单位距离花费a时间,走路单位距离花费b时间,问到d的最短时间. 题解 ...
- Educational Codeforces Round 15_D. Road to Post Office
D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...
- CodeForces 543D:Road Improvement
题目:http://codeforces.com/problemset/problem/543/D 题意:给你一棵树,一开始边都是0,可以使任意的边变成1,对于每一个根节点求使得它到其他任一点的路径上 ...
- codeforces27D Ring Road 2
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- codeforces 228E The Road to Berland is Paved With Good Intentions(2-SAT)
Berland has n cities, some of them are connected by bidirectional roads. For each road we know wheth ...
- codeforces 702D D. Road to Post Office(数学)
题目链接: D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces 702 D Road to Post Office
题目描述 Vasiliy has a car and he wants to get from home to the post office. The distance which he needs ...
随机推荐
- Web性能压力测试之Webbench使用详解
Webbench是知名的网站压力测试工具,它是由Lionbridge公司(http://www.lionbridge.com)开发.Webbench能测试处在相同硬件上,不同服务的性能以及不同硬件上同 ...
- 11g 创建asm磁盘组
[root@Oracle11g ~]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes255 heads, 63 sectors/track, 2 ...
- JSONKit 在iOS9 arm7 64位下出现的问题
最近遇到了一个关于JSONKit的问题,在项目加了arm7 64位以后,JSONKIT会出现[params JSONString] forKey:@”gson”];报错的情况,如下图 具体原因不太清楚 ...
- Composite 模式的实现
实现要点: 1.组合模式采用树形结构来实现普遍存在的对象容器,从而将“一对多”的关系转化“一对一”的关系,使得客户代码可以一致地处理对象和对象容器,无需关心处理的是单个的对象,还是组合的对象容器. 2 ...
- 【NOI2004】郁闷的出纳员
[问题描述] OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的 ...
- php之框架增加日志记录功能类
<?php /* 思路:给定文件,写入读取(fopen ,fwrite……) 如果大于1M 则重写备份 传给一个内容, 判断大小,如果大于1M,备份 小于则写入 */ class Log{ // ...
- Node.js包(JXcore)
Node.js的代码是开放的,并准备好被复制像任何其他Javascript代码.但现在它不可能的了.JXcore 这是一个开源项目,引入了包装和源文件和其他资源加密成JX包一个独特的功能. 考虑大型项 ...
- 图片的css自适应
当需要css来缩放图片的时候,可以采用外层容器100%或者任意百分比, 内层图片img tag 没有宽高,用sass写经过断点后的mixin中的样式就是这样: .workscon_section{ w ...
- 清除html的标签和行内样式
function shieldStyle(){ this._styleStartArr=["<span","<p","<strong ...
- 获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName
http://blog.csdn.net/bichenggui/article/details/4774457 -------------------------------------------- ...