답안 #407185

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
407185 2021-05-18T15:25:55 Z cpp219 조이터에서 친구를 만드는건 재밌어 (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;
set<ll> in[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);
}

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;
        }
        ll par = x;
        if (in[x].find(y) != in[x].end()) Q.push(y);
        else{
            if (in[y].find(x) != in[y].end()){
                cout<<ans<<"\n"; continue;
            }
            in[y].insert(x); ans += -lab[y];
        }
        if (!m){
            //cout<<x<<" "<<y; return 0;
        }
        while(!Q.empty()){
            ll t = Q.front(); Q.pop(); t = Find(t);
            in[par].erase(t); in[t].erase(par);
            ll sz1 = in[par].size(),sz2 = in[t].size();
            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 (in[par].size() < in[t].size()) swap(par,t);
            Union(par,t);
            for (auto i : in[t]){
                if (in[par].find(i) != in[par].end()){
                    Q.push(i); continue;
                }
                in[par].insert(i);
            }
            in[t].clear();
        }
        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:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 5324 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 5324 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 5324 KB Output isn't correct
2 Halted 0 ms 0 KB -