Submission #548409

#TimeUsernameProblemLanguageResultExecution timeMemory
548409TemmieIslands (IOI08_islands)C++17
40 / 100
2080 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::vector <bool> st; long long dfs(int node) { if (st[node]) { return 0; } st[node] = true; long long r = 0; for (auto p : g[node]) { if (!st[p.first]) { r = std::max(r, dfs(p.first) + p.second); } } st[node] = false; return r; } int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); std::cin >> n; st.resize(n, false); 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)); } 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...