답안 #1058499

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1058499 2024-08-14T10:21:30 Z TheQuantiX 참나무 (IOI23_beechtree) C++17
31 / 100
69 ms 28012 KB
#include <bits/stdc++.h>
#include "beechtree.h"

using namespace std;
using ll = long long;

ll n, m, q, k, x, y, a, b, l;
vector<ll> G[200000];
vector<int> p, c;

bool check(vector<int> &v) {
    map<ll, ll> mp;
    vector<ll> v1(n);
    for (int i = 1; i < v.size(); i++) {
        v1[i] = mp[c[v[i]]];
        mp[c[v[i]]]++;
        // cout << p[v[i]] << ' ' << v1[i] << ' ' << v[v1[i]] << '\n';
        if (p[v[i]] != v[v1[i]]) {
            return 0;
        }
    }
    return 1;
}

bool comp(vector<int> &v, vector<int> &u) {
    return v.size() < u.size();
}

bool check2vec(vector<int> &v, vector<int> &u) {
    // for (auto i : v) {
    //     cout << i << ' ';
    // }
    // cout << '\n';
    // for (auto i : u) {
    //     cout << i << ' ';
    // }
    // cout << '\n';
    multiset<ll> st;
    for (int i : u) {
        st.insert(i);
    }
    for (int i : v) {
        if (!st.count(i)) {
            return 0;
        }
        st.erase(st.find(i));
    }
    return 1;
}

vector<int> beechtree(int N, int M, vector<int> P, vector<int> C) {
    n = N;
    m = M;
    p = P;
    c = C;
    bool testcase2 = 1;
    bool testcase3 = 1;
    for (int i = 1; i < n; i++) {
        G[P[i]].push_back(i);
        if (P[i] != i - 1) {
            testcase2 = 0;
        }
        if (i != 0 && P[i] != 0 && P[P[i]] != 0) {
            testcase3 = 0;
        }
    }
    if (n <= 8) {
        vector<int> ans(n);
        vector< vector<int> > has(n);
        for (int i = n - 1; i >= 0; i--) {
            // cout << i << '\n';
            has[i].push_back(i);
            for (int j : G[i]) {
                for (int k : has[j]) {
                    has[i].push_back(k);
                }
            }
            sort(has[i].begin(), has[i].end());
            bool fl = 0;
            do {
                // cout << has[i].size() << endl;
                if (has[i][0] != i) {
                    break;
                }
                fl |= check(has[i]);
                if (fl) {
                    break;
                }
            } while (next_permutation(has[i].begin(), has[i].end()));
            ans[i] = fl;
        }
        return ans;
    }
    if (testcase2) {
        vector<int> ans(n);
        ans[n - 1] = 1;
        ans[n - 2] = 1;
        for (int i = n - 3; i >= 0; i--) {
            ans[i] = (ans[i + 1] & (c[i + 1] == c[i + 2]));
        }
        return ans;
    }
    if (testcase3) {
        vector<int> ans(n);
        for (int i = n - 1; i >= 0; i--) {
            ans[i] = 1;
            set<ll> st;
            for (auto j : G[i]) {
                ans[i] &= ans[j];
                st.insert(c[j]);
            }
            if (st.size() != G[i].size()) {
                ans[i] = 0;
                continue;
            }
            if (i == 0) {
                vector< vector<int> > vec(1);
                for (int j : G[i]) {
                    vec[0].push_back(c[j]);
                }
                for (int j : G[i]) {
                    vec.resize(vec.size() + 1);
                    for (int k : G[j]) {
                        vec[vec.size() - 1].push_back(c[k]);
                    }
                    ans[i] &= check2vec(vec[vec.size() - 1], vec[0]);
                }
                // for (auto &j : vec) {
                //     sort(j.begin(), j.end());
                // }
                sort(vec.begin(), vec.end(), comp);
                for (int j = 0; j < vec.size() - 1; j++) {
                    ans[i] &= check2vec(vec[j], vec[j + 1]);
                }
            }
        }
        return ans;
    }
    vector<int> ans(n);
    vector< unordered_set<int> > has(n);
    for (int i = n - 1; i >= 0; i--) {
        ans[i] = 1;
        set<ll> st1;
        for (auto j : G[i]) {
            ans[i] &= ans[j];
            st1.insert(c[j]);
        }
        if (st1.size() != G[i].size()) {
            ans[i] = 0;
            continue;
        }
        // cout << i << '\n';
        for (int j : G[i]) {
            if (has[j].size() > has[i].size()) {
                swap(has[j], has[i]);
            }
            if (has[j].size() > 0 && has[i].size() > 0) {
                ans[i] = 0;
            }
            for (int k : has[j]) {
                if (has[i].count(k)) {
                    ans[i] = 0;
                }
                has[i].insert(k);
            }
        }
        unordered_set<int> st;
        for (int j : G[i]) {
            if (has[i].count(c[j])) {
                has[i].erase(c[j]);
            }
            else {
                st.insert(c[j]);
            }
        }
        if (!has[i].empty()) {
            ans[i] = 0;
        }
        has[i] = st;
    }
    return ans;
}

Compilation message

beechtree.cpp: In function 'bool check(std::vector<int>&)':
beechtree.cpp:14:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |     for (int i = 1; i < v.size(); i++) {
      |                     ~~^~~~~~~~~~
beechtree.cpp: In function 'std::vector<int> beechtree(int, int, std::vector<int>, std::vector<int>)':
beechtree.cpp:132:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  132 |                 for (int j = 0; j < vec.size() - 1; j++) {
      |                                 ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Incorrect 1 ms 4956 KB 2nd lines differ - on the 2nd token, expected: '1', found: '0'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4952 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Correct 1 ms 4956 KB Output is correct
16 Correct 1 ms 5124 KB Output is correct
17 Correct 1 ms 4956 KB Output is correct
18 Correct 1 ms 4952 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 1 ms 4956 KB Output is correct
21 Correct 1 ms 4956 KB Output is correct
22 Correct 1 ms 4952 KB Output is correct
23 Correct 1 ms 4956 KB Output is correct
24 Correct 1 ms 4956 KB Output is correct
25 Correct 1 ms 4956 KB Output is correct
26 Correct 1 ms 4956 KB Output is correct
27 Correct 1 ms 4956 KB Output is correct
28 Correct 1 ms 4956 KB Output is correct
29 Correct 1 ms 4956 KB Output is correct
30 Correct 1 ms 4956 KB Output is correct
31 Correct 1 ms 4952 KB Output is correct
32 Correct 1 ms 4956 KB Output is correct
33 Correct 1 ms 4956 KB Output is correct
34 Correct 1 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 39 ms 16700 KB Output is correct
8 Correct 43 ms 16724 KB Output is correct
9 Correct 1 ms 4952 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4968 KB Output is correct
13 Correct 1 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 1 ms 5212 KB Output is correct
16 Correct 1 ms 5212 KB Output is correct
17 Correct 37 ms 16700 KB Output is correct
18 Correct 38 ms 16684 KB Output is correct
19 Correct 39 ms 16720 KB Output is correct
20 Correct 41 ms 16732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 2 ms 4952 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Correct 52 ms 9672 KB Output is correct
16 Correct 47 ms 9484 KB Output is correct
17 Correct 50 ms 9420 KB Output is correct
18 Correct 59 ms 9552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 39 ms 16700 KB Output is correct
6 Correct 43 ms 16724 KB Output is correct
7 Correct 1 ms 4952 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 1 ms 5212 KB Output is correct
11 Correct 59 ms 28012 KB Output is correct
12 Correct 69 ms 25168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Incorrect 1 ms 4956 KB 2nd lines differ - on the 2nd token, expected: '1', found: '0'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4952 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 5124 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4952 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 1 ms 4956 KB Output is correct
17 Correct 1 ms 4956 KB Output is correct
18 Correct 1 ms 4952 KB Output is correct
19 Correct 1 ms 4956 KB Output is correct
20 Correct 1 ms 4956 KB Output is correct
21 Correct 1 ms 4956 KB Output is correct
22 Correct 1 ms 4956 KB Output is correct
23 Correct 1 ms 4956 KB Output is correct
24 Correct 1 ms 4956 KB Output is correct
25 Correct 1 ms 5212 KB Output is correct
26 Correct 2 ms 5212 KB Output is correct
27 Correct 1 ms 5212 KB Output is correct
28 Correct 2 ms 5208 KB Output is correct
29 Correct 1 ms 5208 KB Output is correct
30 Incorrect 1 ms 5208 KB 2nd lines differ - on the 802nd token, expected: '1', found: '0'
31 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Incorrect 1 ms 4956 KB 2nd lines differ - on the 2nd token, expected: '1', found: '0'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4952 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 5124 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4952 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 1 ms 4956 KB Output is correct
17 Correct 1 ms 4956 KB Output is correct
18 Correct 1 ms 4952 KB Output is correct
19 Correct 1 ms 4956 KB Output is correct
20 Correct 1 ms 4956 KB Output is correct
21 Correct 1 ms 4956 KB Output is correct
22 Correct 1 ms 4956 KB Output is correct
23 Correct 1 ms 4956 KB Output is correct
24 Correct 1 ms 4956 KB Output is correct
25 Correct 1 ms 5212 KB Output is correct
26 Correct 2 ms 5212 KB Output is correct
27 Correct 1 ms 5212 KB Output is correct
28 Correct 2 ms 5208 KB Output is correct
29 Correct 1 ms 5208 KB Output is correct
30 Incorrect 1 ms 5208 KB 2nd lines differ - on the 802nd token, expected: '1', found: '0'
31 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Incorrect 1 ms 4956 KB 2nd lines differ - on the 2nd token, expected: '1', found: '0'
3 Halted 0 ms 0 KB -