From 14338f0f108166a3cc75cc28db4307993eb57baf Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 2 Jan 2022 11:27:07 +0800 Subject: [PATCH] =?UTF-8?q?P1262=20=E9=97=B4=E8=B0=8D=E7=BD=91=E7=BB=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R66133558 --- Luogu/P1262/P1262.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Luogu/P1262/P1262.cpp diff --git a/Luogu/P1262/P1262.cpp b/Luogu/P1262/P1262.cpp new file mode 100644 index 00000000..a9abd928 --- /dev/null +++ b/Luogu/P1262/P1262.cpp @@ -0,0 +1,85 @@ +#include +#include +#include + +using std::cin; +using std::cout; +using std::endl; + +const int N = 3005; + +int n, p, x, r, u, v, ans, a[N], w[N]; +std::vector g[N]; + +// Tarjan +int cnt, dfn[N], low[N]; +int scc_cnt, id[N]; +int din[N]; +std::stack st; +bool vis[N]; + +void tarjan(int u) { + dfn[u] = low[u] = ++cnt; + st.push(u); + vis[u] = true; + for (int v : g[u]) { + 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; + w[scc_cnt] = std::min(w[scc_cnt], a[v]); + } while (v != u); + } +} + +int main() { + std::fill(a, a + N, 0x7fff); + std::fill(w, w + N, 0x7fff); + cin >> n >> p; + for (int i = 1; i <= p; i++) { + cin >> x >> a[x]; + } + cin >> r; + for (int i = 1; i <= r; i++) { + cin >> u >> v; + g[u].push_back(v); + } + for (int i = 1; i <= n; i++) { + if (!dfn[i] && a[i] != 0x7fff) { + tarjan(i); + } + } + for (int i = 1; i <= n; i++) { + if (!dfn[i]) { + cout << "NO" << endl + << i << endl; + exit(0); + } + } + for (int i = 1; i <= n; i++) { + for (int v : g[i]) { + if (id[i] != id[v]) { + din[id[v]]++; + } + } + } + for (int i = 1; i <= scc_cnt; i++) { + if (!din[i]) { + ans += w[i]; + } + } + cout << "YES" << endl + << ans << endl; + return 0; +}