题意
            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. js正则实现用户输入银行卡号的控制及格式化

    //js正则实现用户输入银行卡号的控制及格式化 <script language="javascript" type="text/javascript"& ...

  2. 简单的SqlHelper

    namespace Login { class SqlHelper { //连接数据库的字符串 //static string dataConnection = "server=***-PC ...

  3. Zend Server安装后首次运行就出现Internal Server Error的解决

    无论是使用哪个版本的Zend Server来搭建PHP服务器,首次运行都会出现Internal Server Error的错误,对很多新手而言,每当看到这种错误时,那一刻内心绝对都是崩溃的.然而,这个 ...

  4. 【转】 C++库常用函数一览

    本文中提到的函数库有:<string> <cctype> <algorithm> <cmath> <cstdlib> <iomanip ...

  5. 寒哥教你学 iOS - 经验漫谈(转)

    转自http://www.cocoachina.com/ios/20150907/13339.html 本篇文章主要讲解 4个问题 load妙用 aop面向切面编程 NSNumber Or Int @ ...

  6. App上线基本流程

    还可参考的:http://www.cocoachina.com/bbs/read.php?tid=330302 iOS项目上传前期准备材料: 1.已有开发者账号 2.已有发布证书 3.一张1024*1 ...

  7. asp.net mvc 部署在IIS7.5上出现的[没有相关的源行]错误的解决办法

    今天在IIS7.5上部署一个MVC小项目的时候出现以下错误:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET File ...

  8. C++中的static关键字的总结 (转载)

    C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用. 1.面向过程设计中的st ...

  9. 学习WindowsPhone 2013/12/22

    菜鸟一枚,只能边看别人的博客变学习来提升自己,参考博客内容:http://blog.csdn.net/column/details/wp-comming.html?page=3 ,稍微看了一下,写的还 ...

  10. B题 - A+B for Input-Output Practice (I)

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description You ...