Submission #419229

# Submission time Handle Problem Language Result Execution time Memory
419229 2021-06-06T15:00:38 Z usachevd0 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
5 ms 7244 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define Time (clock() * 1.0 / CLOCKS_PER_SEC)
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
    return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
    return y > x ? (x = y, true) : false;
}
void debug_out() {
    cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
    cerr << ' ' << A;
    debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
    for (int i = 0; i < n; ++i)
        cerr << a[i] << ' ';
    cerr << endl;
}
#ifdef LOCAL
    #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
    #define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
    #define debug(...) 1337
    #define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T>& v) {
    for (auto& x : v)
        stream << x << ' ';
    return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
    return stream << p.first << ' ' << p.second;
}

const int maxN = 1e5 + 5;
int n, m;

vector<int> comp[maxN];
int where[maxN];
set<int> in_edges[maxN];
ll cur;

set<pii> E;

void init() {
    for (int v = 0; v < n; ++v) {
        comp[v] = {v};
        where[v] = v;
        in_edges[v] = {v};
    }
}

ll f(int c) {
    return comp[c].size() * (ll)(in_edges[c].size() - 1);
}

void add_in_edge(int a, int b) {
    b = where[b];
    cur -= f(b);
    in_edges[b].insert(a);
    cur += f(b);
}

void join(int a, int b) {
    a = where[a], b = where[b];
    if (a == b) return;
    if (comp[a].size() > comp[b].size())
        swap(a, b);
    cur -= f(a);
    cur -= f(b);
    for (int x : comp[a]) {
        comp[b].push_back(x);
        where[x] = b;
    }
    if (in_edges[a].size() > in_edges[b].size())
        swap(in_edges[a], in_edges[b]);
    for (int x : in_edges[a])
        in_edges[b].insert(x);
    cur += f(b);
}

int32_t main() {
#ifdef LOCAL
    freopen("in", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    cin >> n >> m;
    init();
    while (m--) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        E.insert({a, b});
        add_in_edge(a, b);
        if (E.count({b, a})) {
            join(a, b);
        }
        cout << cur << '\n';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 7244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 7244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 7244 KB Output isn't correct
2 Halted 0 ms 0 KB -