QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#785012 | #6127. Kawa Exam | wallace114514 | WA | 0ms | 76032kb | C++14 | 3.9kb | 2024-11-26 16:37:13 | 2024-11-26 16:37:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int n,m,low[N],dfn[N],tarjan_index,col[N],cnt,maxn=0,a[N];
int u[N],v[N];
int num[N],siz[N],son[N],fa[N],temp_cnt[N],res[N];
int id[N],l[N],r[N],new_index,ans[N];
vector<pair<int,int>> g[N],mp[N];
vector<int> new_g[N];
stack<int> st;
struct seg_tree{
int val[N<<2];
void update(int rt,int l,int r,int pos,int x) {
if(l==r) {val[rt]+=x;return;}
int mid=(l+r)>>1;
if(pos<=mid) update(rt*2,l,mid,pos,x);
else update(rt*2+1,mid+1,r,pos,x);
val[rt]=max(val[rt*2],val[rt*2+1]);
}
}In,Out;
void tarjan(int x,int last) {
dfn[x]=low[x]=++tarjan_index;
st.push(x);
for(int i=0;i<g[x].size();i++) {
if(g[x][i].second==last) continue;
if(!dfn[g[x][i].first]) {
tarjan(g[x][i].first,g[x][i].second);
low[x]=min(low[x],low[g[x][i].first]);
}else low[x]=min(low[x],dfn[g[x][i].first]);
}
if(dfn[x]==low[x]) {
cnt++;
col[x]=cnt;
temp_cnt[a[x]]++;
while(st.top()!=x) {
temp_cnt[a[st.top()]]++;
col[st.top()]=cnt;
st.pop();
}
for(int i=1;i<=maxn;i++) {
if(temp_cnt[i]) {
mp[cnt].push_back({i,temp_cnt[i]});
temp_cnt[i]=0;
}
}
st.pop();
}
}
void dfs1(int x,int last) {
for(int i=0;i<mp[x].size();i++) {
Out.update(1,1,maxn,mp[x][i].first,mp[x][i].second);
}
siz[x]=1;son[x]=0;fa[x]=last;
l[x]=++new_index;id[new_index]=x;
for(int i=0;i<new_g[x].size();i++) {
if(new_g[x][i]==last) continue;
dfs1(new_g[x][i],x);
if(siz[new_g[x][i]]>siz[son[x]])
son[x]=new_g[x][i];
siz[x]+=siz[new_g[x][i]];
}r[x]=new_index;
}
void dfs2(int x) {
ans[x]=-Out.val[1];
for(int i=0;i<new_g[x].size();i++) {
if(new_g[x][i]==son[x]||new_g[x][i]==fa[x]) continue;
dfs2(new_g[x][i]);
int v=new_g[x][i];
for(int j=l[v];j<=r[v];j++) {
for(int k=0;k<mp[id[j]].size();k++) {
In.update(1,1,maxn,mp[id[j]][k].first,-mp[id[j]][k].second);
Out.update(1,1,maxn,mp[id[j]][k].first,mp[id[j]][k].second);
}
}
}
if(son[x]) {dfs2(son[x]);}
for(int i=0;i<new_g[x].size();i++) {
if(new_g[x][i]==son[x]||new_g[x][i]==fa[x]) continue;
int v=new_g[x][i];
for(int j=l[v];j<=r[v];j++) {
for(int k=0;k<mp[id[j]].size();k++) {
In.update(1,1,maxn,mp[id[j]][k].first,mp[id[j]][k].second);
Out.update(1,1,maxn,mp[id[j]][k].first,-mp[id[j]][k].second);
}
}
}
for(int i=0;i<mp[x].size();i++) {
In.update(1,1,maxn,mp[x][i].first,mp[x][i].second);
Out.update(1,1,maxn,mp[x][i].first,-mp[x][i].second);
}
ans[x]+=In.val[1]+Out.val[1];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
memset(In.val,0,sizeof(In.val));
int t;
cin>>t;
while(t--) {
cin>>n>>m;
new_index=cnt=tarjan_index=maxn=0;
for(int i=1;i<=n;i++) {
cin>>a[i];
low[i]=dfn[i]=l[i]=0;
maxn=max(maxn,a[i]);
g[i].clear();
new_g[i].clear();
mp[i].clear();
}
for(int i=1;i<=m;i++) {
cin>>u[i]>>v[i];
g[u[i]].push_back({v[i],i});
g[v[i]].push_back({u[i],i});
}
for(int i=1;i<=n;i++) {
if(!dfn[i]) tarjan(i,0);
}
for(int i=1;i<=m;i++) {
if(col[u[i]]!=col[v[i]]) {
new_g[col[u[i]]].push_back(col[v[i]]);
new_g[col[v[i]]].push_back(col[u[i]]);
}
}
int sum=0;
for(int i=1;i<=cnt;i++) {
if(!l[i]) {
dfs1(i,i);
sum+=Out.val[1];
dfs2(i);
for(int j=1;j<=(maxn<<2);j++)
In.val[j]=Out.val[j]=0;
}
}
for(int i=1;i<=m;i++) {
if(col[u[i]]==col[v[i]]) {res[i]=sum;}
else {
res[i]=sum;
if(fa[col[u[i]]]==col[v[i]]) res[i]+=ans[col[u[i]]];
else res[i]+=ans[col[v[i]]];
}
}
for(int i=1;i<=m;i++) cout<<res[i]<<' ';
cout<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 76032kb
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:
wrong answer 1st lines differ - expected: '6 5 5 5 4', found: '6 5 5 5 4 '