답안 #471414

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
471414 2021-09-09T04:43:50 Z shrimb Mergers (JOI19_mergers) C++17
컴파일 오류
0 ms 0 KB
#include"bits/stdc++.h"
#define int long long
#define endl '\n'
#define USACO(x) ifstream cin ((string)#x + ".in"); ofstream cout((string)#x + ".out")
using namespace std;
int n, k;
vector<int> adj[500001];
int s[500001];
int tot[500001];
int subtree[500001];

map<int,int>* s2b[500001];
int cnt[5000001];
bool bad[5000001];

void inc (int a, int b, int v) {
    (*s2b[a])[b] += v;
    if ((*s2b[a])[b] == tot[b]) cnt[a]++;
}

int ans = INT_MAX;

void dfs (int cur, int par) {
    // cerr << cur << " " << par << endl;
    subtree[cur] = 1;
    if (adj[cur].size() == 1 and cur != 1) {
        s2b[cur] = new map<int,int>();
        inc(cur, s[cur], 1);
        ans = min(ans, (int)abs(cnt[cur] - (int)((*s2b[cur]).size())));
        bad[cur] = (cnt[cur] == (int)((*s2b[cur]).size()));
        return;
    }

    int best = -1;
    for (int i : adj[cur]) {
        if (i != par) {
            dfs(i, cur);
            subtree[cur] += subtree[i];
            if (best == -1 or subtree[i] > subtree[best]) best = i;
        }
    }

    s2b[cur] = s2b[best];
    cnt[cur] = cnt[best];

    for (int i : adj[cur]) {
        if (i != par and i != best) {
            for (auto j : (*s2b[i])) {
                inc(cur, j.first, j.second);
            }
            delete s2b[i];
        }
    }

    inc(cur, s[cur], 1);

    ans = min(ans, (int)abs(cnt[cur] - (int)((*s2b[cur]).size())));
    bad[cur] = (cnt[cur] == (int)((*s2b[cur]).size()));
}

int calc_ans (int cur, int par) {
    if (cur != 1 and bad[cur] == 1) return 1;
    int ret = 0;
    for (int i : adj[cur]) {
        if (i != par) {
            ret += calc_ans(i, cur);
        }
    }
    return ret;
}


signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> k;
    for (int i = 1 ; i < n ; i++) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    for (int i = 1 ; i <= n ; i++) {
        cin >> s[i];
        tot[s[i]]++;
    }
    dfs(1, 0);
    cout << max(calc_ans(1, 0) - 1, 0);


}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:90:38: error: no matching function for call to 'max(long long int, int)'
   90 |     cout << max(calc_ans(1, 0) - 1, 0);
      |                                      ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from mergers.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  254 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:254:5: note:   template argument deduction/substitution failed:
mergers.cpp:90:38: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   90 |     cout << max(calc_ans(1, 0) - 1, 0);
      |                                      ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from mergers.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  300 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algobase.h:300:5: note:   template argument deduction/substitution failed:
mergers.cpp:90:38: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   90 |     cout << max(calc_ans(1, 0) - 1, 0);
      |                                      ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from mergers.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
 3480 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3480:5: note:   template argument deduction/substitution failed:
mergers.cpp:90:38: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   90 |     cout << max(calc_ans(1, 0) - 1, 0);
      |                                      ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from mergers.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
 3486 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/10/bits/stl_algo.h:3486:5: note:   template argument deduction/substitution failed:
mergers.cpp:90:38: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   90 |     cout << max(calc_ans(1, 0) - 1, 0);
      |                                      ^