답안 #219841

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
219841 2020-04-06T14:04:09 Z peuch Islands (IOI08_islands) C++17
54 / 100
577 ms 131076 KB
#include<bits/stdc++.h>
using namespace std;
 
const int MAXN = 1000001;
 
int n;
int ar[MAXN], wg[MAXN], in[MAXN], pai[MAXN];
int op[MAXN];
long long prof[MAXN], sc[MAXN], dist[MAXN];
bool marc[MAXN], isCycle[MAXN];
long long ans;
 
vector<int> rev[MAXN], rwg[MAXN];
 
int dfs(int cur);
void getCycle();
 
int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d %d", &ar[i], &wg[i]);
		rev[ar[i]].push_back(i);
		rev[i].push_back(ar[i]);
		rwg[ar[i]].push_back(wg[i]);
		rwg[i].push_back(wg[i]);
		in[ar[i]]++;
	}
	getCycle();
	printf("%lld\n", ans);
}
 
int dfs(int cur){
	marc[cur] = 1;
	int ret = cur;
	for(int i = 0; i < rev[cur].size(); i++){
		int viz = rev[cur][i];
		if(isCycle[viz] && pai[cur] != viz) continue;
		if(marc[viz]) continue;
		pai[viz] = pai[cur];
		dist[viz] = dist[cur] + rwg[cur][i];
		int aux = dfs(viz);
		if(dist[aux] > dist[ret]) ret = aux;
	}
	return ret;
}
 
void getCycle(){
	int fila[n];
	int ini = 0;
	int fim = 0;
	for(int i = 1; i <= n; i++)
		if(in[i] == 0) fila[fim++] = i;
	while(ini != fim){
		int cur = fila[ini++];
		int viz = ar[cur];
		in[viz]--;
		prof[viz] = max(prof[viz], prof[cur] + wg[cur]);
		if(in[viz] == 0) fila[fim++] = viz;
	}
	for(int i = 1; i <= n; i++){
		if(in[i] == 0 || isCycle[i]) continue;
		int cur = i;
		sc[cur] = 0;
		long long aux = 0;
		long long sct = 0;
		while(1) {
			isCycle[cur] = 1;
			pai[cur] = cur;
			dist[cur] = 0;
			int x = dfs(cur);
			dist[x] = 0;
			aux = max(aux, dist[dfs(x)]);
			sct += wg[cur];
			op[ar[cur]] = cur;
			if(ar[cur] == i) break;
			sc[ar[cur]] = sc[cur] + wg[cur];
			cur = ar[cur];
		} 
		cur = ar[i];
		long long aux2 = prof[i] - sc[i];
		long long aux3 = prof[i] + sc[i];
		while(1){
			if(cur == i) break;
			aux = max(aux, aux2 + prof[cur] + sc[cur]);
			aux = max(aux, aux3 + sct + prof[cur] - sc[cur]);
			aux2 = max(aux2, prof[cur] - sc[cur]);
			aux3 = max(aux3, prof[cur] + sc[cur]);
			cur = ar[cur];
		}
		cur = op[op[i]];
		aux2 = prof[ar[cur]] + sc[ar[cur]];
		aux3 = prof[ar[cur]] - sc[ar[cur]];
		while(1){
			if(cur == op[i]) break;
			aux = max(aux, aux2 + prof[cur] - sc[cur]);
			aux = max(aux, aux3 + sct + prof[cur] + sc[cur]);
			aux2 = max(aux2, prof[cur] + sc[cur]);
			aux3 = max(aux3, prof[cur] - sc[cur]);
			cur = op[cur];
		}
		ans += aux;
	}
}

Compilation message

islands.cpp: In function 'int dfs(int)':
islands.cpp:35:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < rev[cur].size(); i++){
                 ~~^~~~~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:19:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:21:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &ar[i], &wg[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 47352 KB Output is correct
2 Incorrect 32 ms 47360 KB Output isn't correct
3 Correct 32 ms 47360 KB Output is correct
4 Correct 32 ms 47360 KB Output is correct
5 Correct 33 ms 47352 KB Output is correct
6 Correct 32 ms 47360 KB Output is correct
7 Correct 32 ms 47360 KB Output is correct
8 Correct 32 ms 47360 KB Output is correct
9 Correct 32 ms 47360 KB Output is correct
10 Incorrect 32 ms 47360 KB Output isn't correct
11 Correct 37 ms 47608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 47488 KB Output is correct
2 Correct 33 ms 47488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 47616 KB Output is correct
2 Correct 35 ms 47872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 49152 KB Output is correct
2 Correct 61 ms 51964 KB Output is correct
3 Correct 48 ms 49656 KB Output is correct
4 Incorrect 41 ms 48508 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 53752 KB Output is correct
2 Correct 89 ms 58872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 166 ms 71032 KB Output is correct
2 Correct 167 ms 73980 KB Output is correct
3 Correct 196 ms 81696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 271 ms 91640 KB Output is correct
2 Correct 346 ms 111864 KB Output is correct
3 Correct 357 ms 119544 KB Output is correct
4 Runtime error 413 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Runtime error 473 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 577 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -