ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#212876 | #3821. T3 | happywinter | 80 | 16027ms | 255068kb | C++11 | 3.5kb | 2024-10-20 16:50:54 | 2024-10-20 18:50:04 |
answer
# include <cstdio>
# include <iostream>
# include <cmath>
# include <algorithm>
# include <cstring>
# include <stack>
# include <queue>
using namespace std;
template <typename Temp> inline void read(Temp & res) {
Temp fh = 1; res = 0; char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') fh = -1;
for(; isdigit(ch); ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ '0');
res = res * fh;
}
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);
sz[nod] += sz[to];
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() {
read(n);
read(m);
//n = m = 1e6;
for (int i = 1; i <= n; i++) read(a[i]);//a[i] = rand() % 100;
for (int i = 1; i <= m; i++) {
int from, to;
read(from);
read(to);
//from = rand() % n + 1, to = rand() % n + 1;
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);
}
//return 0;
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]) printf("%d ", tans);
else {
if (dep[u[i]] > dep[v[i]]) swap(u[i], v[i]);
printf("%d ", 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: 7ms
memory: 54080kb
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: 10
Accepted
time: 2893ms
memory: 176788kb
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:
247913 247913 247913 247913 247913 247913 247914 247915 247913 247913 247914 247914 247913 247914 24...
result:
ok 1000000 numbers
Test #3:
score: 10
Accepted
time: 2775ms
memory: 176780kb
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:
247435 247436 247435 247435 247436 247435 247435 247436 247435 247435 247435 247435 247436 247435 24...
result:
ok 1000000 numbers
Test #4:
score: 10
Accepted
time: 531ms
memory: 255068kb
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: 569ms
memory: 255052kb
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: 10
Accepted
time: 2935ms
memory: 180328kb
input:
1000000 999999 177974 461791 142691 363772 546626 904219 194179 141553 539237 278698 488828 454167 9...
output:
162472 162472 162472 162472 162473 162472 162472 162473 162473 162472 162473 162473 162472 162472 16...
result:
ok 999999 numbers
Test #9:
score: 10
Accepted
time: 3028ms
memory: 180380kb
input:
1000000 999999 511557 68848 896777 731815 32886 932833 390201 839100 548903 603989 235104 501625 206...
output:
162267 162267 162268 162267 162267 162267 162267 162268 162267 162268 162267 162268 162267 162267 16...
result:
ok 999999 numbers
Test #10:
score: 10
Accepted
time: 3289ms
memory: 180364kb
input:
1000000 999999 808476 306755 435706 912414 23416 659340 799407 8345 687388 935128 421729 38016 93583...
output:
162469 162469 162469 162469 162470 162469 162469 162470 162469 162469 162470 162469 162469 162469 16...
result:
ok 999999 numbers