题意
            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. 借用Toad 生成表空间的使用量图示

    图示产生方法 图示(tablespace uage)如下

  2. Spring 创建bean的模式

    在默认情况下,spring创建bean是单例模式 scope="singleton ",还有一种方式为多例模式[prototype]     scope          sing ...

  3. Vijos1734 NOI2010 海拔 平面图最小割

    建立平面图的对偶图,把最小割转化成最短路问题 Dijkstra算法堆优化 (被输入顺序搞WA了好几次T_T) #include <cstdio> #include <cstring& ...

  4. 【POJ1195】【二维树状数组】Mobile phones

    Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...

  5. 了解负载均衡 会话保持 session同步(转)

    一,什么负载均衡 一个新网站是不要做负载均衡的,因为访问量不大,流量也不大,所以没有必要搞这些东西.但是随着网站访问量和流量的快速增长,单台服务器受自身硬件条件的限制,很难承受这么大的访问量.在这种情 ...

  6. Linux下ln链接命令详解

    ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个不同的链接,这个命令最常用的参数是-s,具体用法是:ln –s 源文件 目标文件. 当我们需要在不同的目录,用到相同的 ...

  7. AspNet MVC : 操作/控制器过滤器(action filter)

    1.Action Filter Action Filter提供了在执行action/controller前后对请求/响应修改的能力,可以应用于action和控制器上,作用在控制器上将自动被应用到该控制 ...

  8. ajax验证用户名和密码

    var user = form.name.value; var password = form.password.value; var url = "chkname.php?user=&qu ...

  9. 【技术宅6】把一个无限级分类循环成tree结构

    function list_to_tree($list,$root=0,$pk='cid',$pid = 'pid',$child = '_child'){ if(is_array($list)) { ...

  10. Python自动化运维之29、Bottle框架

    Bottle 官网:http://bottlepy.org/docs/dev/index.html Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除 ...