Submission #563935

#TimeUsernameProblemLanguageResultExecution timeMemory
563935tengiz05전압 (JOI14_voltage)C++17
100 / 100
518 ms42052 KiB
#include <bits/stdc++.h> using i64 = long long; void solve() { int n, m; std::cin >> n >> m; std::vector<std::vector<int>> e(n); std::map<std::pair<int, int>, int> id, cnt; for (int i = 0; i < m; i++) { int u, v; std::cin >> u >> v; u--; v--; e[u].push_back(v); e[v].push_back(u); id[std::minmax(u, v)] = i; cnt[std::minmax(u, v)]++; } std::vector<int> color(n, -1), p(n), dep(n); p[0] = -1; std::function<void(int)> dfs = [&](int u) { for (int v : e[u]) { if (color[v] == -1) { p[v] = u; dep[v] = dep[u] + 1; color[v] = color[u] ^ 1; dfs(v); } } }; for (int i = 0; i < n; i++) { if (color[i] == -1) { color[i] = 0; p[i] = -1; dfs(i); } } std::vector<int> f(n); int badEdges = 0; for (int u = 0; u < n; u++) { for (int v : e[u]) { if (dep[u] < dep[v] && p[v] != u) { if (color[u] == color[v]) { badEdges++; f[v]++; f[u]--; } else { f[v]--; f[u]++; } } } } std::vector<bool> vis(n); dfs = [&](int u) { vis[u] = true; for (int v : e[u]) { if (p[v] == u && !vis[v]) { dfs(v); f[u] += f[v]; } } }; for (int i = 0; i < n; i++) { if (!vis[i]) { dfs(i); } } std::vector<int> ans; if (badEdges == 1) { for (auto [p, i] : id) { if (color[p.first] == color[p.second] && cnt[std::minmax(p.first, p.second)] == 1) { ans.push_back(i); } } } for (int i = 1; i < n; i++) { if (f[i] == badEdges && cnt[std::minmax(p[i], i)] == 1) { ans.push_back(id[std::minmax(p[i], i)]); } } std::sort(ans.begin(), ans.end()); std::cout << ans.size() << "\n"; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t = 1; // std::cin >> t; while (t--) { solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...