Submission #212002

# Submission time Handle Problem Language Result Execution time Memory
212002 2020-03-22T00:44:55 Z qkxwsm Making Friends on Joitter is Fun (JOI20_joitter2) C++14
0 / 100
14 ms 16768 KB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

const int MAXN = 100013;

int N, M;
int dsu[MAXN];
vi nodes[MAXN];
ll ans;
set<int> in[MAXN], out[MAXN], cc[MAXN];
//set of nodes going into a clique
//set of cliques going out of a node
//set of cliques going out of a clique
vpi todo;
vpi merges;

int get(int u)
{
    return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
void proc()
{
    int s = todo.back().fi, t = todo.back().se;
    todo.pop_back();
    s = get(s); t = get(t);
    if (s == t) return;
    cc[s].erase(t);
    cc[t].erase(s);
    // cerr << "merge " << s << ' ' << t << endl;
    ans -= (ll) SZ(in[s]) * SZ(nodes[s]);
    ans -= (ll) SZ(in[t]) * SZ(nodes[t]);
    if (SZ(nodes[s]) > SZ(nodes[t])) swap(s, t);
    //so what happens is in[s] and in[t] merge.
    for (int u : nodes[s])
    {
        for (int c : out[u])
        {
            if (cc[c].count(t))
            {
                todo.PB({c, s});
            }
        }
    }
    for (int u : in[s])
    {
        if (cc[t].count(get(u)))
        {
            todo.PB({get(u), t});
        }
        cc[get(u)].erase(s);
        cc[get(u)].insert(t);
    }
    nodes[t].insert(nodes[t].end(), ALL(nodes[s]));
    for (int x : cc[s]) cc[t].insert(x);
    for (int x : in[s])
    {
        out[x].erase(s);
        out[x].insert(t);
        in[t].insert(x);
    }
    in[s].clear(); cc[s].clear();
    dsu[s] = t;
    ans += (ll) SZ(in[t]) * SZ(nodes[t]);
    if (!todo.empty()) proc();
    return;
}
void update(int u, int v)
{
    v = get(v);
    if (in[v].count(u)) return;
    //if v -> u doesn't exist.
    if (!cc[v].count(get(u)))
    {
        // cerr << "no " << v << ' ' << get(u) << endl;
        in[v].insert(u);
        // cerr << "in " << v << " insert " << u << endl;
        out[u].insert(v);
        ans += SZ(nodes[v]);
        cc[get(u)].insert(v);
        return;
    }
    todo.PB({get(u), v});
    proc();
}

int32_t main()
{
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(4);
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N >> M;
    FOR(i, 0, N)
    {
        dsu[i] = i;
        nodes[i].PB(i);
        in[i].insert(i);
        cc[i].insert(i);
    }
    //cc stores the edges in a forward direction.
    FOR(i, 0, M)
    {
        int u, v;
        cin >> u >> v; u--; v--;
        update(u, v);
        ans = -N;
        FOR(i, 0, N)
        {
            ans += SZ(in[i]) * SZ(nodes[i]);
        }
        cout << ans << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 16768 KB Output is correct
2 Correct 13 ms 16768 KB Output is correct
3 Correct 13 ms 16768 KB Output is correct
4 Correct 13 ms 16768 KB Output is correct
5 Incorrect 13 ms 16768 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 16768 KB Output is correct
2 Correct 13 ms 16768 KB Output is correct
3 Correct 13 ms 16768 KB Output is correct
4 Correct 13 ms 16768 KB Output is correct
5 Incorrect 13 ms 16768 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 16768 KB Output is correct
2 Correct 13 ms 16768 KB Output is correct
3 Correct 13 ms 16768 KB Output is correct
4 Correct 13 ms 16768 KB Output is correct
5 Incorrect 13 ms 16768 KB Output isn't correct
6 Halted 0 ms 0 KB -