Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tarjan algorithm #32

Merged
merged 5 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions 2-sat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
int n, m;
vector<int> g[20000];
int dfsn[20000], scc[20000];
stack<int> stk;
int ce;
vector<vector<int>> res;
int dfs(int now) {
dfsn[now] = ce++;
int ret = dfsn[now];
stk.push(now);
for (int nxt : g[now]) {
if (dfsn[nxt] == -1)
ret = min(ret, dfs(nxt));
else if (scc[nxt] == -1)
ret = min(ret, dfsn[nxt]);
}
if (ret == dfsn[now]) {
res.pb(vector<int>());

int t;
do {
t = stk.top();
stk.pop();
scc[t] = sz(res) - 1;
res.back().push_back(t);
} while (t != now);
// sort
}
return ret;
}

void solve() {
memset(dfsn, -1, sizeof dfsn);
memset(scc, -1, sizeof scc);
// ce = 0;
// res.clear();
//
cin >> n >> m;
// forn(i, n) g[i].clear();

forn(_, m) {
int x, y;
cin >> x >> y;
if (x < 0) {
x = -x;
--x;
x += n;
} else
--x;
if (y < 0) {
y = -y;
--y;
y += n;
} else
--y;
if (x >= n)
g[x - n].pb(y);
else
g[x + n].pb(y);
if (y >= n)
g[y - n].pb(x);
else
g[y + n].pb(x);
}
forn(i, 2 * n) {
if (dfsn[i] == -1)
dfs(i);
}
forn(i, n) {
if (scc[i] == scc[i + n]) {
cout << "0\n";
return;
}
}
cout << "1\n";
vector<int> v(2 * n);
vector<int> ans(n, -1);
iota(v.begin(), v.end(), 0);
sort(v.begin(), v.end(), [](int a, int b) { return scc[a] > scc[b]; });
forn(i, 2 * n) {
if (v[i] >= n) {
if (ans[v[i] - n] == -1)
ans[v[i] - n] = 1;
} else {
if (ans[v[i]] == -1)
ans[v[i]] = 0;
}
}
forn(i, n) cout << ans[i] << ' ';
cout << '\n';
}
60 changes: 60 additions & 0 deletions rotating_calipers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define x second
#define y first
vector<pii> V;
long long int ccw(pii a, pii b, pii c) {
return 1LL * (b.x - a.x) * (c.y - a.y) - 1LL * (b.y - a.y) * (c.x - a.x);
}
bool cmp(pii a, pii b) {
long long int c = ccw(V[0], a, b);
return c ? c > 0 : a < b;
}
double dis(pii a, pii b) { return hypot(a.x - b.x, a.y - b.y); }
pii operator-(pii a, pii b) { return make_pair(a.y - b.y, a.x - b.x); }
void solve(void) {
int N;
scanf("%d", &N);
V.resize(N);
for (int i = 0; i < N; ++i)
scanf("%d%d", &V[i].x, &V[i].y);
swap(V[0], *min_element(V.begin(), V.end()));
sort(V.begin() + 1, V.end(), cmp);
pii S[200010];
int sz = 0;
S[sz++] = V[0], S[sz++] = V[1];
for (int nx = 2; nx < N; S[sz++] = V[nx], ++nx)
while (sz >= 2 && ccw(S[sz - 2], S[sz - 1], V[nx]) <= 0)
sz--;
double ans = 0;
pii f, g;
for (int i = 0, j = 1; i < sz; ++i) {
int ni = (i + 1) % sz;
int nj = (j + 1) % sz;
while (1) {
pii a = {0, 0};
pii b = S[ni] - S[i];
pii c = S[nj] - S[j];
long long int val = ccw(a, b, c);
double d = dis(S[i], S[j]);
if (ans < d) {
ans = d;
f = S[i];
g = S[j];
}
if (val <= 0)
break;
j = (j + 1) % sz;
nj = (j + 1) % sz;
}
}
printf("%d %d %d %d\n", f.x, f.y, g.x, g.y);
}
int main(void) {
int T;
scanf("%d", &T);
while (T--)
solve();
return 0;
}
38 changes: 38 additions & 0 deletions tarjan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
constexpr int MAXN = 10000;

int n, m; // 0-based
vector<int> graph[MAXN];
int dfsn[MAXN], scc[MAXN];
stack<int> stk;
vector<vector<int>> res;
int ce;
int dfs(int now) {
dfsn[now] = ce++;
int ret = dfsn[now];
stk.push(now);
for (int nxt : graph[now]) {
if (dfsn[nxt] == -1)
ret = min(ret, dfs(nxt));
else if (scc[nxt] == -1)
ret = min(ret, dfsn[nxt]);
}
if (ret == dfsn[now]) {
res.push_back(vector<int>());
int t;
do {
t = stk.top();
stk.pop();
scc[t] = res.size() - 1;
res.back().push_back(t);
} while (t != now);
}
return ret;
}
void set_scc() {
memset(dfsn, -1, sizeof dfsn);
memset(scc, -1, sizeof scc);
res.clear();
ce = 0;
for (int i = 0; i < n; ++i)
if (dfsn[i] == -1) dfs(i);
}
Binary file modified teamnote.pdf
Binary file not shown.
10 changes: 8 additions & 2 deletions teamnote.tex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ \subsection{Euler's Phi Function}
\subsection{FFT(Fast Fourier Transform)}
\lstinputlisting[style=mycpp]{fft.cpp}

\subsection{2-SAT}
\lstinputlisting[style=mycpp]{2-sat.cpp}



\section{String}
Expand Down Expand Up @@ -89,8 +92,8 @@ \subsection{Heavy Light Decomposition}

\section{Graph}

% \subsection{SCC: Tarjan's Algorithm}
% \lstinputlisting[style=mycpp]{}
\subsection{SCC: Tarjan's Algorithm}
\lstinputlisting[style=mycpp]{tarjan.cpp}

\subsection{SCC: Kosaraju's Algorithm}
\lstinputlisting[style=mycpp]{kosaraju.cpp}
Expand Down Expand Up @@ -128,6 +131,9 @@ \section{Geometry}
\subsection{CCW(CounterClockWise)}
\lstinputlisting[style=mycpp]{ccw.cpp}

\subsection{Rotating Calipers}
\lstinputlisting[style=mycpp]{rotating_calipers.cpp}



\end{document}