地址 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模板