Submission #219144

# Submission time Handle Problem Language Result Execution time Memory
219144 2020-04-03T20:52:42 Z peuch Islands (IOI08_islands) C++17
Compilation error
0 ms 0 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];
int prof[MAXN], sc[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;
		pai[viz] = pai[cur];
		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] && pai[cur] != 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;
		sc[cur] = 0;
		long long aux = 0;
		long long sct = 0;
		while(1) {
			isCycle[cur] = 1;
			pai[cur] = cur;
			dist[cur] = 0;
			int x = dfs1(cur);
			dist[x] = 0;
			long long y = dfs2(x);
			aux = max(aux, y);
			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 dfs1(int)':
islands.cpp:36:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < rev[cur].size(); i++){
                 ~~^~~~~~~~~~~~~~~~~
islands.cpp: In function 'long long int dfs2(int)':
islands.cpp:51:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < rev[cur].size(); i++){
                 ~~^~~~~~~~~~~~~~~~~
islands.cpp: In function 'void getCycle()':
islands.cpp:101:40: error: no matching function for call to 'max(long long int&, int)'
    aux2 = max(aux2, prof[cur] - sc[cur]);
                                        ^
In file included from /usr/include/c++/7/bits/specfun.h:45:0,
                 from /usr/include/c++/7/cmath:1914,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:41,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h:219:5: note: candidate: template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)
     max(const _Tp& __a, const _Tp& __b)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:219:5: note:   template argument deduction/substitution failed:
islands.cpp:101:40: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
    aux2 = max(aux2, prof[cur] - sc[cur]);
                                        ^
In file included from /usr/include/c++/7/bits/specfun.h:45:0,
                 from /usr/include/c++/7/cmath:1914,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:41,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h:265:5: note: candidate: template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)
     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:265:5: note:   template argument deduction/substitution failed:
islands.cpp:101:40: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
    aux2 = max(aux2, prof[cur] - sc[cur]);
                                        ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algo.h:3462:5: note: candidate: template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)
     max(initializer_list<_Tp> __l)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3462:5: note:   template argument deduction/substitution failed:
islands.cpp:101:40: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
    aux2 = max(aux2, prof[cur] - sc[cur]);
                                        ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algo.h:3468:5: note: candidate: template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)
     max(initializer_list<_Tp> __l, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3468:5: note:   template argument deduction/substitution failed:
islands.cpp:101:40: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
    aux2 = max(aux2, prof[cur] - sc[cur]);
                                        ^
islands.cpp:102:40: error: no matching function for call to 'max(long long int&, int)'
    aux3 = max(aux3, prof[cur] + sc[cur]);
                                        ^
In file included from /usr/include/c++/7/bits/specfun.h:45:0,
                 from /usr/include/c++/7/cmath:1914,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:41,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h:219:5: note: candidate: template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)
     max(const _Tp& __a, const _Tp& __b)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:219:5: note:   template argument deduction/substitution failed:
islands.cpp:102:40: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
    aux3 = max(aux3, prof[cur] + sc[cur]);
                                        ^
In file included from /usr/include/c++/7/bits/specfun.h:45:0,
                 from /usr/include/c++/7/cmath:1914,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:41,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h:265:5: note: candidate: template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)
     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:265:5: note:   template argument deduction/substitution failed:
islands.cpp:102:40: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
    aux3 = max(aux3, prof[cur] + sc[cur]);
                                        ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algo.h:3462:5: note: candidate: template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)
     max(initializer_list<_Tp> __l)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3462:5: note:   template argument deduction/substitution failed:
islands.cpp:102:40: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
    aux3 = max(aux3, prof[cur] + sc[cur]);
                                        ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algo.h:3468:5: note: candidate: template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)
     max(initializer_list<_Tp> __l, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3468:5: note:   template argument deduction/substitution failed:
islands.cpp:102:40: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
    aux3 = max(aux3, prof[cur] + sc[cur]);
                                        ^
islands.cpp:112:40: error: no matching function for call to 'max(long long int&, int)'
    aux2 = max(aux2, prof[cur] + sc[cur]);
                                        ^
In file included from /usr/include/c++/7/bits/specfun.h:45:0,
                 from /usr/include/c++/7/cmath:1914,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:41,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h:219:5: note: candidate: template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)
     max(const _Tp& __a, const _Tp& __b)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:219:5: note:   template argument deduction/substitution failed:
islands.cpp:112:40: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
    aux2 = max(aux2, prof[cur] + sc[cur]);
                                        ^
In file included from /usr/include/c++/7/bits/specfun.h:45:0,
                 from /usr/include/c++/7/cmath:1914,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:41,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h:265:5: note: candidate: template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)
     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:265:5: note:   template argument deduction/substitution failed:
islands.cpp:112:40: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
    aux2 = max(aux2, prof[cur] + sc[cur]);
                                        ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algo.h:3462:5: note: candidate: template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)
     max(initializer_list<_Tp> __l)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3462:5: note:   template argument deduction/substitution failed:
islands.cpp:112:40: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
    aux2 = max(aux2, prof[cur] + sc[cur]);
                                        ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algo.h:3468:5: note: candidate: template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)
     max(initializer_list<_Tp> __l, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3468:5: note:   template argument deduction/substitution failed:
islands.cpp:112:40: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
    aux2 = max(aux2, prof[cur] + sc[cur]);
                                        ^
islands.cpp:113:40: error: no matching function for call to 'max(long long int&, int)'
    aux3 = max(aux3, prof[cur] - sc[cur]);
                                        ^
In file included from /usr/include/c++/7/bits/specfun.h:45:0,
                 from /usr/include/c++/7/cmath:1914,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:41,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h:219:5: note: candidate: template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)
     max(const _Tp& __a, const _Tp& __b)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:219:5: note:   template argument deduction/substitution failed:
islands.cpp:113:40: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
    aux3 = max(aux3, prof[cur] - sc[cur]);
                                        ^
In file included from /usr/include/c++/7/bits/specfun.h:45:0,
                 from /usr/include/c++/7/cmath:1914,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:41,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algobase.h:265:5: note: candidate: template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)
     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:265:5: note:   template argument deduction/substitution failed:
islands.cpp:113:40: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
    aux3 = max(aux3, prof[cur] - sc[cur]);
                                        ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algo.h:3462:5: note: candidate: template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)
     max(initializer_list<_Tp> __l)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3462:5: note:   template argument deduction/substitution failed:
islands.cpp:113:40: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
    aux3 = max(aux3, prof[cur] - sc[cur]);
                                        ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:1:
/usr/include/c++/7/bits/stl_algo.h:3468:5: note: candidate: template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)
     max(initializer_list<_Tp> __l, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3468:5: note:   template argument deduction/substitution failed:
islands.cpp:113:40: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
    aux3 = max(aux3, prof[cur] - sc[cur]);
                                        ^
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]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
islands.cpp:22: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]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~