QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#94332#6127. Kawa Examinstallb#WA 997ms48080kbC++144.6kb2023-04-05 17:33:482023-04-05 17:33:49

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-05 17:33:49]
  • 评测
  • 测评结果:WA
  • 用时:997ms
  • 内存:48080kb
  • [2023-04-05 17:33:48]
  • 提交

answer

#include <bits/stdc++.h>
#define lson (x << 1)
#define rson ((x << 1) | 1)
using namespace std;
typedef long long ll;
const int N = 400005;
const int MXN = 100000;

int eu[N],ev[N];

struct segtree{
    int val[N << 2];
    void pushup(int x){
        val[x] = max(val[lson],val[rson]);
    }
    void modify(int x,int l,int r,int pos,int v){
        if(l == r){
            val[x] += v;
            return;
        }
        int mid = (l + r) >> 1;
        if(pos <= mid) modify(lson,l,mid,pos,v);
        if(pos > mid) modify(rson,mid + 1,r,pos,v);
        pushup(x);
    }
    int query(){ return val[1]; }
}tin,tout;

int ec,to[N << 1],nxt[N << 1],hed[N];
int bri[N << 1];
void add_edge(int f,int t){
    ++ ec; to[ec] = t; nxt[ec] = hed[f]; hed[f] = ec; bri[ec] = 0;
}

int dfn[N],low[N],dfc,a[N];
int mx,cnt[N];
vector <int> ine,inv;

void tarjan(int u,int fe){
    dfn[u] = low[u] = ++ dfc;
    cnt[a[u]] ++; mx = max(mx,cnt[a[u]]);
    inv.push_back(u);
    for(int v,i = hed[u];i;i = nxt[i]){
        v = to[i];
        if(!dfn[v]){
            tarjan(v,i);
            low[u] = min(low[u],low[v]);
            if(low[v] > dfn[u]) bri[i] = bri[i ^ 1] = 1;
        }
        else if(i != (fe ^ 1)) low[u] = min(low[u],dfn[v]);
    }
}

int col[N];
vector <int> vec[N];
vector <pair <int,int> > G[N];

void dfs_dcc(int u,int id){
    col[u] = id;
    vec[id].push_back(u);
    for(int v,i = hed[u];i;i = nxt[i]){
        v = to[i];
        if(bri[i] || col[v]) continue;
        dfs_dcc(v,id);
    }
}

int son[N],siz[N],L[N],R[N],ln[N],lnc = 0,c,n,m;
int ans[N];

void init_dfs(int u,int f,int fe){
    siz[u] = vec[u].size();
    son[u] = 0;
    ln[++ lnc] = u;
    L[u] = lnc;
    for(auto [v,e] : G[u]){
        if(v == f) continue;
        init_dfs(v,u,e);
        siz[u] += siz[v];
        if(!son[u] || siz[v] > siz[son[u]]) son[u] = v;
    }
    R[u] = lnc;
    ine.push_back(fe); inv.push_back(u);
}

void Add_Point(int x,int v){
    for(int u : vec[x]){
        tin.modify(1,1,MXN,a[u],v);
        tout.modify(1,1,MXN,a[u],-v);
        // cout << "MOD " << x << ' ' << u << ' ' << v << endl;
    }
}
void Add_Tree(int u,int v){ for(int i = L[u];i <= R[u];i ++) Add_Point(ln[i],v); }
int query(){ return tin.query() + tout.query(); }

void dsu(int u,int f,int fe){
    for(auto [v,e] : G[u]){
        if(v == f || v == son[u]) continue;
        dsu(v,u,e);
        Add_Tree(v,-1);
    }
    for(auto [v,e] : G[u]){
        if(v == son[u]) dsu(v,u,e);
    }
    for(auto [v,e] : G[u]){
        if(v == f || v == son[u]) continue;
        Add_Tree(v,1);
    }
    Add_Point(u,1);
    // cout << u << ' ' << f << ' ' << tin.query() << ' ' << tout.query() << endl;
    ans[fe] = query();
}

void solve(){
    cin >> n >> m;
    for(int i = 1;i <= n;i ++) cin >> a[i];
    ec = 1; dfc = 0; lnc = 0;
    for(int i = 1;i <= n;i ++) hed[i] = dfn[i] = col[i] = 0;
    for(int i = 1;i <= m;i ++){
        cin >> eu[i] >> ev[i];
        add_edge(eu[i],ev[i]); add_edge(ev[i],eu[i]);
    }
    int S = 0; mx = 0;
    for(int i = 1;i <= n;i ++){
        if(!dfn[i]){
            tarjan(i,0);
            S += mx;
            for(int u : inv) cnt[a[u]] --;
            inv.clear();
        }
    }
    c = 0;
    for(int i = 1;i <= n;i ++){
        if(col[i]) continue;
        c ++; vec[c].clear(); G[c].clear(); siz[c] = 0;
        dfs_dcc(i,c);
    }
    for(int i = 1;i <= m;i ++){
        if(col[eu[i]] != col[ev[i]]){
            G[col[eu[i]]].push_back(make_pair(col[ev[i]],i));
            G[col[ev[i]]].push_back(make_pair(col[eu[i]],i));
        }
    }
    for(int i = 1;i <= c;i ++){
        if(!siz[i]){
            // tin.build(1,1,n);
            // tout.build(1,1,n);
            init_dfs(i,0,0);
            for(int u : inv){
                for(int u2 : vec[u]){
                    tout.modify(1,1,MXN,a[u2],1);
                }
            }
            dsu(i,0,0);
            // cout << i << ", ANS0 = " << ans[0] << endl;
            for(int u : ine) if(u) ans[u] -= ans[0];
            for(int u : inv){
                for(int u2 : vec[u]){
                    tin.modify(1,1,MXN,a[u2],-1);
                }
            }
            ine.clear(); inv.clear();
        }
    }
    for(int i = 1;i <= m;i ++){
        if(col[eu[i]] == col[ev[i]]) cout << S << (i == m ? '\n' : ' ');
        else cout << S + ans[i] << (i == m ? '\n' : ' ');
    }
}

int main(){
    ios::sync_with_stdio(false);
    int TC;
    cin >> TC;
    while(TC --){
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 997ms
memory: 48080kb

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
8 8 9 8 8 8 8 8
4 4 4 5 5 4 4
16 16 16 16
15 15 15 14 15 14 15 14 15 15 16 15 15
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
31 31 31 31 31 32 31 31
12 12 13 12 14 13 12 12 12 12 12 12 12 14 12 12 12 12 12 13
26 25 25 25 25 2...

result:

wrong answer 2nd lines differ - expected: '6 6 7 6 6 6 6 6', found: '8 8 9 8 8 8 8 8'