Submission #134888

#TimeUsernameProblemLanguageResultExecution timeMemory
134888onjo0127Telegraph (JOI16_telegraph)C++11
100 / 100
67 ms15476 KiB
#include <bits/stdc++.h> using namespace std; const long long INF = 1LL * 1e18; int N, A[100009], C[100009], D[100009], vs[100009]; bool fin[100009]; vector<vector<int> > R; vector<int> T[100009]; long long ans; void go(vector<int> &S, int x, int c) { S.push_back(x); vs[x] = c; if(vs[A[x]]) { if(!fin[x] && vs[A[x]] == c) { vector<int> stk; while(1) { int tmp = S.back(); S.pop_back(); stk.push_back(tmp); if(tmp == A[x]) break; } for(auto& it: stk) fin[it] = 1; reverse(stk.begin(), stk.end()); R.push_back(stk); } } else go(S, A[x], c); } void dfs(int x) { D[x] = 0; long long s = 0; for(auto& it: T[x]) { dfs(it); D[x] = max(D[x], C[it]); s += C[it]; } ans += s - D[x]; } int main() { scanf("%d",&N); for(int i=1; i<=N; i++) scanf("%d%d",&A[i],&C[i]); for(int i=1; i<=N; i++) if(!vs[i]) { vector<int> S; go(S, i, i); } // printf("rings: %d\n", R.size()); // for(auto& it: R) { // puts("ring"); // for(auto& jt: it) printf("%d ",jt); // puts(""); // } // puts("\n"); bool f = 1; for(int i=1; i<=N; i++) if(!fin[i]) { // printf("tree edge: %d -> %d\n", A[i], i); T[A[i]].push_back(i); f = 0; } if(f && (int)R.size() == 1) return !printf("0"); for(int i=1; i<=N; i++) if(fin[i]) dfs(i); for(auto& it: R) { long long mn = INF, s = 0; int pr = it.back(); for(int i=0; i<it.size(); i++) { s += min(C[pr], D[it[i]]); pr = it[i]; } for(int i=0; i<it.size(); i++) { s -= min(C[pr], D[it[i]]); mn = min(mn, s + C[pr]); s += min(C[pr], D[it[i]]); pr = it[i]; } ans += mn; } printf("%lld", ans); return 0; }

Compilation message (stderr)

telegraph.cpp: In function 'int main()':
telegraph.cpp:68:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0; i<it.size(); i++) {
                ~^~~~~~~~~~
telegraph.cpp:72:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0; i<it.size(); i++) {
                ~^~~~~~~~~~
telegraph.cpp:42:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&N);
  ~~~~~^~~~~~~~~
telegraph.cpp:43:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1; i<=N; i++) scanf("%d%d",&A[i],&C[i]);
                          ~~~~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...