QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#648458 | #6127. Kawa Exam | atgc | AC ✓ | 314ms | 46104kb | C++23 | 2.8kb | 2024-10-17 19:05:15 | 2024-10-17 19:05:24 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
struct{
int cnt_v[N];
vector<int>cis[N];
int val,cnt;
void push(int x){
cis[++cnt_v[x]].push_back(x);
if(cnt_v[x]>cnt)cnt=cnt_v[val=x];
}
void pop(int x){
cis[--cnt_v[x]].push_back(x);
while(!cis[cnt].empty()&&cnt_v[cis[cnt].back()]!=cnt)
cis[cnt].pop_back();
if(cis[cnt].empty())--cnt;
else val=cis[cnt].back();
}
void clear(){
for(int i=1;i<=cnt;++i){
for(int x:cis[i])cnt_v[x]=0;
cis[i]={};
}
val=cnt=0;
}
}Q;
int n,m,va[N],fa[N];
vector<int>a[N],A[N];
struct{int nxt,v;}edge[N<<1];
int head[N],ec;
void add(int u,int v){edge[++ec]={head[u],v};head[u]=ec;}
int dfn[N],low[N],dfc,bc,c[N],dfu[N],s[N],tp;
void dfs(int u,int in){
dfu[dfn[s[++tp]=u]=low[u]=++dfc]=u;
for(int i=head[u];i;i=edge[i].nxt){
if(i==in)continue;
int v=edge[i].v;
if(!dfn[v])dfs(v,i^1),low[u]=min(low[u],low[v]);
else low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]&&++bc)while(s[tp+1]!=u)a[bc].push_back(va[s[tp]]),c[s[tp--]]=bc;
}
vector<int>G[N];
int siz[N],son[N];
void cal_son(int u,int fa){
::fa[u]=fa;
siz[u]=a[u].size();son[u]=0;
for(int v:G[u])if(v!=fa){
cal_son(v,u);
if(siz[son[u]]<siz[v])son[u]=v;
siz[u]+=siz[v];
}
}
int c0[N],c1[N];
void dfs0(int u,int fa){
for(int v:G[u])if(v!=fa&&v!=son[u])dfs0(v,u),Q.clear();
A[u]=a[u];
if(son[u]){
dfs0(son[u],u);
for(int va:a[u])A[son[u]].push_back(va);
A[u]={};swap(A[u],A[son[u]]);
}
for(int v:a[u])Q.push(v);
for(int v:G[u])if(v!=fa&&v!=son[u])for(int va:A[v])Q.push(va),A[u].push_back(va);
c0[u]=Q.cnt;
}
void dfs1(int u,int fa){
c1[u]=Q.cnt;
for(int va:a[u])Q.push(va);
for(int v:G[u])if(v!=fa&&v!=son[u])
for(int va:A[v])Q.push(va);
if(son[u])dfs1(son[u],u);
for(int v:G[u])if(v!=fa&&v!=son[u]){
for(int va:A[v])Q.pop(va);
dfs1(v,u);
}
}
int ans,dta[N];
void sol(){
#define cls(a) memset(a+1,0,n*sizeof a[0])
cls(head),cls(dfn),cls(low),cls(c);dfc=bc=ans=0;ec=1;tp=0;
cls(s);
cin>>n>>m;
memset(dta+1,0,m*sizeof dta[0]);
for(int i=1;i<=n;++i)cin>>va[i],a[i].clear(),G[i].clear();
for(int i=1,u,v;i<=m;++i)
cin>>u>>v,add(u,v),add(v,u);
for(int u=1;u<=n;++u)if(!dfn[u]){
int pdfc=dfc;
dfs(u,0);
for(int i=pdfc+1;i<=dfc;++i)
for(int u=dfu[i],i=head[u];i;i=edge[i].nxt)
if(int v=edge[i].v;c[u]!=c[v])
G[c[u]].push_back(c[v]);
cal_son(c[u],0);
dfs0(c[u],0);Q.clear();
dfs1(c[u],0);Q.clear();
int vv=c0[c[u]];
ans+=vv;
for(int i=pdfc+1;i<=dfc;++i)
for(int u=dfu[i],i=head[u];i;i=edge[i].nxt)
if(int v=edge[i].v;c[u]!=c[v])
if(fa[c[u]]==c[v])dta[i>>1]=c1[c[u]]+c0[c[u]]-vv;
else dta[i>>1]=c1[c[v]]+c0[c[v]]-vv;
}
for(int i=1;i<=m;++i)
cout<<ans+dta[i]<<" \n"[i==m];
}
signed main() {
ios::sync_with_stdio(0),cin.tie(0);
int T;cin>>T;while(T--)sol();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11312kb
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: 282ms
memory: 45024kb
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: 247ms
memory: 46104kb
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: 252ms
memory: 44848kb
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: 0
Accepted
time: 290ms
memory: 40584kb
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 ...
result:
ok 10 lines
Test #6:
score: 0
Accepted
time: 287ms
memory: 40084kb
input:
10 100000 99999 76207 76207 88551 88551 98176 76207 98176 88551 88551 98176 88551 76207 76207 98176 98176 76207 76207 88551 76207 88551 76207 88551 88551 76207 88551 76207 98176 88551 76207 98176 88551 88551 76207 88551 98176 88551 76207 76207 98176 88551 76207 98176 76207 88551 88551 88551 88551 76...
output:
33484 33484 33476 33484 33477 33485 33476 33485 33477 33485 33477 33486 33477 33484 33477 33485 33476 33485 33476 33485 33476 33483 33477 33483 33477 33485 33476 33485 33477 33485 33476 33487 33476 33487 33476 33486 33477 33486 33476 33486 33477 33486 33476 33486 33476 33486 33477 33487 33477 33487 ...
result:
ok 10 lines
Test #7:
score: 0
Accepted
time: 299ms
memory: 40204kb
input:
10 100000 99999 70486 49904 70486 49904 87935 49904 49904 87935 87935 49904 49904 87935 49904 87935 87935 70486 49904 87935 87935 49904 70486 87935 49904 70486 87935 87935 49904 49904 49904 87935 70486 70486 70486 49904 70486 87935 87935 87935 70486 87935 70486 49904 87935 49904 49904 87935 70486 87...
output:
33491 33486 33489 33486 33489 33486 33489 33486 33487 33486 33487 33486 33486 33485 33486 33486 33486 33486 33485 33486 33485 33485 33486 33486 33485 33486 33485 33486 33485 33485 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33485 33486 33485 ...
result:
ok 10 lines
Test #8:
score: 0
Accepted
time: 314ms
memory: 40424kb
input:
10 100000 99999 98004 33580 98004 98004 98004 92291 92291 98004 98004 92291 92291 33580 98004 92291 33580 98004 98004 33580 98004 92291 92291 33580 92291 92291 98004 33580 98004 33580 33580 98004 33580 92291 33580 33580 92291 92291 92291 98004 33580 98004 92291 92291 33580 92291 98004 98004 92291 92...
output:
33462 33463 33421 33463 33422 33465 33421 33463 33422 33464 33422 33462 33422 33464 33421 33464 33422 33464 33422 33465 33422 33463 33422 33462 33422 33463 33422 33465 33421 33464 33422 33464 33422 33463 33422 33463 33421 33463 33421 33462 33422 33460 33422 33461 33421 33461 33422 33460 33422 33459 ...
result:
ok 10 lines