Submission #219074

# Submission time Handle Problem Language Result Execution time Memory
219074 2020-04-03T15:58:38 Z alexandra_udristoiu Making Friends on Joitter is Fun (JOI20_joitter2) C++14
0 / 100
13 ms 14464 KB
#include<iostream>
#include<set>
#include<vector>
#define DIM 100005
using namespace std;
int n, q, i, j, x, y, r1, r2, ry;
long long sol;
int r[DIM], num[DIM], nv[DIM];
vector<int> v[DIM], vr[DIM];
set<int> s[DIM], sr[DIM];
int rad(int x){
    while(r[x] > 0){
        x = r[x];
    }
    return x;
}
int main(){
    cin>> n >> q;
    for(i = 1; i <= n; i++){
        r[i] = -1;
        num[i] = 1;
        vr[i].push_back(i);
    }
    for(; q; q--){
        cin>> x >> y;
        v[y].push_back(x);
        r1 = rad(x);
        r2 = rad(y);
        if(r1 == r2){
            cout<< sol <<"\n";
            continue;
        }
        if(s[x].find(r2) == s[x].end() ){
            sol += num[r2];
            nv[r2]++;
            s[x].insert(r2);
        }
        sr[r1].insert(r2);
        if(sr[r2].find(r1) == sr[r2].end() ){
            cout<< sol <<"\n";
            continue;
        }
        if(r[r1] > r[r2]){
            swap(r1, r2);
        }
        sol -= num[r1] * 1LL * (num[r1] - 1) + num[r1] * 1LL * nv[r1];
        sol -= num[r2] * 1LL * (num[r2] - 1) + num[r2] * 1LL * nv[r2];
        for(i = 0; i < vr[r2].size(); i++){
            x = vr[r2][i];
            vr[r1].push_back(x);
            if(s[x].find(r1) != s[x].end() ){
                nv[r1]--;
                s[x].erase(r1);
            }
            for(j = 0; j < v[x].size(); j++){
                y = v[x][j];
                ry = rad(y);
                if(ry == r1){
                    s[y].erase(r2);
                    continue;
                }
                if(s[y].find(r1) == s[y].end() ){
                    s[y].insert(r1);
                    nv[r1]++;
                }
                s[y].erase(r2);
                sr[ry].insert(r1);
                sr[ry].erase(r2);
            }
        }
        set<int> :: iterator it;
        for(it = sr[r2].begin(); it != sr[r2].end(); it++){
            sr[r1].insert(*it);
        }
        r[r1] += r[r2];
        r[r2] = r1;
        num[r1] += num[r2];
        sr[r1].erase(r2);
        sol += num[r1] * 1LL * (num[r1] - 1) + num[r1] * 1LL * nv[r1];
        cout<< sol <<"\n";
    }
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:48:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(i = 0; i < vr[r2].size(); i++){
                    ~~^~~~~~~~~~~~~~~
joitter2.cpp:55:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(j = 0; j < v[x].size(); j++){
                        ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Incorrect 12 ms 14464 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Incorrect 12 ms 14464 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 14464 KB Output is correct
2 Correct 13 ms 14464 KB Output is correct
3 Incorrect 12 ms 14464 KB Output isn't correct
4 Halted 0 ms 0 KB -