QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#122280 | #6127. Kawa Exam | lam | WA | 2ms | 21100kb | C++17 | 5.4kb | 2023-07-09 22:20:32 | 2023-07-09 22:20:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define task "a"
#define etr "\n"
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pli pair<long long,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define bg begin
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define lwb lower_bound
#define upb upper_bound
#define range(x, l, r) x+l, x+1+r
#define all(x) (x).bg(), (x).end()
#define compact(x) x.resize(unique(all(x)) - (x).bg())
#define sq(x) ((x)*(x))
auto start = chrono::high_resolution_clock::now();
void start_timer()
{
start = chrono::high_resolution_clock::now();
}
ld elapsed()
{
auto current = chrono::high_resolution_clock::now();
ld duration = chrono::duration_cast<chrono::nanoseconds>(current - start).count();
return duration / 1e9;
}
void freop()
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const int N=1e5, M=1e5, mod=1e9+7;
int n, m, timer=0, global=0;
pii edge[N+5];
int a[N+5], sz[N+5], root[N+5], et[N+5]; //basic properties
int in[N+5], out[N+5], low[N+5]; //bridge properties
int res[N+5], sub[N+5];
vector<pii> adj[N+5], tree[N+5];
vector<int> cc;
map<int,int> mp[N+5];
multiset<int> vals[N+5];
bool bridge[N+5];
void reset()
{
for (int i=1; i<=n; i++)
{
adj[i].clear();
tree[i].clear();
mp[i].clear();
vals[i].clear();
}
cc.clear();
fill(range(bridge, 1, m), false);
fill(range(root, 1, n), 0);
global = 0;
timer = 0;
}
void dfs_init(int u)
{
et[++timer] = u;
sz[u] = 1;
mp[u][a[u]] = 1;
vals[u].insert(1);
for (auto [v,i] : adj[u])
{
if (root[v]) continue;
root[v] = root[u];
tree[u].pb({v,i});
dfs_init(v);
sz[u] += sz[v];
if (mp[v].size() > mp[u].size())
{
swap(mp[u], mp[v]);
swap(vals[u], vals[v]);
}
for (auto [x,y] : mp[v])
{
if (mp[u].find(x) == mp[u].end()) mp[u][x] = 0;
int& val = mp[u][x];
if (val && vals[u].find(val) != vals[u].end()) vals[u].erase(vals[u].find(val));
val += y;
vals[u].insert(val);
}
}
sub[u] = *vals[u].rbegin();
out[u] = timer;
}
void dfs_bridge(int par, int u)
{
//cout << par << ' ' << u << "..." << endl;
in[u] = low[u] = ++timer;
bool skip = true;
for (auto [v,i] : adj[u])
{
if (v == par && skip)
{
skip = false;
continue;
}
if (in[v]) low[u] = min(low[u], in[v]);
else
{
dfs_bridge(u, v);
low[u] = min(low[u], low[v]);
if (low[v] == in[v]) bridge[i] = true;
}
}
}
void dfs_tree(int u, bool keep, int id) //keep = 0: add lai vao map; keep = 1: khong add lai
{
map<int,int>& cnt = mp[root[u]];
multiset<int>& s = vals[root[u]];
int child=0, tmp = *s.rbegin(), idx;
for (auto [v,i] : tree[u])
{
if (sz[v] > sz[child]) child = v, idx=i;
}
for (auto [v,i] : tree[u])
{
if (v != child) dfs_tree(v, 0, i);
}
if (child) dfs_tree(child, 1, idx);
for (auto [v,i] : tree[u])
{
if (v == child) continue;
for (int j=in[v]; j<=out[v]; j++)
{
s.erase(s.find(cnt[a[et[j]]]));
s.insert(--cnt[a[et[j]]]);
}
}
s.erase(s.find(cnt[a[u]]));
s.insert(--cnt[a[u]]);
if (bridge[id]) res[id] = *s.rbegin() + sub[u] + global - tmp;
else res[id] = global;
//cout << u << ' ' << id << ": " << *s.rbegin() << ' ' << tmp << "..." << endl;
if (keep == 1) return;
for (auto [v,i] : tree[u])
{
if (v == child) continue;
for (int j=in[v]; j<=out[v]; j++)
{
s.erase(s.find(cnt[a[et[j]]]));
s.insert(++cnt[a[et[j]]]);
}
}
s.erase(s.find(cnt[a[u]]));
s.insert(++cnt[a[u]]);
}
void debug()
{
cout << endl;
for (int i=1; i<=n; i++)
{
cout << sub[i] << ' ';
}
cout << endl << endl;
for (int i : cc)
{
cout << i << ": " << *vals[i].rbegin() << endl;
//for (auto [x,y] : mp[i]) cout << x << ' ' << y << endl;
}
cout << endl;
for (int i=1; i<=n; i++)
{
cout << et[i] << ' ';
}
cout << endl << endl;
cout << global << "..." << endl;
cout << endl;
}
void process()
{
cin >> n >> m;
for (int i=1; i<=n; i++) cin >> a[i];
for (int i=1; i<=m; i++)
{
int u,v;
cin >> u >> v;
edge[i] = {u,v};
if (u == v) continue;
adj[u].pb({v,i});
adj[v].pb({u,i});
}
for (int i=1; i<=n; i++)
{
if (root[i]) continue;
cc.pb(i);
root[i] = i;
dfs_init(i);
}
timer=0;
for (int i : cc)
{
global += *vals[i].rbegin();
dfs_bridge(i,i);
}
//debug();
for (int i : cc)
{
dfs_tree(i, 0, 0);
}
for (int i=1; i<=m; i++) cout << (bridge[i] ? res[i] : global) << ' ';
cout << etr;
reset();
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t=1; cin >> t;
while (t--) process();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 21100kb
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 '