Submission #407555

# Submission time Handle Problem Language Result Execution time Memory
407555 2021-05-19T02:53:34 Z cpp219 Making Friends on Joitter is Fun (JOI20_joitter2) C++14
0 / 100
4 ms 5324 KB
#pragma GCC optimization "O2"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")

#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 1e5 + 9;
const ll Log2 = 20;
const ll inf = 1e9 + 7;
typedef pair<ll,ll> LL;

ll n,m,x,y,lab[N],ans,in[N];
set<LL> inout[N];

ll Find(ll u){
    if (lab[u] < 0) return u;
    return lab[u] = Find(lab[u]);
}

void Union(ll r,ll s){
    lab[r] += lab[s]; lab[s] = r;
}

ll cal(ll x){
    return x*(x - 1);
}

bool Found(ll id,ll name,ll type){
    return inout[id].find({name,type}) != inout[id].end();
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    #define task "tst"
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        //freopen(task".out", "w", stdout);
    }
    cin>>n>>m; memset(lab,-1,sizeof(lab));
    while(m--){
        queue<ll> Q;
        cin>>x>>y; x = Find(x); y = Find(y);
        if (x == y){
            cout<<ans<<"\n"; continue;
        }
        /// sc = 1 mean out   sc = 0 mean in
        ll par = x;
        if (inout[x].find({y,0}) != inout[x].end()) Q.push(y);
        else{
            if (inout[y].find({x,0}) != inout[y].end()){
                cout<<ans<<"\n"; continue;
            }
            inout[x].insert({y,1});
            inout[y].insert({x,0}); in[y]++;
            ans += -lab[y];
        }
        if (!m){
            //cout<<x<<" "<<y; return 0;
        }
        while(!Q.empty()){

            ll t = Q.front(); Q.pop(); t = Find(t);
            if (Found(par,t,0)) inout[par].erase({t,0}),in[par]--;
            inout[par].erase({t,1});
            if (Found(t,par,0)) inout[t].erase({par,0}),in[t]--;
            inout[t].erase({par,1});
            ll sz1 = in[par],sz2 = in[t];
            ans -= -lab[par]; ans -= cal(-lab[par]); ans -= cal(-lab[t]);
            ans += cal(-lab[t] + -lab[par]);
            ans += sz2*-lab[par] + sz1*-lab[t];
            if (!m){
                //cout<<sz2<<"x"; return 0;
                //cout<<ans<<"x"; return 0;
            }

            if (inout[par].size() < inout[t].size()) swap(par,t);
            Union(par,t);
            for (auto i : inout[t]){
                if (Found(par,i.fs,1 - i.sc)){
                    //cout<<lab[1]<<"x"; return 0;
                    ans -= -lab[Find(i.fs)];
                    Q.push(i.fs); continue;
                }
                inout[par].insert(i);
            }
            inout[t].clear(); //cout<<ans; return 0;
        }
        cout<<ans<<"\n";
    }
}

Compilation message

joitter2.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    1 | #pragma GCC optimization "O2"
      | 
joitter2.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization "unroll-loop"
      | 
joitter2.cpp: In function 'int main()':
joitter2.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5324 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5324 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5324 KB Output isn't correct
2 Halted 0 ms 0 KB -