QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#95160#6127. Kawa ExamGeorge_PloverWA 1019ms43900kbC++145.3kb2023-04-09 11:26:292023-04-09 11:26:36

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-09 11:26:36]
  • 评测
  • 测评结果:WA
  • 用时:1019ms
  • 内存:43900kb
  • [2023-04-09 11:26:29]
  • 提交

answer

#include <map>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define PII pair<int,int>
#define vint vector<int>
#define X first
#define Y second
#define MAXN 110000
#define MAXM 210000
using namespace std;
struct Segment_Tree{
    struct node{
        int l,r;
        int v;
    }tr[MAXN*4];
    int num;
    void init(){
        num=1;
    }
    void pushup(int x){
        tr[x].v=max(tr[tr[x].l].v,tr[tr[x].r].v);
    }
    void Build(int x,int L,int R){
        if(L==R){
            tr[x].v=0;
            return;
        }
        int mid=(L+R)/2;
        tr[x].l=++num;
        Build(tr[x].l,L,mid);
        tr[x].r=++num;
        Build(tr[x].r,mid+1,R);
        pushup(x);
    }
    void Update(int x,int L,int R,int aim,int v){
        if(L==R){
            tr[x].v+=v;
            return;
        }
        int mid=(L+R)/2;
        if(aim<=mid)Update(tr[x].l,L,mid,aim,v);
        else Update(tr[x].r,mid+1,R,aim,v);
        pushup(x);
    }
}seg,sub;
int T,n,m;
int a[MAXN];
bool is_bridge[MAXN],vis[MAXN];
int low[MAXN],dfn[MAXN],num,stk[MAXN],cnt;
int tot=1,pre[MAXN],to[MAXM],lin[MAXM],eid[MAXM];
int SCC,belong[MAXN];
map<int,int> b[MAXN];
PII p[MAXN];
vint son[MAXN],sid[MAXN];
int siz[MAXN],loc[MAXN],hson[MAXN],fa[MAXN],anti[MAXN];
int ans,out[MAXN];

void init(){
    cnt=num=SCC=ans=0;
    tot=1;
    for(int i=1;i<=n;i++){
        pre[i]=0;
        dfn[i]=0;
        b[i].clear();
        belong[i]=0;
        son[i].clear();
        loc[i]=hson[i]=fa[i]=0;
        vis[i]=out[i]=0;
    }
    for(int i=1;i<=m;i++){
        is_bridge[i]=0;
    }
}

void add(int x,int y,int z){
    tot++;lin[tot]=pre[x];pre[x]=tot;to[tot]=y;eid[tot]=z;
}

void tarjan(int x,int in_edge){
    low[x]=dfn[x]=++num;
    stk[++cnt]=x;
    for(int i=pre[x];i;i=lin[i]){
        int v=to[i];
        if(!dfn[v]){
            tarjan(v,i);
            low[x]=min(low[x],low[v]);
            if(low[v]>dfn[x]){
                is_bridge[eid[i]]=1;
                SCC++;
                int t;
                do{
                    t=stk[cnt--];
                    belong[t]=SCC;
                    b[SCC][a[t]]++;
                }while(t!=v);
            }
        }
        else if(i!=(in_edge^1)){
            low[x]=min(low[x],dfn[v]);
        }
    }
}

void dfs(int x,map<int,int> &CNT){
    vis[x]=1;
    loc[x]=++num;
    anti[num]=x;
    siz[x]=b[x].size();
    for(auto &i: b[x]){
        CNT[i.first]+=i.second;
    }
    for(auto &v:son[x]){
        if(v!=fa[x]){
            fa[v]=x;
            dfs(v,CNT);
            siz[x]+=siz[v];
            if(siz[hson[x]]<siz[v])
                hson[x]=v;
        }
    }
}

void deal_sub_tree(int x,int sig){
    for(int i=loc[x];i<loc[x]+siz[x];i++){
        int y=anti[i];
        for(auto &j: b[y]){
            seg.Update(1,1,MAXN,j.X,j.Y*sig);
            sub.Update(1,1,MAXN,j.X,-j.Y*sig);
        }
    }
}
int TMP_ANS;
void dsu(int x,int in){
    int t=0,hsonid;
    for(auto &v:son[x]){
        if(v!=hson[x]){
            dsu(v,sid[x][t]);
            deal_sub_tree(v,-1);
        }
        else hsonid = sid[x][t];
        t++;
    }
    if(hson[x]){
        dsu(hson[x],hsonid);
    }
    for(auto &v:son[x]){
        if(v!=hson[x]){
            deal_sub_tree(v,1);
        }
    }
    for(auto &i: b[x]){
        seg.Update(1,1,MAXN,i.X,i.Y);
        sub.Update(1,1,MAXN,i.X,-i.Y);
    }
    //cout<<x<<" "<<seg.tr[1].v<<" "<<sub.tr[1].v<<endl;
    if(in){
        out[in] += (-TMP_ANS)+seg.tr[1].v+sub.tr[1].v;
    }
}

void work(){
    for(int i=1;i<=n;i++){
        if(!vis[belong[i]]){
            //cout<<i<<endl;
            num=0;
            map<int,int> CNT;
            dfs(belong[i],CNT);
            int t=0;
            for(auto &j: CNT){
                t=max(t,j.second);
                sub.Update(1,1,MAXN,j.first,j.second);
            }
            TMP_ANS = t;
            ans+=t;
            dsu(belong[i],0);
            for(auto &j: CNT){
                seg.Update(1,1,MAXN,j.first,-j.second);
            }
        }
    }
}

int main(){
    scanf("%d",&T);
    seg.init();sub.init();
    seg.Build(1,1,MAXN);sub.Build(1,1,MAXN);
    while(T--){
        scanf("%d%d",&n,&m);
        init();

        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=m;i++){
            scanf("%d%d",&p[i].X,&p[i].Y);
            add(p[i].X,p[i].Y,i);
            add(p[i].Y,p[i].X,i);
        }
        for(int i=1;i<=n;i++){
            if(!dfn[i]){
                tarjan(i,0);
                SCC++;
                while(cnt){
                    int t=stk[cnt--];
                    belong[t]=SCC;
                    b[SCC][a[t]]++;
                }
            }
        }

        for(int i=1;i<=n;i++){
            //cout<<i<<" "<<belong[i]<<endl;
            for(int j=pre[i];j;j=lin[j]){
                int u=i,v=to[j];
                int U = belong[u], V = belong[v];
                if(U>V){
                    son[U].push_back(V);
                    sid[U].push_back(eid[j]);
                }
            }
        }

        work();
        for(int i=1;i<=m;i++){
            printf("%d%c",ans+out[i],i==m?'\n':' ');
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 20024kb

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: -100
Wrong Answer
time: 1019ms
memory: 43900kb

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
8 10 9 8 9 8 9 8 9 9 10 9 9
2 2 2 2 3 2 3 2 3 3 4 3 3 2 2
7 7
1 1 1 1 1 1 1 1 2 2 4 2 2 1 1
4 4 4 4 4 4 4 4 5 5 7 5 5 4 4 4 4
9 10 9
16 16 16 16 16 17 16 16
10 12 11 11 10 11 10 10 10 10 10 10 10 12 10 10 10 10 10 10
9 9 9 10 9 10 9 9 9 9 9 9 9 9 9...

result:

wrong answer 5th lines differ - expected: '9 9 9 8 9 8 9 8 9 9 10 9 9', found: '8 10 9 8 9 8 9 8 9 9 10 9 9'