답안 #134583

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
134583 2019-07-23T04:44:41 Z 송준혁(#3242) Telegraph (JOI16_telegraph) C++14
0 / 100
4 ms 2808 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

int N;
LL ans, sum, S;
int P[101010], V[101010], chk[101010], C;
bool vis[101010];
vector<pii> adj[101010];

int FindCyc(int u){
	if (chk[u]) return chk[u];
	chk[u] = ++C, V[C] = u;
	FindCyc(P[u]);
}

void dfs(int u, int r){
	int Max = 0;
	vis[u] = true;
	for (pii v : adj[u]){
		if (v.first == r) continue;
		Max = max(Max, v.second);
		dfs(v.first, r);
	}
	S += Max;
}

int main(){
	scanf("%d", &N);
	for (int i=1; i<=N; i++){
		int x;
		scanf("%d %d", &P[i], &x);
		adj[P[i]].push_back(pii(i, x));
		sum += x;
	}
	for (int i=1; i<=N; i++){
		if (vis[i]) continue;
		C = 0, ans = 0, S = 0;
		int R = FindCyc(i);
		if (C == N){
			puts("0");
			return 0;
		}
		dfs(V[R], V[R]);
		ans = S, V[R-1] = V[C];
		for (R++; R<=C; R++){
			int pmax=0, nmax=0;
			for (pii v : adj[V[R-1]]){
				if (v.first != V[R]) nmax = max(nmax, v.second);
				pmax = max(pmax, v.second);
			}
			S += nmax - pmax, pmax = nmax = 0;

			for (pii v : adj[V[R-2]]){
				if (v.first != V[R-1]) pmax = max(pmax, v.second);
				nmax = max(nmax, v.second);
			}
			S += nmax - pmax;
			ans = max(ans, S);
		}
		sum -= ans;
	}
	printf("%lld\n", sum);
	return 0;
}

Compilation message

telegraph.cpp: In function 'int FindCyc(int)':
telegraph.cpp:16:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
telegraph.cpp: In function 'int main()':
telegraph.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
telegraph.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &P[i], &x);
   ~~~~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2808 KB Output isn't correct
2 Halted 0 ms 0 KB -