同 AcWing 1169 糖果
Version 1 SPFA
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
const double pi(acos(-1));
const int inf(0x3f3f3f3f);
const int mod(1e9 + 7);
const int maxn(1e5 + 10);
bool vis[maxn];
int ecnt, head[maxn], cnt[maxn];
ll dis[maxn];
struct edge {
int to, wt, nxt;
} edges[maxn * 3];
template<typename T = int>
inline const T read()
{
T x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
template<typename T>
inline void write(T x, bool ln)
{
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10, false);
putchar(x % 10 + '0');
if (ln) putchar(10);
}
void addEdge(int u, int v, int w)
{
edges[ecnt].to = v;
edges[ecnt].wt = w;
edges[ecnt].nxt = head[u];
head[u] = ecnt++;
}
bool spfa(int n)
{
memset(dis, -0x3f, sizeof dis);
dis[0] = 0;
stack<int> st;
st.push(0);
vis[0] = true;
while (not st.empty()) {
int u = st.top();
st.pop();
vis[u] = false;
for (int i = head[u]; compl i; i = edges[i].nxt) {
int v = edges[i].to, w = edges[i].wt;
if (dis[v] < dis[u] + w) {
dis[v] = dis[u] + w;
if (not vis[v]) {
vis[v] = true;
st.push(v);
cnt[v] = cnt[u] + 1;
if (cnt[v] >= n + 1) {
return false;
}
}
}
}
}
return true;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
memset(head, -1, sizeof head);
int n = read(), k = read();
while (k--) {
int x = read(), u = read(), v = read();
if (x == 1) {
addEdge(v, u, 0);
addEdge(u, v, 0);
} else if (x == 2) {
addEdge(u, v, 1);
} else if (x == 3) {
addEdge(v, u, 0);
} else if (x == 4) {
addEdge(v, u, 1);
} else {
addEdge(u, v, 0);
}
}
for (int i = 1; i <= n; ++i) {
addEdge(0, i, 1);
}
if (spfa(n)) {
printf("%lld
", accumulate(dis + 1, dis + 1 + n, 0ll));
} else {
printf("-1
");
}
return 0;
}
Version 2 Tarjan
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
const double pi(acos(-1));
const int inf(0x3f3f3f3f);
const int mod(1e9 + 7);
const int maxn(1e5 + 10);
const int maxm(6e5 + 10);
int ecnt, head[maxn], shead[maxn];
int tim, dfn[maxn], low[maxn];
int scnt, scc[maxn], siz[maxn];
int dis[maxn];
bool vis[maxn];
stack<int> st;
struct edge {
int to, wt, nxt;
} edges[maxm];
template<typename T = int>
inline const T read()
{
T x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
template<typename T>
inline void write(T x, bool ln)
{
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10, false);
putchar(x % 10 + '0');
if (ln) putchar(10);
}
void addEdge(int u, int v, int w, bool s = false)
{
edges[ecnt].to = v;
edges[ecnt].wt = w;
if (s) {
edges[ecnt].nxt = shead[u];
shead[u] = ecnt++;
} else {
edges[ecnt].nxt = head[u];
head[u] = ecnt++;
}
}
void tarjan(int u)
{
dfn[u] = low[u] = ++tim;
st.push(u);
vis[u] = true;
for (int i = head[u]; compl i; i = edges[i].nxt) {
int v = edges[i].to;
if (not dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (vis[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u]) {
++scnt;
int v = -1;
do {
v = st.top();
st.pop();
vis[v] = false;
scc[v] = scnt;
++siz[scnt];
} while (v not_eq u);
}
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
memset(head, -1, sizeof head);
memset(shead, -1, sizeof shead);
int n = read(), m = read();
while (m--) {
int t = read(), u = read(), v = read();
if (t == 1) {
addEdge(u, v, 0);
addEdge(v, u, 0);
} else if (t == 2) {
addEdge(u, v, 1);
} else if (t == 3) {
addEdge(v, u, 0);
} else if (t == 4) {
addEdge(v, u, 1);
} else {
addEdge(u, v, 0);
}
}
for (int i = 1; i <= n; ++i) {
addEdge(0, i, 1);
}
tarjan(0);
bool flag = true;
for (int u = 0; u <= n; ++u) {
for (int i = head[u]; compl i; i = edges[i].nxt) {
int v = edges[i].to, w = edges[i].wt;
int a = scc[u], b = scc[v];
if (a not_eq b) {
addEdge(a, b, w, true);
} else if (w > 0) {
flag = false;
break;
}
}
if (not flag) {
break;
}
}
if (not flag) {
write(-1, true);
} else {
for (int u = scnt; u; --u) {
for (int i = shead[u]; compl i; i = edges[i].nxt) {
int v = edges[i].to, w = edges[i].wt;
dis[v] = max(dis[v], dis[u] + w);
}
}
ll sum = 0;
for (int i = 1; i <= scnt; ++i) {
sum += dis[i] * siz[i];
}
write(sum, true);
}
return 0;
}