题意
            n个数1~n按顺序围成一个圈...现在在某些两点间加边..边可以加在圈内或者圈外..问是否会发生冲突?如果不发生冲突..输每一条边是放圈内还是圈外.
    题解
            这道题和POJ 3207差不多了..只是那道题只要判断是否存在不要输出方案...发现个很严重的问题..POJ 3207的数据实在是太弱了..我上一个程序里判断两个线段是否相交是个错了..都让我AC了..导致我做这题是沿用了思路...浪费了很多时间...
            先把每条线段看成一个组连个点..圈外和圈内..然后根据线段的冲突关系构造2-sat图..用tarjan做强联通分量判断是否存在方案使得每个线段都不冲突..并且将每个强联通分量缩成一个点,..若存在方案..开始找方案..将缩点后的图构造好..得到的会是一个有向无环图(要是有环那两个强联通分量就应该合并了..所以无环)..按照拓扑排序从入度为0的点进入...对到达的点染色(也就是标记)..并且将对应的一些点也染色(就是同一组的另一个,标记为另一个颜色)...最后根据染色输出每条边的内外..

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..并输出选择方案的更多相关文章

  1. CodeForces 24A Ring road(dfs)

    A. Ring road time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  3. Educational Codeforces Round 15 Road to Post Office

    Road to Post Office 题意: 一个人要从0走到d,可以坐车走k米,之后车就会坏,你可以修或不修,修要花t时间,坐车单位距离花费a时间,走路单位距离花费b时间,问到d的最短时间. 题解 ...

  4. 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 ...

  5. CodeForces 543D:Road Improvement

    题目:http://codeforces.com/problemset/problem/543/D 题意:给你一棵树,一开始边都是0,可以使任意的边变成1,对于每一个根节点求使得它到其他任一点的路径上 ...

  6. codeforces27D Ring Road 2

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Deep Learning 学习随记(四)自学习和非监督特征学习

    接着看讲义,接下来这章应该是Self-Taught Learning and Unsupervised Feature Learning. 含义: 从字面上不难理解其意思.这里的self-taught ...

  2. ios strong weak 的区别 与 理解

    先一句话总结:strong类保持他们拥有对象的活着,weak类他们拥有的对象被人家一牵就牵走,被人家一干就干死.(strong是一个好大哥所以strong,呵呵,weak是一个虚大哥所以weak,呵呵 ...

  3. C# 多线程详解

    1.使用多线程的几种方式 (1)不需要传递参数,也不需要返回参数 ThreadStart是一个委托,这个委托的定义为void ThreadStart(),没有参数与返回值. 复制代码 代码如下: cl ...

  4. Chess---->简单命令框象棋(人VS人)

    简单粗暴,直接先上代码: ChessBoard.h:  1 #ifndef CHESBOARD_H  2 #include<iostream>  3 #include<string& ...

  5. Extjs4 关于设置form中所有子控件为readOnly属性的解决方案

    之前在网上找了一堆,但那些确实没法用,后来考虑了一下,发现主要是网上提供的假设form中只有一层控件,没有考虑到布局稍微复杂的form情形,此处采用递归的形式实现对form中所有控件(grid及but ...

  6. 在CMD下用java命令出现“找不到或无法加载主类”问题

    解决思路: 从网上查找原因和解决方法,有提到环境变量classpath设置问题,但多次尝试问题依旧没有解决.然后使用java -cp %classpath; Hello执行,结果正确. 使用echo  ...

  7. C#面试-关于const和readonly(看了一个点赞很多的帖子有感而发!)

    前景提要: 最近大家都在面试,讨论最多.最基础的问题,莫过于“关于const和readonly常见的笔试题剖析”,等等的大牛解析.我就是一个小菜,只不过,有点不敢苟同大牛的意见.废话少说,进入重点. ...

  8. MYSQL的binary解决mysql数据大小写敏感问题 《转载》

    BINARY不是函数,是类型转换运算符,它用来强制它后面的字符串为一个二进制字符串,可以理解为在字符串比较的时候区分大小写如下:mysql> select binary 'ABCD'='abcd ...

  9. Unity NGUI实现按钮点击播放Aniamtion

    unity版本:4.5 NGUI版本:3.6.5 参考链接:http://www.colabug.com/thread-1029974-1-1.html,作者:COLABUG.COM 橘虞 1.怎么创 ...

  10. Unity C# 游戏间物体间的访问

    脚本语言:C# 1.在Unity工程中新建两个物体:Cube和Sphere 2.分别为Cube和Sphere添加脚本CubeScript和SphereScript: 在SphereScript这两个定 ...