From bafd0a2d901fef7c295e43bed0fbfa57d0e2fe7d Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Thu, 15 Jul 2021 20:48:00 +0800 Subject: [PATCH] =?UTF-8?q?P3275=20[SCOI2011]=E7=B3=96=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R53241911 --- Luogu/problem/P3275/P3275.cpp | 72 +++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Luogu/problem/P3275/P3275.cpp diff --git a/Luogu/problem/P3275/P3275.cpp b/Luogu/problem/P3275/P3275.cpp new file mode 100644 index 00000000..95c25cdc --- /dev/null +++ b/Luogu/problem/P3275/P3275.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +int n, m, x, a, b, head[300005], vis[300005], used[300005], dis[300005], num; +queue q; + +struct { + int next, to, val; +} g[1000005]; + +void add(int u, int v, int w) { + g[++num].next = head[u]; + g[num].to = v; + g[num].val = w; + head[u] = num; +} + +int main() { + cin >> n >> m; + for (int i = 1; i <= m; i++) { + cin >> x >> a >> b; + if (x == 1) { + add(a, b, 0); + add(b, a, 0); + } else if (x == 2) { + add(a, b, 1); + } else if (x == 3) { + add(b, a, 0); + } else if (x == 4) { + add(b, a, 1); + } else if (x == 5) { + add(a, b, 0); + } + if (x % 2 == 0 && a == b) { + cout << -1 << endl; + exit(0); + } + } + for (int i = 1; i <= n; i++) { + vis[i] = 1; + dis[i] = 1; + used[i] = 1; + q.push(i); + } + while (!q.empty()) { + int z = q.front(); + q.pop(); + if (used[z] >= n - 1) { + cout << -1 << endl; + 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; + } + } + } + vis[z] = 0; + } + long long ans = 0; + for (int i = 1; i <= n; i++) { + ans += dis[i]; + } + cout << ans << endl; + return 0; +}