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 ...
随机推荐
- js正则实现用户输入银行卡号的控制及格式化
//js正则实现用户输入银行卡号的控制及格式化 <script language="javascript" type="text/javascript"& ...
- 简单的SqlHelper
namespace Login { class SqlHelper { //连接数据库的字符串 //static string dataConnection = "server=***-PC ...
- Zend Server安装后首次运行就出现Internal Server Error的解决
无论是使用哪个版本的Zend Server来搭建PHP服务器,首次运行都会出现Internal Server Error的错误,对很多新手而言,每当看到这种错误时,那一刻内心绝对都是崩溃的.然而,这个 ...
- 【转】 C++库常用函数一览
本文中提到的函数库有:<string> <cctype> <algorithm> <cmath> <cstdlib> <iomanip ...
- 寒哥教你学 iOS - 经验漫谈(转)
转自http://www.cocoachina.com/ios/20150907/13339.html 本篇文章主要讲解 4个问题 load妙用 aop面向切面编程 NSNumber Or Int @ ...
- App上线基本流程
还可参考的:http://www.cocoachina.com/bbs/read.php?tid=330302 iOS项目上传前期准备材料: 1.已有开发者账号 2.已有发布证书 3.一张1024*1 ...
- asp.net mvc 部署在IIS7.5上出现的[没有相关的源行]错误的解决办法
今天在IIS7.5上部署一个MVC小项目的时候出现以下错误:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET File ...
- C++中的static关键字的总结 (转载)
C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用. 1.面向过程设计中的st ...
- 学习WindowsPhone 2013/12/22
菜鸟一枚,只能边看别人的博客变学习来提升自己,参考博客内容:http://blog.csdn.net/column/details/wp-comming.html?page=3 ,稍微看了一下,写的还 ...
- B题 - A+B for Input-Output Practice (I)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description You ...