Submission #396610

# Submission time Handle Problem Language Result Execution time Memory
396610 2021-04-30T12:35:18 Z peuch Islands (IOI08_islands) C++17
60 / 100
65 ms 30488 KB
#include<bits/stdc++.h>
using namespace std;
 
const int MAXN = 100001;
 
int n;
int ar[MAXN], wg[MAXN], in[MAXN], rep;
int op[MAXN];
long long prof[MAXN], dist[MAXN];
bool marc[MAXN], isCycle[MAXN];
long long ans;
 
vector<int> rev[MAXN], rwg[MAXN];
 
int dfs1(int cur);
long long dfs2(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 dfs1(int cur){
	marc[cur] = 1;
	int ret = cur;
	for(int i = 0; i < rev[cur].size(); i++){
		int viz = rev[cur][i];
		if(in[viz]) continue;
		if(marc[viz]) continue;
		dist[viz] = dist[cur] + rwg[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 < rev[cur].size(); i++){
		int viz = rev[cur][i];
		if(isCycle[viz] && rep != viz) continue;
		if(!marc[viz]) continue;
		dist[viz] = dist[cur] + rwg[cur][i];
		ret = max(ret, dfs2(viz));
	}
	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;
		dist[cur] = 0;
		long long aux = 0;
		long long sct = 0;
		while(1) {
			isCycle[cur] = 1;
			rep = cur;
			long long auxDist = dist[cur];
			dist[cur] = 0;
			int x = dfs1(cur);
			dist[x] = 0;
			long long y = dfs2(x);
			aux = max(aux, y);
			dist[cur] = auxDist;
			sct += wg[cur];
			op[ar[cur]] = cur;
			if(ar[cur] == i) break;
			dist[ar[cur]] = dist[cur] + wg[cur];
			cur = ar[cur];
		} 
		cur = ar[i];
		long long aux2 = prof[i] - dist[i];
		long long 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]);
			cur = ar[cur];
		}
		cur = op[op[i]];
		aux2 = prof[ar[cur]] + dist[ar[cur]];
		aux3 = prof[ar[cur]] - dist[ar[cur]];
		while(1){
			if(cur == op[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]);
			cur = op[cur];
		}
		ans += aux;
	}
}

Compilation message

islands.cpp: In function 'int dfs1(int)':
islands.cpp:36:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |  for(int i = 0; i < rev[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~~
islands.cpp: In function 'long long int dfs2(int)':
islands.cpp:50:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |  for(int i = 0; i < rev[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:20:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   20 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
islands.cpp:22:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   22 |   scanf("%d %d", &ar[i], &wg[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 5008 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 5008 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 4 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5140 KB Output is correct
2 Correct 5 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5268 KB Output is correct
2 Correct 6 ms 5452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 6604 KB Output is correct
2 Correct 28 ms 8832 KB Output is correct
3 Correct 20 ms 7368 KB Output is correct
4 Correct 10 ms 6164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 10468 KB Output is correct
2 Correct 52 ms 15760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 62 ms 26724 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 65 ms 30488 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 9 ms 9932 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 24 ms 14544 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -