地址 https://algospot.com/judge/problem/read/MEETINGROOM

解答  2-sat 代码样例过了 没有ac。 我又没有正确代码对拍。。。。。

已确认是输出问题 修改完成

 #include <algorithm>
#include <iostream>
#include <vector>
#include <stack> using namespace std; vector<vector<int>> adj; vector<int> sccId, discovered, finished;
stack<int> st; //保存顶点序号的栈
int sccCounter, vertexCounter; //返回以here为根节点的子树中
//能够到达后向边的最小发现顺序
int scc(int here) {
int ret = discovered[here] = vertexCounter++;
//将here存入栈,here的所有后代节点都会在here之后进栈
st.push(here); for (int i = ; i < adj[here].size(); ++i) {
int there = adj[here][i];
//(here,there)是树边
if (discovered[there] == -)
ret = min(ret, scc(there));
else if (discovered[there] < discovered[here] && finished[there] != )
ret = min(ret, discovered[there]);
} //判断here是否为强联通分量的根节点
if (ret == discovered[here]) {
//以here为根节点的子树中,将剩余所有顶点全部绑定为同一分量
while (true) {
int t = st.top();
st.pop();
sccId[t] = sccCounter;
if (t == here) break;
}
++sccCounter;
} finished[here] = ;
return ret;
} //tarjan 的scc算法
vector<int> tarjanSCC() {
//数组和计数器的初始化
sccId = discovered = finished = vector<int>(adj.size(), -);
sccCounter = vertexCounter = ; //对所有顶点调用scc()
for (int i = ; i < adj.size(); ++i)
if (discovered[i] == -) scc(i);
return sccId;
} //========================================================================
//图的领接表表示法
//vector<vector<int>> adj; bool disjoint(const pair<int, int>& a, const pair<int, int>& b) {
return a.second <= b.first || b.second <= a.first;
} //如果meetings[]表示各队提出的开会时间
//则将此题转换为2-SAT问题后生成蕴含图
//第i个团队需要选择meetings[2*i]或meetings[2*i+1]时候之一开会
void makeGraph(const vector<pair<int, int>>& meetings)
{
int vars = meetings.size(); //每个变量对应图的两个顶点
adj.clear(); adj.resize(vars * );
for (int i = ; i < vars; i += ) {
//各团队需要选择第i号和第j号会议之一
//添加(i or j )句子
int j = i + ;
adj[i * + ].push_back(j * );
adj[j * + ].push_back(i * );
} for (int i = ; i < vars; ++i) {
for (int j = ; j < i; ++j) {
//第i号会议和第j号会议重叠
if (!disjoint(meetings[i], meetings[j])) {
//放弃第i个会议 或者放弃第j个会议
//添加 (~i or ~j)子句
adj[i * ].push_back(j * + );
adj[j * ].push_back(i * + );
}
}
}
} vector<int> solve2SAT()
{
int n = adj.size() / ;
vector<int> label = tarjanSCC(); for (int i = ; i < * n; i += )
if (label[i] == label[i + ])
return vector<int>(); vector<int> value( * n, -); vector<pair<int, int>> order;
for (int i = ; i < * n; i++)
order.push_back(make_pair(label[i], i));
sort(order.begin(), order.end()); for (int i = ; i < * n; ++i) {
int vertex = order[i].second;
int variable = vertex / , isTrue = vertex % ;
if (value[variable] != -) continue;
value[variable] = !isTrue;
}
return value;
} int main()
{
int n;
cin >> n; while (n--) {
int m;
cin >> m;
vector<pair<int, int>> meetings;
while (m--) {
int a, b, c, d;
cin >> a >> b >> c >> d;
meetings.push_back(make_pair(a, b));
meetings.push_back(make_pair(c, d));
}
makeGraph(meetings); vector<int> v = solve2SAT();
if (v.empty()) {
cout << "IMPOSSIBLE" << endl;;
}
else {
cout << "POSSIBLE" << endl;
for (int i = ; i < v.size()/; i+=) {
if (v[i] == ) {
cout << meetings[i].first << ' ' << meetings[i].second << endl;
}
else {
cout << meetings[i+].first << ' ' << meetings[i+].second << endl;
}
}
}
} return ;
}

一份tarjan 模板

 // 11111111.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// #include "pch.h"
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm> using namespace std; const int maxn = 2e5 + ;
vector<int> E[maxn];
int vis[maxn];
int dfn[maxn], low[maxn], tot, n, ans = maxn;
stack<int> s; void tarjan(int x)
{
low[x] = dfn[x] = ++ tot;
s.push(x); vis[x] = ; for (int i = ; i < E[x].size(); i++) {
int v = E[x][i];
if (!dfn[v]) {
tarjan(v);
low[x] = min(low[x], low[v]);
}
else if (vis[v]) {
low[x] = min(low[x], dfn[v]);
}
} if (low[x] == dfn[x]) {
int cnt = ;
while () {
int now = s.top();
s.pop();
vis[x] = ;
cnt++;
if (now == x) break;
}
if (cnt > ) ans = min(ans, cnt);
} } int main()
{
scanf("%d",&n );
for (int i = ; i <= n; i++) {
int x;
scanf("%d", &x);
E[i].push_back(x);
} for (int i = ; i <= n; i++) {
if (!dfn[i])
tarjan(i);
} cout << ans << endl; return ;
}

tarjan模板

算法问题实战策略 MEETINGROOM 附一份tarjan模板的更多相关文章

  1. 算法问题实战策略 PICNIC

    下面是另一道搜索题目的解答过程题目是<算法问题实战策略>中的一题oj地址是韩国网站 连接比较慢 https://algospot.com/judge/problem/read/PICNIC ...

  2. 《算法问题实战策略》-chaper7-穷举法

    关于这一章节<算法实战策略>有一段概述问题,我认为对于编程人员来说非常有价值,故在这里进行如下的摘抄: 构想算法是很艰难的工作.相比大家都经历过,面对复杂的要求只是傻乎乎地盯着显示器,或者 ...

  3. 《算法问题实战策略》-chaper32-网络流

    基本的网络流模型: 在图论这一块初步的应用领域中,两个最常见的关注点,其一时图中的路径长度,也就是我们常说的的最短路径问题,另一个则是所谓的“流问题”. 流问题的基本概念: 首先给出一张图. 其实所谓 ...

  4. 《算法问题实战策略》-chaper13-数值分析

    这一章节主要介绍我们在进行数值分析常用的二分.三分和一个近似求解区间积分的辛普森法. 首先介绍二分. 其实二分的思想很好理解并且笔者在之前的一些文章中也有所渗透,对于二次函数甚至单元高次函数的零点求解 ...

  5. 《算法问题实战策略》——chaper9——动态规划法技巧

    Q1: 数字游戏: 两个人(A.B)用n个整数排成的一排棋盘玩游戏,游戏从A开始,每个人有如下操作: (1)    拿走棋盘最右侧或者最左侧的棋子,被拿走的数字从棋盘中抹掉. (2)    棋盘中还剩 ...

  6. 《算法问题实战策略》-chaper8-动态规划法

    Q1:偶尔在电视上看到一些被称为“神童”的孩子们背诵小数点以后几万位的圆周率.背诵这么长的数字,可利用分割数字的方法.我们用这种方法将数字按照位数不等的大小分割后再背诵. 分割形式如下: 所有数字都相 ...

  7. 《算法问题实战策略》-chaper21-树的实现和遍历

    这一章节开始介绍一个数据结构中的一个基本概念——树. 我们从数据结构的解读来解释树结构的重要性,现实世界的数据除了最基本的线性结构(我们常用队列.数组和链表等结构表征),还有一个重要的特性——层级结构 ...

  8. 算法问题实战策略 QUADTREE

    地址 https://algospot.com/judge/problem/read/QUADTREE 将压缩字符串还原后翻转再次压缩的朴素做法 在数据量庞大的情况下是不可取的 所以需要在压缩的情况下 ...

  9. 算法问题实战策略 DICTIONARY

    地址 https://algospot.com/judge/problem/read/DICTIONARY 解法 构造一个26字母的有向图 判断无回路后 就可以输出判断出来的字符序了 比较各个字母的先 ...

随机推荐

  1. Linux(一)

    1.简单命令        1.1 ls指令         语法1:#ls  [路径]               表示列出指定路径下的文件夹和文件的名字,如果路径没有指定则列出当前路径下的(lis ...

  2. Laravel6实现第三方 微信登录

    目前很多的网站中都会存在很多的交互功能,从而降低用户的操作难度,特此带来微信的第三方登录的项目实战功能开发.对于本实例中的开发内容,就不在使用原生的内容,而是直接使用别人写好的封装的类库. 1. 安装 ...

  3. java类成员的默认可访问性是什么?你猜

    先看下面TestModel: import org.apache.commons.collections4.BidiMap; import org.apache.commons.collections ...

  4. SLES 12: Database Startup Error with ORA-27300 ORA-27301 ORA-27303 While Starting using Srvctl (Doc ID 2340986.1)

    SLES 12: Database Startup Error with ORA-27300 ORA-27301 ORA-27303 While Starting using Srvctl (Doc ...

  5. MySQL数据库:子查询的应用

    子查询 子查询是一种常用计算机语言SELECT-SQL语言中嵌套查询下层的程序模块.当一个查询是另一个查询的条件时,称之为子查询. # 子查询的用法 # 在字段 select (select cNam ...

  6. Python3 中的Number

    Python3 支持 int.float.bool.complex(复数). 在Python 3里,只有一种整数类型 int,表示为长整型,没有 python2 中的 Long. 像大多数语言一样,数 ...

  7. Python数据结构性能分析

    1.目标 告诉大家Python列表和字典操作的 大O 性能.然后我们将做一些基于时间的实验来说明每个数据结构的花销和使用这些数据结构的好处 2.实操 在列表的操作有一个非常常见的编程任务就是是增加一个 ...

  8. ADB常用命令(一)

    转自:https://blog.csdn.net/qq_26552691/article/details/81348222 一.操作前请确认电脑上已配置好ADB环境.可在CMD命令行输入adb,如果出 ...

  9. Java中文本文件的读取(按行读取)

    在之前的学习过程中,经常会遇到将文本文件中的数据读取到数组或其他数据结构中.每次遇到,总是在网上搜索代码解决,解决之后并没有总结复习,因此在下一次遇到同样的问题时,又重复之前的过程.这样周而复始,并没 ...

  10. IT兄弟连 HTML5教程 HTML5表单 新增的表单属性3

    9  novalidate novalidate是属性规定在提交表单时不应该验证form和input域.novalidate属性适用于的<input>类型有:text.search.url ...