QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#91062#6127. Kawa ExamValera_GrinenkoAC ✓969ms54376kbC++147.6kb2023-03-26 22:32:132023-03-26 22:32:16

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-26 22:32:16]
  • 评测
  • 测评结果:AC
  • 用时:969ms
  • 内存:54376kb
  • [2023-03-26 22:32:13]
  • 提交

answer

//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")

#ifdef __APPLE__

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <map>
#include <queue>
#include <ctime>
#include <stack>
#include <set>
#include <list>
#include <random>
#include <deque>
#include <functional>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <complex>
#include <numeric>
#include <immintrin.h>
#include <cassert>
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <thread>

#else
#include <bits/stdc++.h>
#endif

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

template<typename T>
bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#if __APPLE__
#define D for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl

template<class ...Ts>
auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }

#else
#define D while (false)
#define LOG(...)
#endif

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int max_n = 2e5 + 42;

vector<pair<int, int> > g[max_n], g2[max_n];

vector<int> ans, two_edge_component, comp_num;
vector<vector<int> > two_edge_components;
int cur_component = 0;

vector<bool> used, is_bridge;

int a[max_n];

map<int, int> am_dfs1;

vector<int> nodes_dfs1;

void dfs1(int v, bool save_nodes = false) {
    if(save_nodes) nodes_dfs1.pb(v);
    used[v] = true;
    am_dfs1[a[v]]++;
    for(auto& to : g[v])
        if(!used[to.fi])
            dfs1(to.fi, save_nodes);
}

vector<bool> visited;
vector<int> tin, low;
int timer;

void dfs2(int v, int p = -1) {
    visited[v] = true;
    tin[v] = low[v] = timer++;
    for (auto& to : g[v]) {
        if (to.se == p) continue;
        if (visited[to.fi]) {
            low[v] = min(low[v], tin[to.fi]);
        } else {
            dfs2(to.fi, to.se);
            low[v] = min(low[v], low[to.fi]);
            if (low[to.fi] > tin[v]) is_bridge[to.se] = true;
        }
    }
}

void find_bridges(int n) {
    timer = 0;
    visited.assign(n, false);
    tin.assign(n, -1);
    low.assign(n, -1);
    for (int i = 0; i < n; ++i) {
        if (!visited[i])
            dfs2(i);
    }
}

void dfs3(int v) {
    comp_num[v] = cur_component;
    two_edge_component.pb(v);
    used[v] = true;
    for(auto& to : g[v])
        if(!used[to.fi] && !is_bridge[to.se])
            dfs3(to.fi);
}

struct kek {
    //add integer x to multiset
    //delete integer x from multiset
    //get number of occurences of mode(most frequent element) in multiset
    int am_element[max_n] = {}; //number of elements equal to x
    int am_am_element[max_n] = {}; //number of elements that have x occurences in multiset
    int res = 0;
    void init() {
        res = 0;
        for(int i = 0; i < max_n; i++) {
            am_element[i] = am_am_element[i] = 0;
        }
        am_am_element[0] = max_n + 42;
    }
    void add(int val) {
        am_am_element[am_element[val]]--;
        am_element[val]++;
        am_am_element[am_element[val]]++;
        umax(res, am_element[val]);
    }
    void del(int val) {
        am_am_element[am_element[val]]--;
        am_element[val]--;
        am_am_element[am_element[val]]++;
        if(res && !am_am_element[res]) res--;
    }
    int get() {
        return res;
    }
};

kek kek1, kek2; //kek1 -> inside of subtree, kek2 -> outside of subtree

vector<int> sz;

void dfssz(int v, int p) {
    sz[v] = len(two_edge_components[v]);
    for(auto& to : g2[v])
        if(to.fi != p) {
            dfssz(to.fi, v);
            sz[v] += sz[to.fi];
        }
}

vector<int> vec[max_n];

int base_ans = 0;

void dfs4(int v, int p, bool keep) {
    int par_edge = -1;
    int mx_child = -1, child = -1;
    for(auto& to : g2[v]) {
        if(to.fi == p) { par_edge = to.se; continue; }
        if(umax(mx_child, sz[to.fi])) {
            child = to.fi;
        }
    }
    for(auto& to : g2[v])
        if(to.fi != p && to.fi != child)
            dfs4(to.fi, v, 0);
    if(child != -1) {
        dfs4(child, v, 1);
        vec[v].swap(vec[child]);
    } else vec[v] = vector<int>();
    for(auto& node : two_edge_components[v]) {
        vec[v].pb(node);
        kek1.add(a[node]);
        kek2.del(a[node]);
    }
    for(auto& to : g2[v])
        if(to.fi != p && to.fi != child)
            for(auto& node : vec[to.fi]) {
                vec[v].pb(node);
                kek1.add(a[node]);
                kek2.del(a[node]);
            }
    if(par_edge != -1) {
        ans[par_edge] = kek1.get() + kek2.get() + base_ans;
    }
    if(!keep) {
        for(auto& node : vec[v]) {
            kek1.del(a[node]);
            kek2.add(a[node]);
        }
    }
}

void solve() {
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++) {
        g[i].clear();
        g2[i].clear();
        cin >> a[i];
    }
    vector<pair<int, int> > edges;
    for(int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        edges.pb({u, v});
        g[u].pb({v, i});
        g[v].pb({u, i});
    }
    vector<int> roots;
    used.assign(n, false);
    base_ans = 0;
    for(int i = 0; i < n; i++)
        if(!used[i]) {
            roots.pb(i);
            am_dfs1.clear();
            dfs1(i);
            int mx = 0;
            for(auto& x : am_dfs1) umax(mx, x.se);
            base_ans += mx;
        }
    ans.assign(m, base_ans);
    is_bridge.assign(m, false);
    find_bridges(n);
    used.assign(n, false);
    two_edge_component.assign(n, -1);
    cur_component = 0;
    two_edge_components.clear();
    comp_num.assign(n, -1);
    for(int i = 0; i < n; i++)
        if(!used[i]) {
            two_edge_component.clear();
            dfs3(i);
            two_edge_components.pb(two_edge_component);
            cur_component++;
        }
    for(int i = 0; i < m; i++)
        if(comp_num[edges[i].fi] != comp_num[edges[i].se]) {
            g2[comp_num[edges[i].fi]].pb({comp_num[edges[i].se], i});
            g2[comp_num[edges[i].se]].pb({comp_num[edges[i].fi], i});
        }
    used.assign(n, false);
    sz.assign(n, 0);
    for(auto& root : roots) {
        am_dfs1.clear();
        dfs1(root, true);
        int mx = 0;
        for(auto& x : am_dfs1) umax(mx, x.se);
        for(auto& v : nodes_dfs1) kek2.add(a[v]);
        base_ans -= mx;
        dfssz(comp_num[root], comp_num[root]);
        dfs4(comp_num[root], comp_num[root], false);
        base_ans += mx;
        for(auto& v : nodes_dfs1) kek2.del(a[v]);
        nodes_dfs1.clear();
    }
    for(int i = 0; i < m; i++) {
        if(i) cout << ' ';
        cout << ans[i];
    }
    cout << '\n';
}

signed main() {
//   freopen("input.txt", "r", stdin);
//   freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    kek1.init(); kek2.init();

    int t = 1;

    cin >> t;

    while (t--) solve();

}

/*
KIVI
 testing
*/

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 20764kb

input:

3
7 5
1 2 1 2 1 2 1
1 2
1 3
2 4
5 6
5 7
3 3
1 2 3
1 2
1 3
2 3
2 3
12345 54321
1 2
1 2
1 1

output:

6 5 5 5 4
1 1 1
1 1 1

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 969ms
memory: 50716kb

input:

5557
2 7
79960 79960
2 2
1 1
1 1
2 2
1 1
2 1
1 2
9 8
21881 70740 70740 21881 22458 22458 639 21881 70740
3 3
1 6
5 8
7 5
5 7
2 3
5 1
7 6
6 7
13064 20716 6746 13064 6746 69225
5 5
4 1
4 1
1 6
4 5
3 2
3 2
8 4
45146 14400 45146 45146 14400 72969 14400 45146
8 6
1 3
4 6
8 3
18 13
48132 37949 92338 92338...

output:

2 2 2 2 2 2 2
6 6 7 6 6 6 6 6
3 3 3 4 4 3 3
7 7 7 7
9 9 9 8 9 8 9 8 9 9 10 9 9
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
7 8
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
9 10 9
16 16 16 16 16 17 16 16
10 10 11 10 12 11 10 10 10 10 10 10 10 12 10 10 10 10 10 11
10 9 9 9 9 9 9 9 9 9 9 9 9 9 10 ...

result:

ok 5557 lines

Test #3:

score: 0
Accepted
time: 473ms
memory: 54312kb

input:

10
100000 99999
3983 3983 20157 97983 20157 20157 3983 3983 97983 20157 20157 3983 97983 20157 3983 20157 20157 3983 3983 3983 97983 97983 20157 3983 3983 97983 20157 97983 20157 97983 3983 97983 97983 3983 20157 3983 20157 20157 97983 3983 3983 3983 3983 97983 97983 3983 97983 97983 3983 20157 3983...

output:

33392 33393 33393 33393 33393 33392 33392 33393 33393 33393 33392 33393 33393 33392 33393 33393 33392 33392 33392 33393 33393 33393 33392 33392 33393 33393 33393 33393 33393 33392 33393 33393 33392 33393 33392 33393 33393 33393 33392 33392 33392 33392 33393 33393 33392 33393 33393 33392 33393 33392 ...

result:

ok 10 lines

Test #4:

score: 0
Accepted
time: 496ms
memory: 54376kb

input:

10
100000 99999
27534 27534 3780 3780 27534 53544 27534 3780 3780 53544 53544 27534 53544 53544 3780 3780 3780 3780 53544 27534 3780 3780 53544 27534 27534 53544 27534 27534 53544 27534 27534 27534 3780 27534 27534 3780 3780 3780 27534 53544 3780 53544 27534 3780 3780 3780 27534 27534 27534 3780 275...

output:

33613 33601 33601 33600 33600 33601 33601 33601 33600 33601 33600 33600 33601 33601 33601 33601 33601 33601 33600 33600 33601 33601 33601 33601 33600 33601 33601 33600 33601 33600 33601 33600 33601 33601 33601 33601 33600 33601 33601 33601 33601 33601 33601 33601 33601 33601 33600 33601 33600 33601 ...

result:

ok 10 lines

Test #5:

score: 0
Accepted
time: 483ms
memory: 47576kb

input:

10
100000 99999
92499 92270 92270 92499 92499 92499 92270 54017 92270 92270 92270 54017 54017 54017 54017 92270 92499 54017 92270 54017 92499 92499 92270 92270 54017 54017 54017 54017 92270 92270 92499 54017 54017 92499 92499 54017 92270 92270 54017 92499 92270 92270 54017 54017 54017 92499 92499 54...

output:

33506 33482 33507 33482 33508 33483 33508 33483 33508 33483 33507 33483 33506 33483 33505 33483 33503 33483 33503 33482 33504 33483 33505 33483 33504 33483 33502 33483 33501 33483 33500 33482 33502 33483 33500 33483 33501 33482 33502 33483 33501 33483 33500 33482 33500 33483 33498 33483 33499 33483 ...

result:

ok 10 lines

Test #6:

score: 0
Accepted
time: 479ms
memory: 47796kb

input:

10
100000 99999
76207 76207 88551 88551 98176 76207 98176 88551 88551 98176 88551 76207 76207 98176 98176 76207 76207 88551 76207 88551 76207 88551 88551 76207 88551 76207 98176 88551 76207 98176 88551 88551 76207 88551 98176 88551 76207 76207 98176 88551 76207 98176 76207 88551 88551 88551 88551 76...

output:

33484 33484 33476 33484 33477 33485 33476 33485 33477 33485 33477 33486 33477 33484 33477 33485 33476 33485 33476 33485 33476 33483 33477 33483 33477 33485 33476 33485 33477 33485 33476 33487 33476 33487 33476 33486 33477 33486 33476 33486 33477 33486 33476 33486 33476 33486 33477 33487 33477 33487 ...

result:

ok 10 lines

Test #7:

score: 0
Accepted
time: 489ms
memory: 48400kb

input:

10
100000 99999
70486 49904 70486 49904 87935 49904 49904 87935 87935 49904 49904 87935 49904 87935 87935 70486 49904 87935 87935 49904 70486 87935 49904 70486 87935 87935 49904 49904 49904 87935 70486 70486 70486 49904 70486 87935 87935 87935 70486 87935 70486 49904 87935 49904 49904 87935 70486 87...

output:

33491 33486 33489 33486 33489 33486 33489 33486 33487 33486 33487 33486 33486 33485 33486 33486 33486 33486 33485 33486 33485 33485 33486 33486 33485 33486 33485 33486 33485 33485 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33485 33486 33485 ...

result:

ok 10 lines

Test #8:

score: 0
Accepted
time: 515ms
memory: 48420kb

input:

10
100000 99999
98004 33580 98004 98004 98004 92291 92291 98004 98004 92291 92291 33580 98004 92291 33580 98004 98004 33580 98004 92291 92291 33580 92291 92291 98004 33580 98004 33580 33580 98004 33580 92291 33580 33580 92291 92291 92291 98004 33580 98004 92291 92291 33580 92291 98004 98004 92291 92...

output:

33462 33463 33421 33463 33422 33465 33421 33463 33422 33464 33422 33462 33422 33464 33421 33464 33422 33464 33422 33465 33422 33463 33422 33462 33422 33463 33422 33465 33421 33464 33422 33464 33422 33463 33422 33463 33421 33463 33421 33462 33422 33460 33422 33461 33421 33461 33422 33460 33422 33459 ...

result:

ok 10 lines