ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#212872 | #3821. T3 | happywinter | 30 | 2016ms | 255128kb | C++11 | 3.2kb | 2024-10-20 16:39:24 | 2024-10-20 18:48:55 |
answer
# include <cstdio>
# include <iostream>
# include <cmath>
# include <algorithm>
# include <cstring>
# include <stack>
# include <queue>
using namespace std;
const int MAXN = 1e6 + 10;
int n, m, a[MAXN], sz[MAXN], son[MAXN], dep[MAXN];
vector <pair <int, int> > vec[MAXN];
vector <int> ts[MAXN];
int Clock = 0, dfn[MAXN], low[MAXN], vis[MAXN], root, ans[MAXN], u[MAXN], v[MAXN], cnt[MAXN];
void tarjan(int nod, int fa) {
low[nod] = dfn[nod] = ++ Clock;
vis[nod] = true;
for (int i = 0; i < vec[nod].size(); i++) cnt[vec[nod][i].first]++;
for (int i = 0; i < vec[nod].size(); i++) {
int to = vec[nod][i].first, id = vec[nod][i].second;
if (!vis[to]) {
tarjan(to, nod);
low[nod] = min(low[nod], low[to]);
if (low[to] > dfn[nod] && cnt[to] < 2) ans[id] = 1;
} else if (to != fa && dfn[nod] > dfn[to]) low[nod] = min(low[nod], dfn[to]);
}
for (int i = 0; i < vec[nod].size(); i++) cnt[vec[nod][i].first]--;
}
int t[MAXN], c[MAXN], p, ans_t[MAXN], ans_nt[MAXN], rt[MAXN];
void add(int x) {
t[c[x]]--;
t[++c[x]]++;
p = max(p, c[x]);
}
void del(int x) {
t[c[x]]--;
t[--c[x]]++;
if (!t[p]) p--;
}
void dfs(int nod, int fa) {
// cout << nod << " " << fa << endl ;
if (fa) rt[nod] = rt[fa];
else rt[nod] = nod;
add(a[nod]);
dep[nod] = dep[fa] + 1;
sz[nod] = 1;
vis[nod] = 1;
for (auto p : vec[nod]) {
int to = p.first, id = p.second;
if (to == fa || vis[to]) continue;
dfs(to, nod);
ts[nod].push_back(to);
if (sz[to] > sz[son[nod]]) son[nod] = to;
}
}
void subtree(int nod, int type) {
if (type == -1) del(a[nod]);
else add(a[nod]);
for (int i : ts[nod]) subtree(i, type);
}
void dfs2(int nod) {
vector <int> v;
for (int i = nod; i; i = son[i]) v.push_back(i);
for (int i = v.size() - 1; i >= 0; i--) {
int nod = v[i];
del(a[nod]);
for (int j : ts[nod]) {
if (j != son[nod]) subtree(j, -1);
}
ans_nt[nod] = p;
}
subtree(nod, 1);
for (int to : v) {
for (int s : ts[to]) if (s != son[to]) dfs2(s);
}
}
void dfs3(int nod) {
vector <int> v;
for (int i = nod; i; i = son[i]) v.push_back(i);
for (int i = v.size() - 1; i >= 0; i--) {
int nod = v[i];
add(a[nod]);
for (int j : ts[nod]) {
if (j != son[nod]) subtree(j, 1);
}
ans_t[nod] = p;
}
subtree(nod, -1);
for (int to : v) {
for (int s : ts[to]) if (s != son[to]) dfs3(s);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++) {
int from, to;
cin >> from >> to;
u[i] = from;
v[i] = to;
vec[from].push_back({to, i});
vec[to].push_back({from, i});
}
for (int i = 1; i <= n; i++) if (!vis[i]) {
root = i;
tarjan(i, i);
}
memset(vis, 0, sizeof vis);
int tans = 0;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
// cout << "erfwer\n" ;
dfs(i, 0);
dfs2(i);
subtree(i, -1);
dfs3(i);
tans += ans_t[i];
}
// cout << tans << endl ;
for (int i = 1; i <= m; i++) {
if (!ans[i]) cout << tans << " " ;
else {
if (dep[u[i]] > dep[v[i]]) swap(u[i], v[i]);
cout << tans - ans_t[rt[u[i]]] + ans_t[v[i]] + ans_nt[v[i]] << " " ;
}
}
cout << endl ;
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 18ms
memory: 54124kb
input:
1000 1000 127389 742557 379975 792810 941035 343277 900097 363092 558167 425747 665153 303524 127389...
output:
165 164 164 165 164 164 164 164 164 165 165 164 165 164 164 164 164 165 165 165 165 164 165 165 164 ...
result:
ok 1000 numbers
Test #2:
score: 0
Time Limit Exceeded
input:
1000000 1000000 1 10 5 5 1 5 10 1 1 7 1 4 6 5 1 8 5 9 1 10 7 1 1 9 6 6 6 3 9 4 1 5 6 6 9 2 4 6 5 2 1...
output:
result:
Test #3:
score: 0
Time Limit Exceeded
input:
1000000 1000000 4 8 3 10 6 5 2 1 1 5 5 4 10 6 3 3 3 1 5 8 8 8 3 1 4 1 9 5 6 9 4 5 1 2 7 10 8 3 5 3 5...
output:
result:
Test #4:
score: 10
Accepted
time: 1185ms
memory: 255128kb
input:
1000000 999999 459788 891071 867951 861734 991468 169523 626206 72233 797362 681021 590377 119511 88...
output:
404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 ...
result:
ok 999999 numbers
Test #5:
score: 10
Accepted
time: 813ms
memory: 255116kb
input:
1000000 999999 80449 974907 433485 520089 585081 449526 106163 294162 453340 699909 512597 103883 63...
output:
434 434 434 434 434 434 434 434 434 434 434 434 434 434 434 434 434 434 434 434 434 434 434 434 434 ...
result:
ok 999999 numbers
Test #6:
score: 0
Time Limit Exceeded
input:
1000000 999999 993172 569193 453215 859064 765172 926293 79502 468888 473832 139292 536558 66118 723...
output:
result:
Test #7:
score: 0
Time Limit Exceeded
input:
1000000 999999 483962 269691 318073 523931 943387 659724 757207 752838 973431 637496 211548 132948 9...
output:
result:
Test #8:
score: 0
Time Limit Exceeded
input:
1000000 999999 177974 461791 142691 363772 546626 904219 194179 141553 539237 278698 488828 454167 9...
output:
result:
Test #9:
score: 0
Time Limit Exceeded
input:
1000000 999999 511557 68848 896777 731815 32886 932833 390201 839100 548903 603989 235104 501625 206...
output:
result:
Test #10:
score: 0
Time Limit Exceeded
input:
1000000 999999 808476 306755 435706 912414 23416 659340 799407 8345 687388 935128 421729 38016 93583...