제출 #219140

#제출 시각아이디문제언어결과실행 시간메모리
219140peuchIslands (IOI08_islands)C++17
18 / 100
1192 ms131076 KiB
#include<bits/stdc++.h> using namespace std; const int MAXN = 1000010; int n; int rep; int in[MAXN]; long long prof[MAXN], dist[MAXN]; vector<int> ar[MAXN], wg[MAXN]; bool isCycle[MAXN], marc[MAXN]; long long ans; void getCycle(); int dfs1(int cur){ marc[cur] = 1; int ret = cur; for(int i = 0; i < ar[cur].size(); i++){ int viz = ar[cur][i]; if(in[viz]) continue; if(marc[viz]) continue; dist[viz] = dist[cur] + wg[cur][i]; int aux = dfs1(viz); if(dist[aux] > dist[ret]) ret = aux; } return ret; } long long dfs2(int cur){ marc[cur] = 0; long long ret = dist[cur]; for(int i = 0; i < ar[cur].size(); i++){ int viz = ar[cur][i]; if(isCycle[viz] && viz != rep) continue; if(!marc[viz]) continue; dist[viz] = dist[cur] + wg[cur][i]; ret = max(ret, dfs2(viz)); } return ret; } int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++){ int aux1, aux2; scanf("%d %d", &aux1, &aux2); in[aux1]++; ar[aux1].push_back(i); ar[i].push_back(aux1); wg[i].push_back(aux2); wg[aux1].push_back(aux2); } getCycle(); printf("%lld\n", ans); } void getCycle(){ int fila[n]; int fim = 0; int ini = 0; for(int i = 1; i <= n; i++) if(in[i] == 0) fila[fim++] = i; while(ini != fim){ int cur = fila[ini++]; for(int j = 0; j < ar[cur].size(); j++){ int viz = ar[cur][j]; if(in[viz] == 0) continue; in[viz]--; prof[viz] = max(prof[viz], prof[cur] + wg[cur][j]); if(in[viz] == 0) fila[fim++] = viz; } } for(int i = 1; i <= n; i++){ if(!in[i] || isCycle[i]) continue; int cur = i; dist[cur] = 0; int cnt = 0; int last = 0; long long aux = 0; long long sct = 0; while(1){ last = cur; cnt++; isCycle[cur] = 1; rep = cur; long long auxDist = dist[cur]; dist[cur] = 0; int x = dfs1(cur); dist[x] = 0; aux = max(aux, dfs2(x)); dist[cur] = auxDist; bool flag = true; for(int j = 0; j < ar[cur].size(); j++){ int viz = ar[cur][j]; if(!in[viz] || isCycle[viz]) continue; dist[viz] = dist[cur] + wg[cur][j]; sct += wg[cur][i]; cur = viz; flag = false; break; } if(flag) break; } if(cnt == 1){ ans += aux; continue; } if(cnt == 2){ sct = 0; for(int j = 0; j < ar[i].size(); j++) if(isCycle[ar[i][j]]) sct += wg[i][j]; } else{ for(int j = 0; j < ar[cur].size(); j++) if(ar[cur][j] == i) sct += wg[i][j]; } long long aux2 = prof[cur] + dist[cur]; long long aux3 = prof[cur] - dist[cur]; if(cnt == 2) cur = i; else{ for(int j = 0; j < ar[cur].size(); j++) { int viz = ar[cur][j]; if(!isCycle[viz]) continue; if(dist[viz] > dist[cur] || viz == i) continue; cur = viz; break; } } while(1){ aux = max(aux, aux2 + prof[cur] - dist[cur]); aux = max(aux, aux3 + sct + prof[cur] + dist[cur]); aux2 = max(aux2, prof[cur] + dist[cur]); aux3 = max(aux3, prof[cur] - dist[cur]); bool flag = true; for(int j = 0; j < ar[cur].size(); j++) { int viz = ar[cur][j]; if(!isCycle[viz]) continue; if(dist[viz] > dist[cur]) continue; cur = viz; flag = false; break; } if(flag) break; } if(cnt == 2) cur = last; else{ for(int j = 0; j < ar[cur].size(); j++) { int viz = ar[cur][j]; if(!isCycle[viz]) continue; if(dist[viz] < dist[cur] || viz == last) continue; cur = viz; break; } } aux2 = prof[i] - dist[i]; aux3 = prof[i] + dist[i]; while(1){ if(cur == i) break; aux = max(aux, aux2 + prof[cur] + dist[cur]); aux = max(aux, aux3 + sct + prof[cur] - dist[cur]); aux2 = max(aux2, prof[cur] - dist[cur]); aux3 = max(aux3, prof[cur] + dist[cur]); bool flag = true; for(int j = 0; j < ar[cur].size(); j++) { int viz = ar[cur][j]; if(!isCycle[viz]) continue; if(dist[viz] < dist[cur]) continue; cur = viz; flag = false; break; } if(flag) break; } ans += aux; } }

컴파일 시 표준 에러 (stderr) 메시지

islands.cpp: In function 'int dfs1(int)':
islands.cpp:19:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < ar[cur].size(); i++){
                 ~~^~~~~~~~~~~~~~~~
islands.cpp: In function 'long long int dfs2(int)':
islands.cpp:33:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < ar[cur].size(); i++){
                 ~~^~~~~~~~~~~~~~~~
islands.cpp: In function 'void getCycle()':
islands.cpp:66:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j = 0; j < ar[cur].size(); j++){
                  ~~^~~~~~~~~~~~~~~~
islands.cpp:94:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j = 0; j < ar[cur].size(); j++){
                   ~~^~~~~~~~~~~~~~~~
islands.cpp:111:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j = 0; j < ar[i].size(); j++) 
                   ~~^~~~~~~~~~~~~~
islands.cpp:115:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j = 0; j < ar[cur].size(); j++) 
                   ~~^~~~~~~~~~~~~~~~
islands.cpp:122:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j = 0; j < ar[cur].size(); j++) {
                   ~~^~~~~~~~~~~~~~~~
islands.cpp:136:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j = 0; j < ar[cur].size(); j++) {
                   ~~^~~~~~~~~~~~~~~~
islands.cpp:148:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j = 0; j < ar[cur].size(); j++) {
                   ~~^~~~~~~~~~~~~~~~
islands.cpp:165:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j = 0; j < ar[cur].size(); j++) {
                   ~~^~~~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:47:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &aux1, &aux2);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#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...