Submission #548408

#TimeUsernameProblemLanguageResultExecution timeMemory
548408TemmieIslands (IOI08_islands)C++17
36 / 100
2096 ms131072 KiB
#include <bits/stdc++.h> int n; std::vector <std::vector <std::pair <int, long long>>> g; std::vector <int> cc; void dfs(int node, int c, std::vector <bool>& vis) { if (vis[node]) { return; } vis[node] = true; cc[node] = c; for (auto p : g[node]) { dfs(p.first, c, vis); } } std::set <int> st; long long dfs(int node, int par, int orig) { if (st.count(node)) { return 0; } if (par != -1 && node == orig) { return 0; } st.insert(node); long long r = 0; for (auto p : g[node]) { if (p.first != par && !st.count(p.first)) { r = std::max(r, dfs(p.first, node, orig) + p.second); } } st.erase(node); return r; } int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); std::cin >> n; g.resize(n); for (int i = 0; i < n; i++) { int u, w; std::cin >> u >> w; u--; g[u].emplace_back(i, w), g[i].emplace_back(u, w); } cc.resize(n, -1); std::vector <bool> vis(n, false); int cnt = 0; for (int i = 0; i < n; i++) { if (!vis[i]) { dfs(i, cnt++, vis); } } std::vector <long long> cans(cnt, 0); for (int i = 0; i < n; i++) { cans[cc[i]] = std::max(cans[cc[i]], dfs(i, -1, i)); } long long ans = 0; for (long long x : cans) { ans += x; } std::cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...