Submission #430976

#TimeUsernameProblemLanguageResultExecution timeMemory
4309762qbingxuanMaking Friends on Joitter is Fun (JOI20_joitter2)C++14
0 / 100
2 ms2636 KiB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#ifdef local
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define pary(a...) danb(#a, a)
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
    int cnt = sizeof...(T);
    ((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
    std::cerr << "\033[1;32m[ " << s << " ] = [ ";
    for (int f = 0; L != R; ++L)
        std::cerr << (f++ ? ", " : "") << *L;
    std::cerr << " ]\033[0m\n";
}
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#define pary(...) ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define pb emplace_back
#define sort_uni(v) sort(all(v)),v.erase(unique(all(v)),v.end())

using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
const int mod = 1000000007;
const int inf = 1e9;
const ll INF = 1e18;
const int maxn = 100025;

int pa[maxn], sz[maxn];
int anc(int x) { return x==pa[x] ? x : pa[x]=anc(pa[x]); }
vector<int> out[maxn];
void join(int x, int y) {
    x = anc(x);
    y = anc(y);
    if (x == y)
        return;
    pa[x] = y;
    sz[y] += sz[x];
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) pa[i] = i, sz[i] = 1;
    set<pair<int,int>> edges;
    while (m--) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        if (edges.count({ b, a }))
            join(a, b);
        edges.insert({ a, b });
        out[a].push_back(b);

        ll ans = 0;
        for (int i = 0; i < n; i++) {
            for (int &x: out[i])
                x = anc(x);
            sort_uni(out[i]);
            for (int x: out[i])
                if (x != anc(i))
                    ans += sz[x];
        }
        for (int i = 0; i < n; i++) if (anc(i) == i) ans += 1LL * sz[i] * (sz[i] - 1);
        cout << ans << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...