QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#817304#3561. Capital Citytopfloorboss#0 238ms51572kbC++203.5kb2024-12-16 21:33:442024-12-16 21:33:46

Judging History

你现在查看的是最新测评结果

  • [2024-12-16 21:33:46]
  • 评测
  • 测评结果:0
  • 用时:238ms
  • 内存:51572kb
  • [2024-12-16 21:33:44]
  • 提交

answer

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <random>
#include <cassert>

using namespace std;

#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

typedef long long ll;
typedef long double ld;

mt19937_64 rnd(1329);

void solve() {
    int n, k;
    cin >> n >> k;
    if (n == 1 || k == 1) {
        cout << "0\n";
        return;
    }
    vector<vector<int>> gr(n);
    for (int i = 0; i < n - 1; ++i) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        gr[a].push_back(b);
        gr[b].push_back(a);
    }
    vector<int> ord;
    for (int i = 0; i < n; ++i) {
        if (gr[i].size() != 1) continue;
        ord.push_back(i);
        ord.push_back(gr[i][0]);
        for (int i = 0; i < n - 2; ++i) {
            for (auto e : gr[ord.back()]) {
                if (ord[ord.size() - 2] == e) continue;
                ord.push_back(e);
                break;
            }
        }
        break;
    }
    vector<int> backord(n);
    for (int i = 0; i < n; ++i) {
        backord[ord[i]] = i;
    }
    vector<int> c(n);
    for (int i = 0; i < n; ++i) {
        cin >> c[backord[i]];
    }
    for (int i = 0; i < n; ++i) {
        c[i]--;
    }
    vector<int> L(k, 1e9), R(k, -1);
    for (int i = 0; i < n; ++i) {
        L[c[i]] = min(L[c[i]], i);
        R[c[i]] = max(R[c[i]], i);
    }
    for (int i = 0; i < k; ++i) {
        if (L[i] == R[i]) {
            cout << "0\n";
            return;
        }
    }
    vector<vector<int>> lolkek(k);
    for (int i = 0; i < n; ++i) {
        lolkek[c[i]].push_back(i);
    }
    vector<unsigned long long> LOL(k);
    for (int i = 0; i < k; ++i) {
        LOL[i] = rnd();
    }
    vector<unsigned long long> x(n);
    vector<bool> ok(n);
    for (int i = 0; i < k; ++i) {
        ok[L[i]] = 1;
        x[L[i]] = LOL[i];
        x[R[i]] = LOL[i];
    }
    vector<unsigned long long> px(n + 1);
    for (int i = 0; i < n; ++i) px[i + 1] = (px[i] ^ x[i]);
    vector<int> p2(n + 1);
    for (int i = 0; i < k; ++i) p2[L[i] + 1]++;
    for (int i  = 1; i <= n; ++i) p2[i] += p2[i - 1];
    map<unsigned long long, vector<int>> kek;
    for (int i = 0; i <= n; ++i) {
        kek[px[i]].push_back(i);
    }
    for (auto &e : kek) {
        e.second.push_back(n + 2);
        reverse(e.second.begin(), e.second.end());
    }
    int ans = k - 1;
    set<int> fucked;
    fucked.insert(n + 1);
    for (int i = 0; i < n; ++i) {
        kek[px[i]].pop_back();
        if (!ok[i]) continue;
        int R = kek[px[i]].back();
        while (*fucked.begin() <= i) fucked.erase(fucked.begin());
        int T = *fucked.begin();
        if (T > R) {
            ans = min(ans, p2[R] - p2[i] - 1);
        }
        for (auto e : lolkek[c[i]]) {
            fucked.insert(e);
        }
    }
    cout << ans << '\n';
}

signed main() {
    int tc = 1;
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#else
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
#endif
    // cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t  << ": ";
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 0
Runtime Error

input:

20 3
16 10
10 3
18 2
4 5
8 6
11 12
2 14
1 2
6 3
1 11
1 4
7 20
3 2
9 7
3 13
15 19
5 7
17 6
12 15
2
2
1
1
1
2
2
1
3
3
1
3
1
3
2
2
1
2
2
3

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #31:

score: 0
Wrong Answer
time: 238ms
memory: 51572kb

input:

200000 100000
185785 19011
19011 181550
181550 117972
117972 192238
192238 137685
137685 10126
10126 193657
193657 130856
130856 119980
119980 37122
37122 24497
24497 162102
162102 104298
104298 61332
61332 103789
103789 71060
71060 54044
54044 12075
12075 55296
55296 70106
70106 27512
27512 190160
...

output:

99999

result:

wrong answer 1st lines differ - expected: '4', found: '99999'

Subtask #4:

score: 0
Skipped

Dependency #1:

0%