QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#339398#6127. Kawa Exam1820357523TL 0ms4620kbC++145.4kb2024-02-27 11:04:322024-02-27 11:04:34

Judging History

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

  • [2024-02-27 11:04:34]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:4620kb
  • [2024-02-27 11:04:32]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
#define Endl "\n"
#define emdl "\n"
#define Emdl "\n"
#define __count __builtin_popcount
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
const int N=2e5+5;
using namespace std;
std::vector<ll> gt(200005,0);ll op=0;ll te=0;
ll f[200005],r[200005];

void init(ll n){
    for(ll i=1;i<=n;i++){
        r[i]=1;
        f[i]=i;
    }
}

ll find(ll x){
    if(x==f[x]) return x;
    else return f[x]= find(f[x]);
}

void merge(ll a,ll b){
    ll x=find(a);ll y= find(b);
    if (r[x] >= r[y])
        f[y] = x;
    else
        f[x] = y;
    if (r[x] == r[y] && x != y)
        r[x]++;
}

void tarjan(std::vector<std::vector<ll>> e,ll n){
    ll m,Q,u,v,fa[N];
    ll cnt=0,ins[n+1],low[n+1],dfn[n+1];
    for(ll i=1;i<=n;i++) {
        low[i]=ins[i]=dfn[i]=0;
    }
    stack<ll> s;

    function<void(ll, ll)> dfs = [&](ll u, ll fa) {
        low[u] = dfn[u] = ++cnt;
        s.push(u), ins[u] = 1;
        for (int i = 0; i < e[u].size(); i++) {
            int v = e[u][i];
            if (v == fa) {
                continue;
            }
            if (!dfn[v]) {
                dfs(v, u);
                low[u] = min(low[u], low[v]);
            }
            else if (ins[v])
                low[u] = min(low[u], dfn[v]);
        }
        if (dfn[u] == low[u]) {
            while (s.top() != u) {
                merge(u, s.top());
                ins[s.top()] = 0;
                s.pop();
            }
            ins[u] = 0;
            s.pop();
        }
    };

    for(ll i=1;i<=n;i++){
        if(!low[i]) dfs(i,0);
    }
}

void solve() {
    op++;
    ll n,m;cin>>n>>m;
    init(n);
    ll v[n+1];ll i;
    for(i=1;i<=n;i++) cin>>v[i];

    std::vector<std::vector<ll> > a(n+1);
    std::map<pair<ll,ll>,queue<ll>> mp;
    ll tot=0;
    while(m--){
        tot++;
        ll x,y;cin>>x>>y;
        if(mp[{x,y}].size()||x==y){
            mp[{x,y}].push(tot);
            if(x!=y){
                swap(x,y);
                mp[{x,y}].push(tot);
            }
            continue;
        }
        a[x].push_back(y);
        a[y].push_back(x);
        mp[{x,y}].push(tot);
        swap(x,y);
        mp[{x,y}].push(tot);
    }

    tarjan(a,n);
//    cout<<"tarjan\n";
//    for(i=1;i<=n;i++){
//        cout<<i<<" "<<find(i)<<endl;
//    }
    std::vector<ll> cnt(tot+1,n),siz(n+1,0),vt(n+1,0),ans(n+1,0);
    ll sum=0;
    std::vector<std::map<ll,ll>> mvp(n+1),mvvp(n+1);
    std::map<ll,std::map<ll,ll>> dsu;
    std::multiset<ll> se[n+1];std::unordered_map<ll,ll> gg,ggg,gggg;
    function<void(ll,ll)  > dfs=[&](ll now,ll old){
        siz[now]++;
        for(auto x:a[now]){
            if(x==old||siz[x]) continue;
            dfs(x,now);
        }
        mvp[now][v[now]]++;
        ll maxt=1;
//        gg[now]++;
        for(auto x:a[now]){
            if(x==old) continue;
            if(gg[x]) continue;
            gg[x]++;
            if(mvp[x].size()>mvp[now].size()){
                swap(mvp[now],mvp[x]);
            }
            for(auto [c,d]:mvp[x]){
                mvp[now][c]+=d;maxt=max(maxt,mvp[now][c]);
            }
        }
        ans[now]=maxt;
    };
    for(i=1;i<=n;i++){
        if(siz[i]==0) {
            dfs(i,i);
            dsu[i]=mvp[i];
            sum+=ans[i];
            for(auto [x,y]:mvp[i]) se[i].insert(y);
        }
    }
//    cerr<<sum<<endl;
    ll g=0;
    std::map<pair<ll,ll>,ll> cc;
    function<void(ll,ll) > answer=[&](ll now,ll old){
        vt[now]++;
        for(auto x:a[now]){
            if(x==old||vt[x]) continue;
            answer(x,now);
        }
        mvvp[now][v[now]]++;
//        cout<<"now ";
//        cout<<now<<endl;
        for(auto x:a[now]){
            if(x==old) {
                continue;
            }
//            if(gggg[x] ) continue;
//            gggg[x]++;
            if(find(x)==find(now)) {
                continue;
            }
            if(mp[{x,now}].size()>=2) continue;

            for(auto [c,d]:mvvp[x]){
                se[g].insert(dsu[g][c]-d);
                if(se[g].count(dsu[g][c])) se[g].erase(se[g].find(dsu[g][c]));
//                cout<<c<<" ";
            }
            if(mp[{now,x}].size()){
                cnt[mp[{now,x}].front()]=sum-ans[g]+(se[g].size()==0?0:*--se[g].end())+ans[x];
                mp[{now,x}].pop();
                mp[{x,now}].pop();
            }
            for(auto [c,d]:mvvp[x]){
                se[g].insert(dsu[g][c]);
                if(se[g].count(dsu[g][c]-d))se[g].erase(se[g].find(dsu[g][c]-d));
            }
        }
//        ggg[now]++;
        for(auto x:a[now]){
            if(x==old) continue;
            if(ggg[x]) continue;
            ggg[x]++;
            if(mvvp[x].size()>mvvp[now].size()){
                swap(mvvp[x],mvvp[now]);
            }
            for(auto [c,d]:mvvp[x]){
                mvvp[now][c]+=d;
            }
        }
    };
    for(i=1;i<=n;i++){
        if(!vt[i]){
            g=i;
            answer(i,i);
        }
    }
    for(auto [x,y]:mp){
            while(!y.empty()){
                cnt[y.front()]=sum;
                y.pop();
            }
    }
    for(i=1;i<=tot;i++){
        gt[i]=0;
        cout<<cnt[i]<<" \n"[i==tot];
    }

}

signed main(){
//    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll t;cin>>t;while(t--)
        solve();

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4620kb

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
Time Limit Exceeded

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
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
7 8
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
9 10 9
16 16 16 16 16 17 16 16
10 7 8 7 9 8 7 7 7 7 7 7 7 9 8 7 7 7 7 8
6 5 5 5 5 5 5 5 5 5 5 5 6 5 6 4 5 6
1 1 1 1 1 1 1 1...

result: