QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#121480 | #6127. Kawa Exam | pemguimn | TL | 2279ms | 30268kb | C++14 | 4.8kb | 2023-07-08 12:11:44 | 2023-07-08 12:11:53 |
Judging History
answer
#include <bits/stdc++.h>
#define pii pair<int, int>
#define pb push_back
#define gcd __gcd
#define endl "\n"
#define task "C"
using namespace std;
const int N = 1e5 + 5, MOD = 1e9 + 7;
int n, m, a[N], num[N], low[N], timedfs = 0, total = 0, comp[N], freq[N], rfreq[N], ans[N], sz[N], source[N];
vector<pii> adj[N];
vector<int> vert;
bool isBridge[N], vis[N], big[N], mainE[N];
multiset<int> s1, s2;
void reset(){
total = timedfs = 0;
for(int i = 1; i <= n; i++){
adj[i].clear();
num[i] = low[i] = 0;
sz[i] = 0;
freq[a[i]] = rfreq[a[i]] = 0;
comp[i] = 0;
sz[i] = 0;
vis[i] = big[i] = false;
}
for(int i = 1; i <= m; i++){
isBridge[i] = mainE[i] = false;
ans[i] = 0;
source[i] = 0;
}
vert.clear();
s1.clear(), s2.clear();
}
void dfs_tree(int i, int pre){
num[i] = low[i] = ++timedfs;
sz[i] = 1;
bool ok = false;
for(pii e : adj[i]){
int x = e.first, id = e.second;
if(x == i) continue;
if(x == pre && !ok){
ok = true;
continue;
}
if(!num[x]){
mainE[id] = true;
dfs_tree(x, i); sz[i] += sz[x];
low[i] = min(low[i], low[x]);
if(low[x] > num[i]){
isBridge[id] = true;
}
} else{
low[i] = min(low[i], num[x]);
}
}
}
void dfs_ans(int i, int pre){
vert.pb(i);
vis[i] = true;
s2.erase(s2.find(rfreq[a[i]]));
rfreq[a[i]]++;
s2.insert(rfreq[a[i]]);
for(pii e : adj[i]){
int x = e.first, id = e.second;
if(!vis[x]){
dfs_ans(x, i);
}
}
comp[i] = *s2.rbegin();
}
void add(int i, int pre){
s1.erase(s1.find(freq[a[i]]));
s2.erase(s2.find(rfreq[a[i]]));
freq[a[i]]++;
rfreq[a[i]]--;
s2.insert(rfreq[a[i]]);
s1.insert(freq[a[i]]);
for(pii e : adj[i]){
int x = e.first, id = e.second;
if(x == pre || big[x] || !mainE[id] || num[x] <= num[i]) continue;
add(x, i);
}
}
void rem(int i, int pre){
s1.erase(s1.find(freq[a[i]]));
s2.erase(s2.find(rfreq[a[i]]));
freq[a[i]]--;
rfreq[a[i]]++;
s2.insert(rfreq[a[i]]);
s1.insert(freq[a[i]]);
for(pii e : adj[i]){
int x = e.first, id = e.second;
if(x == pre || big[x] || !mainE[id] || num[x] <= num[i]) continue;
rem(x, i);
}
}
void dfs_final(int i, int pre, int root, int hehe, bool keep){
source[hehe] = root;
int h_sz = -1, h_child = -1, h_id = 0;
for(pii e : adj[i]){
int x = e.first, id = e.second;
if(x == pre) continue;
if(!mainE[id] || num[x] <= num[i]) continue;
if(h_sz < sz[x]){
h_sz = sz[x], h_child = x, h_id = id;
}
}
for(pii e : adj[i]){
int x = e.first, id = e.second;
if(x == pre || x == h_child) continue;
if(mainE[id] && num[x] > num[i]){
dfs_final(x, i, root, id, 0);
}
}
if(h_child != -1) {
dfs_final(h_child, i, root, h_id, 1); big[h_child] = true;
}
add(i, pre);
if(isBridge[hehe]){
ans[hehe] = *s1.rbegin() + *s2.rbegin();
// cout << hehe << " " << ans[hehe] << endl;
// for(int x : s2) cout << x << " ";
}
if(h_child != -1) big[h_child] = false;
if(keep == 0){
rem(i, pre);
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
int t; cin >> t;
while(t--){
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= m; i++){
int x, y; cin >> x >> y;
adj[x].pb({y, i});
adj[y].pb({x, i});
}
for(int i = 1; i <= n; i++) {
if(!num[i]) dfs_tree(i, i);
}
for(int i = 1; i <= n; i++) s2.insert(0), s1.insert(0);
for(int i = 1; i <= n; i++){
if(!vis[i]){
dfs_ans(i, i);
dfs_final(i, i, i, 0, 0);
total += comp[i];
for(int x : vert) rfreq[a[x]] = freq[a[x]] = 0;
vert.clear();
while(!s1.empty() && *s1.rbegin()) s1.erase(s1.find(*s1.rbegin())), s1.insert(0);
while(!s2.empty() && *s2.rbegin()) s2.erase(s2.find(*s2.rbegin())), s2.insert(0);
}
}
for(int i = 1; i <= m; i++){
if(isBridge[i]) cout << total - comp[source[i]] + ans[i];
else cout << total;
if(i < m) cout << " ";
}
cout << endl;
reset();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 7520kb
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: 2234ms
memory: 30268kb
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: 2279ms
memory: 27392kb
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: 2249ms
memory: 27156kb
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: -100
Time Limit Exceeded
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 ...