Submission #356207

# Submission time Handle Problem Language Result Execution time Memory
356207 2021-01-23T08:11:16 Z achibasadzishvili Islands (IOI08_islands) C++14
Compilation error
0 ms 0 KB
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define N 1000002
using namespace std;
ll n,p[N],l[N],fix[N],F[N],ans,pas,k,c[N],mx[N];
vector<int>v[N];
map<int,int>M[N],ra[N];
vector<int>g,all;
void dfs(ll x){
    if(F[x])return;
    all.pb(x);
    F[x] = 1;
    for(int i=0; i<v[x].size(); i++)
        dfs(v[x][i]);
}
void findc(ll x,ll par){
    if(fix[x]){
        g.pb(x);
        k = 1;
        return;
    }
    g.pb(x);
    fix[x] = 1;
    for(int i=0; i<v[x].size(); i++)
        if(v[x][i] != par || ra[x][v[x][i]] > 1){
            findc(v[x][i] , x);
            if(k)return;
        }
    g.pop_back();
}
void findmx(ll x,ll par){
    ll mx1 = 0,mx2 = 0;
    for(int i=0; i<v[x].size(); i++){
        if(v[x][i] != par && !c[v[x][i]]){
            findmx(v[x][i] , x);
            mx[x] = max(mx[x] , mx[v[x][i]] + M[x][v[x][i]]);
            if(mx[v[x][i]] + M[x][v[x][i]] > mx1){
                mx2 = mx1;
                mx1 = mx[v[x][i]] + M[x][v[x][i]];
            }
            else if(mx[v[x][i]] + M[x][v[x][i]] > mx2)
                    mx2 = mx[v[x][i]] + M[x][v[x][i]];
        }
    }
    ans = max(ans , mx1 + mx2);
}
int main(){
    ios::sync_with_stdio(false);
    cin >> n;
    
    for(int i=1; i<=n; i++){
        cin >> p[i] >> l[i];
        M[i][p[i]] = max(M[i][p[i]] , l[i]);
        M[p[i]][i] = max(M[p[i]][i] , l[i]);
        ra[i][p[i]]++;
        ra[p[i]][i]++;
        v[p[i]].pb(i);
        v[i].pb(p[i]);
    }
    
    for(int s=1; s<=n; s++){
        if(F[s])continue;
        ans = 0;
        k = 0;
        all.clear();
        dfs(s);
        g.clear();
        findc(s , 0);
        vector<int>cyc;
        for(int i=0; i<g.size(); i++){
            if(g[i] == g[(int)g.size() - 1]){
                for(int j=i+1; j<g.size(); j++){
                    cyc.pb(g[j]);
                    c[g[j]] = 1;
                }
                break;
            }
        }
        ll len = 0;
        for(int i=0; i<cyc.size(); i++){
            findmx(cyc[i] , 0);
            if(i)len += M[cyc[i]][cyc[i - 1]];
        }
        len += M[cyc[0]][cyc[cyc.size() - 1]];
        ll cur = mx[cyc[0]];
        ll cur2 = mx[cyc[0]];
        ll pre = 0;
        for(int i=1; i<cyc.size(); i++){
            pre += M[cyc[i]][cyc[i - 1]];
            ans = max(ans , cur + mx[cyc[i]] + pre);
            ans = max(ans , cur2 + len + mx[cyc[i]] - pre);
            cur = max(cur , mx[cyc[i]] - pre);
            cur2 = max(cur2 , mx[cyc[i]] + pre);
        }
        pas += ans;
    }
    
    cout << pas << '\n';
    
    return 0;
}

Compilation message

islands.cpp: In function 'void dfs(long long int)':
islands.cpp:16:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for(int i=0; i<v[x].size(); i++)
      |                  ~^~~~~~~~~~~~
islands.cpp: In function 'void findc(long long int, long long int)':
islands.cpp:27:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |     for(int i=0; i<v[x].size(); i++)
      |                  ~^~~~~~~~~~~~
islands.cpp: In function 'void findmx(long long int, long long 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<v[x].size(); i++){
      |                  ~^~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:56:43: error: no matching function for call to 'max(std::map<int, int>::mapped_type&, long long int&)'
   56 |         M[i][p[i]] = max(M[i][p[i]] , l[i]);
      |                                           ^
In file included from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/istream:38,
                 from /usr/include/c++/9/sstream:38,
                 from /usr/include/c++/9/complex:45,
                 from /usr/include/c++/9/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:54,
                 from islands.cpp:1:
/usr/include/c++/9/bits/stl_algobase.h:222:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  222 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/9/bits/stl_algobase.h:222:5: note:   template argument deduction/substitution failed:
islands.cpp:56:43: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   56 |         M[i][p[i]] = max(M[i][p[i]] , l[i]);
      |                                           ^
In file included from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/istream:38,
                 from /usr/include/c++/9/sstream:38,
                 from /usr/include/c++/9/complex:45,
                 from /usr/include/c++/9/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:54,
                 from islands.cpp:1:
/usr/include/c++/9/bits/stl_algobase.h:268:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  268 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/9/bits/stl_algobase.h:268:5: note:   template argument deduction/substitution failed:
islands.cpp:56:43: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   56 |         M[i][p[i]] = max(M[i][p[i]] , l[i]);
      |                                           ^
In file included from /usr/include/c++/9/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:65,
                 from islands.cpp:1:
/usr/include/c++/9/bits/stl_algo.h:3456:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
 3456 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/9/bits/stl_algo.h:3456:5: note:   template argument deduction/substitution failed:
islands.cpp:56:43: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   56 |         M[i][p[i]] = max(M[i][p[i]] , l[i]);
      |                                           ^
In file included from /usr/include/c++/9/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:65,
                 from islands.cpp:1:
/usr/include/c++/9/bits/stl_algo.h:3462:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
 3462 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/9/bits/stl_algo.h:3462:5: note:   template argument deduction/substitution failed:
islands.cpp:56:43: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   56 |         M[i][p[i]] = max(M[i][p[i]] , l[i]);
      |                                           ^
islands.cpp:57:43: error: no matching function for call to 'max(std::map<int, int>::mapped_type&, long long int&)'
   57 |         M[p[i]][i] = max(M[p[i]][i] , l[i]);
      |                                           ^
In file included from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/istream:38,
                 from /usr/include/c++/9/sstream:38,
                 from /usr/include/c++/9/complex:45,
                 from /usr/include/c++/9/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:54,
                 from islands.cpp:1:
/usr/include/c++/9/bits/stl_algobase.h:222:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  222 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/9/bits/stl_algobase.h:222:5: note:   template argument deduction/substitution failed:
islands.cpp:57:43: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   57 |         M[p[i]][i] = max(M[p[i]][i] , l[i]);
      |                                           ^
In file included from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/istream:38,
                 from /usr/include/c++/9/sstream:38,
                 from /usr/include/c++/9/complex:45,
                 from /usr/include/c++/9/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:54,
                 from islands.cpp:1:
/usr/include/c++/9/bits/stl_algobase.h:268:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  268 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/9/bits/stl_algobase.h:268:5: note:   template argument deduction/substitution failed:
islands.cpp:57:43: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   57 |         M[p[i]][i] = max(M[p[i]][i] , l[i]);
      |                                           ^
In file included from /usr/include/c++/9/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:65,
                 from islands.cpp:1:
/usr/include/c++/9/bits/stl_algo.h:3456:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
 3456 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/9/bits/stl_algo.h:3456:5: note:   template argument deduction/substitution failed:
islands.cpp:57:43: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   57 |         M[p[i]][i] = max(M[p[i]][i] , l[i]);
      |                                           ^
In file included from /usr/include/c++/9/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:65,
                 from islands.cpp:1:
/usr/include/c++/9/bits/stl_algo.h:3462:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
 3462 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/9/bits/stl_algo.h:3462:5: note:   template argument deduction/substitution failed:
islands.cpp:57:43: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   57 |         M[p[i]][i] = max(M[p[i]][i] , l[i]);
      |                                           ^
islands.cpp:73:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |         for(int i=0; i<g.size(); i++){
      |                      ~^~~~~~~~~
islands.cpp:75:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |                 for(int j=i+1; j<g.size(); j++){
      |                                ~^~~~~~~~~
islands.cpp:83:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for(int i=0; i<cyc.size(); i++){
      |                      ~^~~~~~~~~~~
islands.cpp:91:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |         for(int i=1; i<cyc.size(); i++){
      |                      ~^~~~~~~~~~~