QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#122290 | #6127. Kawa Exam | lam | RE | 0ms | 18516kb | C++17 | 5.5kb | 2023-07-09 22:45:08 | 2023-07-09 22:45:09 |
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])
{
mp[i].clear();
vals[i].clear();
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 << (bridge[m] ? res[m] : global);
cout << endl;
reset();
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t=1; cin >> t;
while (t--) process();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 18516kb
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: -100
Dangerous Syscalls
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 3 3 3 3 7 7 7 7 9 8 8 8 9 8 8 8 9 9 10 9 9 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 7 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 9 9 16 16 16 16 16 16 16 16 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9...