QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#91061 | #6127. Kawa Exam | Valera_Grinenko | WA | 2ms | 20612kb | C++14 | 7.6kb | 2023-03-26 22:31:18 | 2023-03-26 22:31:19 |
Judging History
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(auto& x : ans) cout << x << ' ';
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 20612kb
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:
wrong answer 1st lines differ - expected: '6 5 5 5 4', found: '6 5 5 5 4 '