0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-09 16:58:48 +00:00

P3275 [SCOI2011]糖果

R66266202
This commit is contained in:
Baoshuo Ren 2022-01-04 21:33:02 +08:00
parent d775e2ce8b
commit d15e40ad7f
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -1,71 +1,92 @@
#include <bits/stdc++.h> #include <iostream>
#include <stack>
#include <vector>
using namespace std; using std::cin;
using std::cout;
using std::endl;
int n, m, x, a, b, head[300005], vis[300005], used[300005], dis[300005], num; const int N = 100005;
queue<int> q;
struct { int n, m, t, a, b;
int next, to, val; long long ans;
} g[1000005]; std::vector<std::pair<int, int>> g[N], g2[N];
void add(int u, int v, int w) { // Tarjan
g[++num].next = head[u]; int cnt, dfn[N], low[N];
g[num].to = v; int scc_cnt, id[N], siz[N];
g[num].val = w; std::stack<int> st;
head[u] = num; bool vis[N];
void tarjan(int u) {
dfn[u] = low[u] = ++cnt;
st.push(u);
vis[u] = true;
for (auto e : g[u]) {
int v = e.first;
if (!dfn[v]) {
tarjan(v);
low[u] = std::min(low[u], low[v]);
} else if (vis[v]) {
low[u] = std::min(low[u], dfn[v]);
} }
}
if (low[u] == dfn[u]) {
scc_cnt++;
int v;
do {
v = st.top();
st.pop();
vis[v] = false;
id[v] = scc_cnt;
siz[scc_cnt]++;
} while (v != u);
}
}
// Longest Path
int dist[N];
int main() { int main() {
cin >> n >> m; cin >> n >> m;
for (int i = 1; i <= n; i++) {
g[0].push_back(std::make_pair(i, 1));
}
for (int i = 1; i <= m; i++) { for (int i = 1; i <= m; i++) {
cin >> x >> a >> b; cin >> t >> a >> b;
if (x == 1) { if (t == 1) {
add(a, b, 0); g[a].push_back(std::make_pair(b, 0));
add(b, a, 0); g[b].push_back(std::make_pair(a, 0));
} else if (x == 2) { } else if (t == 2) {
add(a, b, 1); g[a].push_back(std::make_pair(b, 1));
} else if (x == 3) { } else if (t == 3) {
add(b, a, 0); g[b].push_back(std::make_pair(a, 0));
} else if (x == 4) { } else if (t == 4) {
add(b, a, 1); g[b].push_back(std::make_pair(a, 1));
} else if (x == 5) { } else {
add(a, b, 0); g[a].push_back(std::make_pair(b, 0));
} }
if (x % 2 == 0 && a == b) { }
tarjan(0);
for (int i = 0; i <= n; i++) {
for (auto e : g[i]) {
if (id[e.first] != id[i]) {
g2[id[i]].push_back(std::make_pair(id[e.first], e.second));
} else if (e.second) {
cout << -1 << endl; cout << -1 << endl;
exit(0); exit(0);
} }
} }
for (int i = 1; i <= n; i++) {
vis[i] = 1;
dis[i] = 1;
used[i] = 1;
q.push(i);
} }
while (!q.empty()) { for (int i = scc_cnt; i > 0; i--) {
int z = q.front(); for (auto e : g2[i]) {
q.pop(); int v = e.first,
if (used[z] >= n - 1) { w = e.second;
cout << -1 << endl; dist[v] = std::max(dist[v], dist[i] + w);
exit(0);
}
used[z]++;
for (int i = head[z]; i; i = g[i].next) {
int t = g[i].to;
if (dis[t] < dis[z] + g[i].val) {
dis[t] = dis[z] + g[i].val;
if (!vis[t]) {
q.push(t);
vis[t] = 1;
} }
} }
} for (int i = 1; i <= scc_cnt; i++) {
vis[z] = 0; ans += 1ll * dist[i] * siz[i];
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
ans += dis[i];
} }
cout << ans << endl; cout << ans << endl;
return 0; return 0;